一旦页面到达页面顶部,如何让div向下滚动?

我知道标题有点令人困惑; D但基本上我想要做的就是在这个网站上清楚地展示http://9gag.com向下滚动并注意侧边栏,有2个广告一旦第二个广告到达页面顶部它开始向下滚动页面。

我想知道怎么做? html / css或jQuery解决方案已经预先准备好了。

正如凯文指出的那样,你需要做的是听取文档的scroll事件。 当它被触发时,你会想要查看scrollTop的值。

 $(document).scroll(function() { var useFixedSidebar = $(document).scrollTop() >= myThresholdValue; $('.my-sidebar-items').toggleClass('fixed', useFixedSidebar); }); 

你的CSS基本上是这样的:

 .my-sidebar-items.fixedSidebar { position: fixed; } 

一个复杂的因素是,您可能希望将项目修复为新的顶部位置,并且您希望执行以下操作:

 .my-sidebar-items.fixedSidebar { position: fixed; top: 0 } 

但是,这可能会将您的物品堆叠在一起。 解决方法是将fixedSidebar类放在容器上,并按照描述使用该类。

演示

您可以使用粘性条http://archive.plugins.jquery.com/plugin-tags/sticky或jQuery Waypoints http://imakewebthings.github.com/jquery-waypoints/