防抖函数的定义是当事件触发完成之后再延迟触发,并且只触发一次;如果频繁滚动就会刷新定时器,这样就不会执行函数,如果停止后,一秒后才会继续执行
下边例子是滚动停止一秒后执行
ngOnInit(){ window.onscroll = e => { this.debounce(this.showTop, 1000) } } showTop() { var scrollTop = document.body.scrollTop || document.documentElement.scrollTop; console.log('滚动条位置:' + scrollTop); } debounce(fn, delay) { if (this.timer) { clearTimeout(this.timer) } this.timer = setTimeout(fn, delay) }
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/216654.html原文链接:https://javaforall.net
