0

So I have been looking around on SO and google for 20 minutes or so and cannot figure out if there is a jQuery function or a block of code somewhere that does this but what I am trying to due is run various jQuery functions as you scroll down the page. Is this possible, I was thinking there might be a way to use anchors to do this and just run a piece of code when you scroll to each anchor point? Thanks

4

3 回答 3

0

尝试这个:

$(window).on("scroll", function() {
  var scrollPosition = scrollY || pageYOffset;

  if (scrollPosition > $("#someAnchor").position().top - $(window).height()) {
    alert("run code here");
  }
});

只要向下滚动到 ID 为“someAnchor”的元素,就会显示警报。这是您可以运行代码的地方。

这是一个 jsfiddle 来演示这一点

于 2013-08-26T15:06:30.173 回答
0

来自 jQuery API:

.scroll() 将事件处理程序绑定到“滚动”JavaScript 事件,或在元素上触发该事件。

http://api.jquery.com/scroll

于 2013-08-26T14:36:04.350 回答
0

用 jQuery 试试这个:

$(window).scroll(function () { 
 console.log("Hi");
});
于 2013-08-26T14:37:32.223 回答