How to detect changes of more than 15px in Resize in the [duplicate] window

0

I need to trigger an event (alert) whenever a window is resized, but only when the difference goes from 15 pixels (for less or more).

What is the simplest way to calculate these 15px?

    
asked by anonymous 19.03.2015 / 20:02

1 answer

2

If I understood correctly, you want something to happen every 15 pixels of resize. Because during window resizing the resize event occurs several times, you need to create "milestones" where you save the previous window dimensions, and compare the current value with those milestones. Something like this:

var ultimaLargura = window.innerWidth, 
    ultimaAltura = window.innerHeight;

window.onresize = function() {
    var diffLargura = Math.abs(ultimaLargura - window.innerWidth);
    var diffAltura = Math.abs(ultimaAltura - window.innerHeight);
    if(diffLargura >= 15 || diffAltura >= 15) {
        console.log('executando');   
        ultimaLargura = window.innerWidth;
        ultimaAltura = window.innerHeight;
    }
};

link

    
19.03.2015 / 20:45