Skip to content

Instantly share code, notes, and snippets.

@shangdev
Created September 7, 2019 15:56
Show Gist options
  • Save shangdev/34bc396821e352af0367db608061d981 to your computer and use it in GitHub Desktop.
Save shangdev/34bc396821e352af0367db608061d981 to your computer and use it in GitHub Desktop.
Detect scrolling event.
let overflowX = "auto";
let overflowY = "auto";
let scrollLeft = 0;
let scrollTop = 0;
let isScrolling;
const handleScroll = e => {
const element = e.target;
window.clearTimeout(isScrolling);
isScrolling = window.setTimeout(() => {
overflowX = "auto";
overflowY = "auto";
console.log("滚动停止");
}, 250);
if (scrollLeft !== element.scrollLeft) {
scrollLeft = element.scrollLeft;
overflowY = "hidden";
console.log(1);
console.log("水平滚动:" + scrollLeft);
}
if (scrollTop !== element.scrollTop) {
scrollTop = element.scrollTop;
overflowX = "hidden";
console.log(2);
console.log("垂直滚动:" + scrollTop);
}
};
@shangdev
Copy link
Author

shangdev commented Sep 7, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment