jQuery $(window).resize();等效的事件侦听器,只在指定的轴上触发更改?
问题描述:
我正在寻找一个事件监听器,它的工作原理就像jQuery的.resize(),但是只有当调整大小的对象(谈论窗口)在x轴上或两者都调整大小时才会触发,而不是在y轴上。 - 所以基本上只会收听宽度调整大小的事件。
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.
答
您可以将浏览器的宽度保存在窗口上加载变量。示例:
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();
delete w;
}
});