Aditya Aditya - 1 month ago 5
Javascript Question

how to get width of screen before every resize on page?

I have this code

$(document).ready(function(){
var oldwidth= $(window).width();
$(window).resize(function(){
var nw= $(window).width();

//compare new and old width
});
});


The problem is the old width is only set during loading of document, new width changes during every resize. I want to get the width just before resizing, so I can check if user is decreasing width or increasing width of screen.

Answer

In the resize() handler, update oldwidth with the new width as the very last line of the function.

$(document).ready(function () {
    var oldwidth = $(window).width();
    $(window).resize(function () {
        var nw = $(window).width();
        //compare new and old width      
        oldwidth = nw;
    });
});
Comments