简体   繁体   中英

Scroll event only fire when scrolling top

I have a scroll function. It needs to alert when you scroll to bottom. Strangely, it only alerts when you scroll to top. What is the correct way to make it work when you scroll at bottom.

$(window).scroll(function() {
  if ($(window).scrollTop() == $(document).height() - $(window).height()){
    alert();
  });
});

You can use a flag to keep the current scroll or update it and then check the current position:

$(function () {
  cur = $(window).scrollTop();
  $(window).scroll(function() {
    if ($(window).scrollTop() < cur) {
      // Scrolled Up!
    } // Remove the extra `);` here.
  });
});

try this

$(window).scroll(function() {
 if ($(window).scrollTop() == $(document).height() - $(window).height()){
  alert();
 }  //Remove from here
});

Remove ) after the end of if block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM