2012-12-23 138 views
0

如何在兩個y位置之間手動滾動時自動調整.scrollTo()(使用插件)? (if().scroll()內部的聲明)jQuery如何在某個位置手動滾動時滾動到?

下面的代碼看起來很簡單。但是,滾動有點顛簸,當它自動滾動到內容時,滾動會停留在那裏。彷彿它想要做兩件事。

scrollToContent = function() { 
    $(document).stop(1, 1).scrollTo($('.content'), 750, { easing: 'easeInOutQuint' }); 
} 

$(window).scroll(function() { 
    if ($(this).scrollTop() <= $(this).height()) { 
     scrollToContent(); 
    } 
}); 
+1

看起來它試圖做2件事情,因爲它是_is_試圖做2件事情。它試圖在用戶滾動時自動滾動。每次用戶滾動時,動畫都會停止並重新開始。聽起來像你可能想要[禁用用戶滾動](http://stackoverflow.com/questions/4770025/how-to-disable-scrolling-temporarily),而滾動自動發生。 –

+0

我試過了,但是如何檢查用戶是否在滾動?除了$(window).scroll()'?因爲這樣,我得到了同樣的結果。 – fishbaitfood

回答

2

這太長了評論,所以我把它作爲一個答案,而不是。 $(window).scroll()是您想用來檢查用戶是否滾動的方法。如果與scrollTo()插件混淆,請嘗試設置標誌。例如:

var scrolling = false; 

scrollToContent = function() { 
    scrolling = true; 

    // disable user scroll here 

    $(document).stop(1, 1).scrollTo($('.content'), 750, { easing: 'easeInOutQuint', onAfter: function() { 
    scrolling = false; 

    // reenable user scroll here 
    }}); 
} 

$(window).scroll(function() { 
    if ($(this).scrollTop() <= $(this).height() && !scrolling) { 
    scrollToContent(); 
    } 
}); 
+0

真棒,基督徒!這工作得很好。 – fishbaitfood