【发布时间】:2013-02-07 00:52:44
【问题描述】:
我在谷歌上搜索并遇到了许多类似的问题,人们试图在 jQuery 插件中设置超时,但我正在努力寻找答案,这不是一个懒惰的帖子。
我试图在调用动画以隐藏某些内容时实现延迟,例如,如果用户将鼠标悬停在某个区域上,则会有更多内容进入视图并隐藏原始内容。然后,当用户在 2 秒后将鼠标移开时,会返回原始内容。
尽管忽略了超时,但动画按预期工作。这是我无法理解的!
我们将一如既往地对代码提供任何帮助!
这是简化的代码,专注于动画,但我保留了插件结构:-
;(function($){
$.fn.extend({
pluginName: function(options) {
// - Settings list and the default values
var defaults = {
width: this.css('width'),
};
var options = $.extend({}, defaults, options);
return this.each(function() {
// -- Globals
var o = options;
var timeoutID;
function deceptionAnimate(display) {
if(display == 1) {
obj.clearQueue().animate({
'top': 0,
'left': -o.width
}, o.interval, o.easing);
} else if(display == 0) {
obj.clearQueue().animate({
'top': 0,
'left': 0
}, o.interval, o.easing)
}
}
function delaydeceptionAnimate () {
timeoutID = window.setTimeout(deceptionAnimate(0), 2000);
}
// ---- Initiate
function init() {
// ----- Animate
$(document).on(o.eventTrigger, wrapperID, function() {
deceptionAnimate(1);
});
$(document).on('mouseout', wrapperID, function() {
delaydeceptionAnimate(0);
});
}
// Call
init();
});
}
});
})(jQuery);
【问题讨论】:
标签: javascript jquery jquery-plugins timeout