console
let body = document.body;
function showTop() {
var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
console.log("滚动条位置" + screenTop)
}
window.onscroll = throttle(showTop, 500)
function debounce(fn, delay) {
let timer = null
return function () {
if (timer) {
clearTimeout(timer)
timer = setTimeout(fn, delay)
} else {
timer = setTimeout(fn, delay)
}
}
}
function throttle(fn, delay) {
let flag = true;
return function () {
if (flag) {
setTimeout(function () {
fn()
flag = true;
}, delay)
}
flag = false
}
}
<div id="container">
</div>
#container{
height: 2000px;
width: 100px;
background-color: black;
}