Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery $(window).resize(); equivalent event listener, that only fires on a specified axis change?

I am looking for an event listener, what works like jQuery's .resize(), but only fires when the resized object (talking about the window) is resized in x axis, or both, but not in only y axis. - So basically it will only listen the resize events of the width.

like image 985
István Pálinkás Avatar asked Jan 08 '14 08:01

István Pálinkás


2 Answers

You can save the width of the browser on a window load in variable. Example:

var w = 0;

$( window ).load( function(){

   w = $( window ).width();

});

$( window ).resize( function(){

  if( w != $( window ).width() ){

    //Do something

    w = $( window ).width();


  }

});
like image 167
edonbajrami Avatar answered Oct 16 '22 16:10

edonbajrami


How about using like this?

var w = $(window).width();
$(window).resize(function(){
  if ($(window).width()==w) return; 
  w = $(window).width();
  // ... your code
});
like image 37
Bhojendra Rauniyar Avatar answered Oct 16 '22 16:10

Bhojendra Rauniyar