一个简单的浏览器页面滚动防抖

    const scrollTimer =  ref(null) //滚动防抖定时器
    const currentTop =  ref(null) //实时滚动高度
    const scrollTop =  ref(0) //防抖结束后的滚动高度    

    function bindScrollEvent() {
      window.addEventListener('scroll', handleScrollStart);
    }

    function removeScrollEvent() {
      window.removeEventListener('scroll', handleScrollStart);
    }

    function handleScrollStart() {
      scrollTimer.value && clearTimeout(scrollTimer.value);
      scrollTimer.value = setTimeout(() => {
        handleScrollEnd();
      }, 200);
      currentTop.value = document.documentElement.scrollTop || document.body.scrollTop;
    }

    function handleScrollEnd() {
      scrollTop.value = document.documentElement.scrollTop || document.body.scrollTop;
      // 判断是否停止滚动的条件
      if (scrollTop.value == currentTop.value) {
        // do something
      }
    }

    
    onMounted(()=>{
      bindScrollEvent()
    })

    onBeforeUnmount(()=>{
      removeScrollEvent()
     })
posted @   huihuihero  阅读(1)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示