2013-08-21 273 views
-1
  • 如何检测用户是否使用jQuery滚动屏幕的50%? (在这里我可以说,如果它滚动50像素)
  • 然后,动画,去#second或者,有什么不一样,前100%(它似乎动画,但事情发生了疏远)

这里的例子:http://jsfiddle.net/NH6Km/2/滚动%屏幕

JQUERY:

$(function(){ 
$(window).scroll(function() { 
    if ($(window).scrollTop() > 50) { 
    ('body,html').animate({ scrollTop:    
    $('#second').offset().top }, 1500); 
    } 
});  
}) 

HTML:

<div id="first"></div> 
<div id="second"></div> 

CSS:

#first{ 
    position:absolute; 
    width:100%; height:100%; 
    background:blue; 
} 
#second{ 
    position:absolute; 
    top:100%; 
    width:100%; height:100%; 
    background:yellow; 
} 
+0

因此,当用户开始滚动时,您想要中断它们的滚动并滚动到下一个div。正确? –

+0

我尝试动画到下一个/ prev div,如果它在中间。这可能是用户开始滚动时,或者当他完成滚动时可能更好? – Nrc

+0

你怎么知道它是在中间? (简单的答案)。当滚动停止时检测,如果它在中间,则滚动到两者中较近的一个。 –

回答

0

正如@thecodedestroyer说,你可以使用滚动事件做这样的事情:

var currentDiv = "#first"; 
var divArray = ["#first", "#second", "#third", "#fourth"]; 

$(window).on("scroll", function (e) { 
    var ix = divArray.indexOf(currentDiv); 
    if (ix >= 0) { 
     if (window.pageYOffset > $(currentDiv).offset().top) { 
      currentDiv = divArray[(ix + 1) % currentDiv.length]; 
     } else if (window.pageYOffset < $(currentDiv).offset().top) { 
      currentDiv = divArray[(ix - 1) % currentDiv.length]; 
     } 
     $("body, html").animate({ 
      scrollTop: $(currentDiv).offset().top 
     }, 0); 
     e.preventDefault(); 
     return false; 
    } 
}); 

这里测试: http://jsfiddle.net/cxJQE/