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