简体   繁体   中英

How do you remember scrollTop if it went backwards?

I have a function and event for scroll.

var item = document.querySelector('.js_item');

item.addEventListener('scroll', scroll);

function scroll() {
  yScroll = item.scrollTop;
  console.log(yScroll);
}

How I can remember a position if scrolling goes backwards? For example in var saveScroll.

I mean, in the console, scrolled down. 1, 10, 20, 30 (where scroll stopped and went backwards) 20, 10, 1. How can I put in var saveScroll = 30?

Try this:

 var saveScroll;

 function scroll() {
      yScroll = item.scrollTop;
   // If the yScroll is greater than the last saveScroll value, it will replace the old saveScroll with the new value:
      saveScroll = yScroll > saveScroll ? yScroll : saveScroll;
      console.log(yScroll);
  }

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