Aditya Aditya - 10 months ago 44
Javascript Question

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

I have this code

var oldwidth= $(window).width();
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 Source

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;