[英]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.例如在 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? 1, 10, 20, 30(滚动停止并后退) 20, 10, 1. 我怎样才能输入 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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.