2013-08-21 58 views
-1

请帮我下面的场景动态滚动位置jQuery中

我在的地方,用户滚动3页的页面滚动时到达第二页,它必须找到特定的ID,然后触发功能。一旦第三页启动另一个功能触发器。 按要求我不应该使用任何插件

<script> 
    $(window).on("scroll", function() { 
     var offset = $("#two").offset(); 
     var posY = offset.top - $(window).scrollTop(); 
     if (offset.top + $("#two").height() > $(window).scrollTop()) { 
      apply(); 
     } else { 
      remove(); 
     } 
</script> 
<body> 
    <div id="one"></div> 
    <div id="two"></div> 
    <div id="three"></div> 
</body> 
+2

你能告诉我们你已经尝试了代码? – Richard

回答

2

有一个伟大的插件来做到这一点叫WAYPOINT

我已经安装的例子在jsfiddle check it out

HTML

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

CSS

.first { 
    background:green; 
    height: 600px; 
    width:100%; 
} 
.second { 
    background:red; 
    height: 600px; 
    width:100%; 
} 

JS

$('.second').waypoint(function() { 
    alert('You have scrolled to an entry.'); 
}, { 
    offset: '100%' 
}); 
+1

嗨感谢您的回复。根据要求,我不应该使用任何插件。你有没有其他想法? – michaelvino86