vue 判断内容是否滑动到底部

本文介绍了三种处理滚动事件的方法:直接使用handleScroll监听滚动,利用IntersectionObserver观察内容是否进入视口,以及在内容末尾动态添加元素并监控。详细讲解了每种方法的实现和适用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方式一:直接给滚动的部分加个 @scroll="handleScroll" 然后js里面进行业务处理

<div class="tip-info" @scroll="handleScroll">
    <div class="tip-blank" :key="outerIndex" v-for="(item, outerIndex) in htmlCaption">
</div>
methods: {
    // 滚动事件
    handleScroll(event) {
      const dialog = event.target;
      if (dialog.scrollHeight - dialog.scrollTop === dialog.clientHeight) {
        // 当内容滑动到底部时,执行想要的操作
      }
    }
}

方式二:可以采用给滚动内容,在最后一个内容的div后面追加一个新的元素,然后IntersectionObserver 进行观察

<div class="tip-info">
    <div class="tip-blank" :key="outerIndex" v-for="(item, outerIndex) in htmlCaption">
</div>
mounted() {
    this.addNewElementToTipBlank();
},
 methods: {
    addNewElementToTipBlank() {
      // 创建新元素
      const newElement = document.createElement('div');
      newElement.className = 'tip-box';
      newElement.textContent = 'New Tip Box Added';
      // 找到 tip-blank 类所在的 div 元素
      const tipBlankDivs = document.querySelectorAll('.tip-blank');
      const lastTipBlankDiv = tipBlankDivs[tipBlankDivs.length - 1]; // 获取最后一个 tip-blank 元素
      // 在最后一个 tip-blank 元素后面插入新的 div 元素
      if (lastTipBlankDiv) {
        lastTipBlankDiv.insertAdjacentElement('afterend', newElement);
      }
      // 创建一个观察者实例
      const observer = new IntersectionObserver((entries) => {
        console.log(entries);
        entries.forEach((entry) => {
          // entry.isIntersecting 判断目标元素是否在视口中
          if (entry.isIntersecting) {
            console.log('目标元素在视口中!');
          }
          else {
            console.log('目标元素不在视口中.');
          }
        });
      });
      // 开始观察某个元素
      const targetElement = document.querySelector('.tip-box');
      if (targetElement) {
        observer.observe(targetElement);
      }
      // 停止观察
      // 如果你不再需要观察某个元素,你可以调用:
      observer.unobserve(targetElement);
      // 如果你想停止观察所有元素,你可以调用:
      observer.disconnect();
    },
}

IntersectionObserver具体的用法可以参考 https://www.cnblogs.com/anans/p/17697522.html

方式三 如果前2种方式不可行,可试试这一种

<template>
    <div class="tip-info" @scroll.passive="handleScroll">
        <div class="sn-f-c-c tip-blank" :key="i" v-for="(item, i) in caption">
            {{item}}
        </div>
    </div>
</template>

<script>
    data() {
        return {
            caption: []
        };
    },
    methods: {
        // 接口返回数据
        interface() {
            this.caption = ''; // 接口返回数据
            if (this.caption.length > 0) {
                this.$nextTick(() => {
                  this.handleScroll({
                    target: document.querySelector('.tip-info')
                  });
                });
            }
        },
        handleScroll(e) {
          const { scrollTop, clientHeight, scrollHeight } = e.target;
            // 条件判断(scrollHeight - (scrollTop + clientHeight)) / clientHeight <= 0.05
            // 是在计算滚动条距离底部的距离与可视区域高度的比例。如果这个比例小于或等于5%(0.05),则认为滚动条已经非常接近底部。
            if ((scrollHeight - (scrollTop + clientHeight)) / clientHeight <= 0.05) {
                console.log('内容到底了');
            }       
        }  
    }
</script>

Vue判断滚动条是否到达底部,可以通过监听元素的滚动事件,并检查滚动条的位置是否达到了内容底部。以下是一个简单的实现方法: 1. 首先,在Vue组件中添加一个ref引用到需要监听的元素。 2. 然后,在mounted生命周期钩子中添加一个滚动事件监听器。 3. 在事件处理函数中,检查滚动条的位置。 以下是一个示例代码: ```vue <template> <div ref="scrollContainer" class="scroll-container"> <!-- 内容 --> </div> </template> <script> export default { name: 'ScrollDetector', mounted() { this.$refs.scrollContainer.addEventListener('scroll', this.handleScroll); }, methods: { handleScroll() { const container = this.$refs.scrollContainer; if (container.scrollTop + container.clientHeight >= container.scrollHeight) { // 滚动条到达底部 console.log('滚动条到达底部'); // 在这里可以添加触底后的逻辑 } } }, beforeUnmount() { this.$refs.scrollContainer.removeEventListener('scroll', this.handleScroll); } } </script> <style> .scroll-container { max-height: 400px; overflow-y: auto; } </style> ``` 在这个示例中: 1. 我们使用`ref="scrollContainer"`来引用需要监听的容器元素。 2. 在`mounted`生命周期钩子中,我们添加了一个滚动事件监听器。 3. `handleScroll`方法中,我们检查`scrollTop + clientHeight`是否大于或等于`scrollHeight`。如果是,则表示滚动条已经到达底部。 4. 在`beforeUnmount`生命周期钩子中,我们移除了滚动事件监听器以防止内存泄漏。 通过这种方式,我们可以在Vue中轻松地检测滚动条是否到达底部,并在需要时执行相应的操作。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

圆音

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值