2016-08-04 86 views
0

所以我試圖讓一些div滾動到他們的內容當用戶滾動某個地方。這是我目前的代碼:jQuery滾動事件不斷觸發滾動功能

var target = 0; 
var main_scroll = 0;  
var top_scroll = 0; 
var bot_scroll = 0; 
var counter = 1; 

$('.main-container').on('scroll', function() { 
    if ($('#trigger-' + (counter + 1)).length && $(this).scrollTop() > target) {     
     main_scroll = main_scroll + $('.main-container').height();   
     $('.main-container').animate({    
      scrollTop: main_scroll    
     }, 1000); 

     top_scroll = top_scroll + $('.top-container').height();   
     $('.top-container').animate({    
      scrollTop: top_scroll    
     }, 1000); 

     bot_scroll = bot_scroll + $('.bottom-container').height();   
     $('.bottom-container').animate({     
      scrollTop: bot_scroll    
     }, 1000); 

     target = target + $('.main-container').height();    
     counter++;    
    } else if ($('#trigger-' + (counter - 1)).length && $(this).scrollTop() < target) { 
     main_scroll = main_scroll - $('.main-container').height();    
     $('.main-container').animate({    
      scrollTop: main_scroll    
     }, 1000); 

     top_scroll = top_scroll - $('.top-container').height();    
     $('.top-container').animate({    
      scrollTop: top_scroll    
     }, 1000); 

     bot_scroll = bot_scroll - $('.bottom-container').height();   
     $('.bottom-container').animate({     
      scrollTop: bot_scroll    
     }, 1000); 

     target = target - $('.main-container').height();    
     counter--;    
    }   
}); 

就像現在這樣,如果我滾動一旦divs不斷反彈上下。不是100%確定我做錯了什麼。

+0

一個可行的例子在這裏會有很多幫助,無論是在一個片段或一個http://jsfiddle.net –

回答

0

我實際上是能夠通過使用來解決這個問題:

if (! $(this).is(':animated')) { 

確保滾動事件沒有被我的動畫解僱。