0
对不起,这不是更具体,但我无法隔离问题。为什么setInterval在我的jQuery插件中似乎有范围问题?
我写了a very simple jQuery plugin,它可以在一个设定的时间间隔内通过div(例如轮播)滚动图像或其他元素。我希望这个插件可以在一个页面上使用多个实例,但是当我在多个元素上调用它时,只有最后一个初始化的元素会滚动。我假设我使用setInterval的方式是原因,但我不明白为什么。
滚动功能如下,完整源代码链接在上面。
function scrollRight() {
// Don't animate if the mouse is over the scrollah
if (hovering) { return; }
/* If we're at the end, flip back to the first image
* before animating, lest we view blankness in the wrapper
*/
if (position === nChildren) {
position = 0;
$wrapper.css('left', '0px');
}
// Animate to the next view
position++;
$wrapper.animate({
left: position*-width+'px'
}, 1000, 'swing', function() {
// Animation complete.
});
}
setInterval(scrollRight, 5000);
那么为什么这个插件的个别实例不再滚动一次又被初始化了?
需要看到更多的代码,即'$ wrapper'和其他变量的定义。 – mVChr 2011-05-25 19:25:28
你有没有尝试将第一个参数包装成一个匿名函数:'setInterval(function(){scrollRight();},5000);'? – 2011-05-25 19:26:39
@ gion_13,这没有什么区别。 – dtbarne 2011-05-25 19:29:10