Detect a window width change but not a height change

shrewdbeans picture shrewdbeans · May 25, 2012 · Viewed 74k times · Source

I'm using the .resize() function to detect window re-size events, but this detects both height and width changes.

Is there any way to detect just a width change and not a height change?

Answer

thecodeparadox picture thecodeparadox · May 25, 2012
var width = $(window).width();
$(window).on('resize', function() {
  if ($(this).width() !== width) {
    width = $(this).width();
    console.log(width);
  }
});