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?
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?
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;
}
};