2012-10-09 79 views
0

我該如何增加滾動循環內的函數?for循環內滾動功能

我:

$(window).scroll(function() { 
b1Center = $("#block-1").offset().top - ($(window).height() - divHeight)/2; 
b1Bottom = $("#block-1").offset().top - $(window).height() 
b1Top = $("#block-1").offset().top + divHeight; 
if(getScrollTop() > b1Bottom && getScrollTop() < b1Top){ 
$("#block-1 .txt").css('marginTop', ((getScrollTop()) *(1.6)) + 'px'); 
}... 

需要進行滾動功能REDY所有的時間內,我對所有的按鈕,使相同的塊:

http://jsfiddle.net/Dx3hr/27/

希望這將使更多感。

回答

1

試試這個:

$(document).ready(function(){ 
    var divHeight = 700; 
    $(window).scroll(function() { 
     $(".blocks").each(function(){ 
      var this = $(this), 
       Center = $(this).offset().top - ($(window).height() - divHeight)/2, 
       Bottom = $(this).offset().top - $(window).height(), 
       Top = $(this).offset().top + divHeight; 
      if(getScrollTop() > Bottom && getScrollTop() < Top){ 
       this.find('.txt').css('marginTop', ((getScrollTop()) *(1.6)) + 'px'); 
      } 
     });  
    });  
}); 
+0

因爲這是視差,所以它會取消工作,因爲滾動會同時滾動所有窗口,如果我在那裏滾動,那麼窗口中沒有內容 – user1709407

0

將這個你.scrollTop()內循環:

$('.block').each(function () { 
    var this_block = $(this); 
    this_block.whateverFunction(you, want, to, do); 
}); 

確保使用類的按鈕,而不是ID的...