当 JQuery 中的 resize() 时,触发器会多次触发
我使用 Paul Irish Smartresize,但是当我调整窗口大小时,resize() 内部的函数会多次触发,导致我的手风琴无法正常工作。 有谁知道为什么会发生这种情况? 这是运行的代码: http://jsfiddle.net/rebel2000/PnAH7/6/
$(document).ready( function(){
(function($,sr){
// debouncing function from John Hann
// http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/
var debounce = function (func, threshold, execAsap) {
var timeout;
return function debounced () {
var obj = this, args = arguments;
function delayed () {
if (!execAsap)
func.apply(obj, args);
timeout = null;
};
if (timeout)
clearTimeout(timeout);
else if (execAsap)
func.apply(obj, args);
timeout = setTimeout(delayed, threshold || 100);
};
}
// smartresize
jQuery.fn[sr] = function(fn){ return fn ? this.bind('resize', debounce(fn)) : this.trigger(sr); };
})(jQuery,'smartresize');
function anim3() {
$('#button').click(
function(){
if($(this).hasClass('active')){
$(this).animate({ "height": "30px"}, { queue:true, duration: 900 });
$(this).removeClass('active');
return false;
} else {
$(this).animate({ "height": "100px"}, { queue:true, duration: 900 });
$(this).addClass('active');
return false;
}
}
);
}
//anim3();
$(window).smartresize(function(){
anim3();
});
});
I use Paul Irish Smartresize but when I resize the window the function inside resize() fires multiple times causing my accordion not to work properly.
Does anyone have any idea why this happens?
Here is the code running: http://jsfiddle.net/rebel2000/PnAH7/6/
$(document).ready( function(){
(function($,sr){
// debouncing function from John Hann
// http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/
var debounce = function (func, threshold, execAsap) {
var timeout;
return function debounced () {
var obj = this, args = arguments;
function delayed () {
if (!execAsap)
func.apply(obj, args);
timeout = null;
};
if (timeout)
clearTimeout(timeout);
else if (execAsap)
func.apply(obj, args);
timeout = setTimeout(delayed, threshold || 100);
};
}
// smartresize
jQuery.fn[sr] = function(fn){ return fn ? this.bind('resize', debounce(fn)) : this.trigger(sr); };
})(jQuery,'smartresize');
function anim3() {
$('#button').click(
function(){
if($(this).hasClass('active')){
$(this).animate({ "height": "30px"}, { queue:true, duration: 900 });
$(this).removeClass('active');
return false;
} else {
$(this).animate({ "height": "100px"}, { queue:true, duration: 900 });
$(this).addClass('active');
return false;
}
}
);
}
//anim3();
$(window).smartresize(function(){
anim3();
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
发生这种情况是因为当您调整大小时,调整大小事件会多次触发。理论上(更多用于说明目的)当 JavaScript 循环验证窗口大小时,它检测到它比以前更小/更大,并再次触发它。由于循环速度非常快,因此在“单次”调整大小期间会发生多次火灾。
您可以执行以下操作:
这应该安全地忽略多个火灾,并且仅处理最后一个火灾。 (除非你需要花费很多时间来调整大小,否则你可以增加超时时间)
That happens because when you are re-sizing, the re-size event fires multiple times. Teorically(more for illustration purposes) when the JavaScript loop goes through the verification of the window size it detects it is smaller/larger than before and fires it again. As the loop is very fast you get multiple fires, during your "single" re-size.
You can do something like this:
This should safely ignore the multiple fires and only process on the last one. (Unless you take lots of time to resize, in that case you can increase the timeout)
这里有一段很棒的代码可以为您处理这个问题:
http://benalman.com/ items/jquery-throttle-debounce-plugin/
另请参阅:
http://benalman.com/projects/jquery-dotimeout-plugin/
Here's a great piece of code that handles this for you :
http://benalman.com/projects/jquery-throttle-debounce-plugin/
Also see:
http://benalman.com/projects/jquery-dotimeout-plugin/