Skip to content Skip to sidebar Skip to footer

Jquery - How To Know If Window Is Resizing In Width/height Or Both?

I have a little problem with window resizing using jQuery's function .resize(). I would like to know which dimension is getting bigger/smaller - width or height. I need this becaus

Solution 1:

By saving last window size values in variables.

var h = $(window).height(), w = $(window).width();
$(window).resize(function(){

    var nh = $(window).height(), nw = $(window).width();
     // compare the corresponding variables.
    h = nh; w = nw; // update h and w;
});

Solution 2:

Save the previous size and compare with it, everytime the size changes.

For ex:

var prevW = -1, prevH = -1;

$(document).ready(function() {

    // ... other stuff you might have inside .ready()

    prevW = $(window).width();
    prevH = $(window).height();
});

$(window).resize(function() {
    var widthChanged = false, heightChanged = false;
    if($(window).width() != prevW) {
        widthChanged  = true;
    }
    if($(window).height() != prevH) {
        heightChanged = true;
    }

    // your stuff

    prevW = $(window).width();
    prevH = $(window).height();

});

Demo:http://jsfiddle.net/44aNW/

Post a Comment for "Jquery - How To Know If Window Is Resizing In Width/height Or Both?"