0

当滚动条不在文档顶部时,我想用 jQuery 触发一个事件。到目前为止,我只能在文档顶部使用它。"!==" 似乎不起作用。

$(document).ready(function() {
  if($(window).scrollTop() === 0) {
    alert("top!")
  };
});
4

2 回答 2

2

Try this:

$(document).ready(function() {
  $(window).scroll(function() { /* or whatever element you want to attach to */
      if($(window).scrollTop() === 0) {
        alert("top!")
      };
  });
});

Of course, this will trigger every time you scroll to the top, which may not be what you want.

于 2013-10-18T21:14:43.200 回答
1

You need to use the .scroll() event

$(document).ready(function() {
  $(window).scroll(function() {
      if($(window).scrollTop() !== 0) {
        alert("not top!")
      };
  });
});

http://jsfiddle.net/EX2q2/

于 2013-10-18T21:15:32.130 回答