前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >vue3实现元素监听滚动事件

vue3实现元素监听滚动事件

作者头像
蓓蕾心晴
发布2023-06-10 11:08:31
1.8K0
发布2023-06-10 11:08:31
举报
文章被收录于专栏:前端小叙前端小叙
代码语言:javascript
复制
// useScroll.js

import { debounce } from '@/common/util.js'

export default function useScroll(elRef) {
  console.log('########3useScroll', elRef.value)
  let el = window
  const isReachBottom = ref(false)
  const clientHeight = ref(0)
  const scrollTop = ref(0)
  const scrollHeight = ref(0)

  const scrollListenerHandler = debounce(() => {
    if (el === window) {
      clientHeight.value = document.documentElement.clientHeight || document.body.clientHeight
      scrollHeight.value = document.documentElement.scrollHeight || document.body.scrollHeight
      scrollTop.value = document.documentElement.scrollTop || document.body.scrollTop
    } else {
      clientHeight.value = el.clientHeight
      scrollTop.value = el.scrollTop
      scrollHeight.value = el.scrollHeight
    }
    if (clientHeight.value + scrollTop.value >= scrollHeight.value) {
      isReachBottom.value = true
    } else {
      isReachBottom.value = false
    }
    console.log('#########scrollTop', scrollTop.value)
    console.log('#########clientHeight', clientHeight.value)
    console.log('#########scrollHeight', scrollHeight.value)
    console.log('#########isReachBottom', isReachBottom.value)
  }, 100)
  onMounted(() => {
    if (elRef) {
      el = elRef.value
    }
    el.addEventListener('scroll', scrollListenerHandler)
  })
  onUnmounted(() => {
    el.removeEventListener('scroll', scrollListenerHandler)
  })

  return { isReachBottom, clientHeight, scrollTop, scrollHeight }
}

使用的时候,通过监听到达底部的变量改变,可以做想做的事情,比如加载更多,或跳转页面等。

代码语言:javascript
复制

注意:对于滚动事件,最好要使用防抖,防抖可以保证最后一次滚动事件始终是触发的,而节流是在一段时间内执行一次,最后一次不保证会触发,除非手动修改节流方法,来最后一次保证始终触发。

修改后的节流方法如下:

代码语言:javascript
复制
// 节流, 不需要 setTimeout,只需要判断上次执行和本次触发时间的时间差
            // setTimeout只是用来兜底的,防止最后一次没有执行
            function throttle(fn, delay) {
                let timer = null;
                let begin = 0;
                return function () {
                    if (timer) {
                        clearTimeout(timer);
                    }
                    let flag = false;
                    let cur = new Date().getTime();
                    if (cur - begin > delay) {
                        fn.apply(this, arguments);
                        begin = cur;
                        flag = true;
                    }
                    console.log("flag", flag);
                    if (!flag) {
                        timer = setTimeout(() => {
                            console.log("兜底");
                            fn.apply(this, arguments);
                        }, delay);
                    }
                };
            }

修改前:

代码语言:javascript
复制
// 节流
const throttle = (func, delay) => {
    let lastTime = 0;
    return function (...args) {
        let now = +new Date().getTime();
        if (now - lastTime >= delay) {
            func.apply(this, args)
            lastTime = now;
        }
    }
}

// 防抖
function debounce(func,delay) {
    let timer = null;
    return function(...args){
        clearTimeout(timer)
        timer = setTimeout(() => {
            func.apply(this,args)
        }, delay);
    }
} 
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-06-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档