翼度科技»论坛 编程开发 JavaScript 查看内容

vue判断内容是否滑动到底部的三种方式

12

主题

12

帖子

36

积分

新手上路

Rank: 1

积分
36
方式一:直接给滚动的部分加个 @scroll="handleScroll" 然后js里面进行业务处理
  1. <div class="tip-info" @scroll="handleScroll">
  2.     <div class="tip-blank" :key="outerIndex" v-for="(item, outerIndex) in htmlCaption">
  3. </div>
复制代码
  1. methods: {
  2.     // 滚动事件
  3.     handleScroll(event) {
  4.       const dialog = event.target;
  5.       if (dialog.scrollHeight - dialog.scrollTop === dialog.clientHeight) {
  6.         // 当内容滑动到底部时,执行想要的操作
  7.       }
  8.     }
  9. }
复制代码
方式二:可以采用给滚动内容,在最后一个内容的div后面追加一个新的元素,然后IntersectionObserver 进行观察
  1. <div class="tip-info">
  2.     <div class="tip-blank" :key="outerIndex" v-for="(item, outerIndex) in htmlCaption">
  3. </div>
复制代码
  1. mounted() {
  2.     this.addNewElementToTipBlank();
  3. },
  4. methods: {
  5.     addNewElementToTipBlank() {
  6.       // 创建新元素
  7.       const newElement = document.createElement('div');
  8.       newElement.className = 'tip-box';
  9.       newElement.textContent = 'New Tip Box Added';
  10.       // 找到 tip-blank 类所在的 div 元素
  11.       const tipBlankDivs = document.querySelectorAll('.tip-blank');
  12.       const lastTipBlankDiv = tipBlankDivs[tipBlankDivs.length - 1]; // 获取最后一个 tip-blank 元素
  13.       // 在最后一个 tip-blank 元素后面插入新的 div 元素
  14.       if (lastTipBlankDiv) {
  15.         lastTipBlankDiv.insertAdjacentElement('afterend', newElement);
  16.       }
  17.       // 创建一个观察者实例
  18.       const observer = new IntersectionObserver((entries) => {
  19.         console.log(entries);
  20.         entries.forEach((entry) => {
  21.           // entry.isIntersecting 判断目标元素是否在视口中
  22.           if (entry.isIntersecting) {
  23.             console.log('目标元素在视口中!');
  24.           }
  25.           else {
  26.             console.log('目标元素不在视口中.');
  27.           }
  28.         });
  29.       });
  30.       // 开始观察某个元素
  31.       const targetElement = document.querySelector('.tip-box');
  32.       if (targetElement) {
  33.         observer.observe(targetElement);
  34.       }
  35.       // 停止观察
  36.       // 如果你不再需要观察某个元素,你可以调用:
  37.       observer.unobserve(targetElement);
  38.       // 如果你想停止观察所有元素,你可以调用:
  39.       observer.disconnect();
  40.     },
  41. }
复制代码
IntersectionObserver具体的用法:
IntersectionObserver 是一个现代的浏览器 API,允许开发者在某个元素与其祖先元素或顶层文档视口发生交叉时得到通知。它非常适合实现图片懒加载、无限滚动、广告曝光率等功能。
1. 浏览器的兼容性
IntersectionObserver目前在大多数现代浏览器中都得到了支持。但是在一些老版本的浏览器,如 IE 中,则没有支持。点击查看 IntersectionObserver 的兼容性
2. 如何使用?
  1. const observer = new IntersectionObserver((entries, observer) => {
  2.     entries.forEach(entry => {
  3.         // entry.isIntersecting 判断目标元素是否在视口中
  4.         if (entry.isIntersecting) {
  5.             console.log('目标元素在视口中!');
  6.         } else {
  7.             console.log('目标元素不在视口中.');
  8.         }
  9.     });
  10. });
  11. // 开始观察某个元素
  12. const targetElement = document.querySelector('.some-class');
  13. observer.observe(targetElement);

  14. // 停止观察
  15. // 如果你不再需要观察某个元素,你可以调用:
  16. observer.unobserve(targetElement);
  17. // 如果你想停止观察所有元素,你可以调用:
  18. observer.disconnect();

  19. // 配置选项
  20. 当创建 IntersectionObserver 实例时,你可以提供一个配置对象,该对象有以下属性:
  21. const options = {
  22.     root: document.querySelector('.scroll-container'), // 观察目标的父元素,如果不设置,默认为浏览器视口
  23.     rootMargin: '10px', // 增加或减少观察目标的可见区域大小
  24.     threshold: [0, 0.25, 0.5, 0.75, 1] // 当观察目标的可见比例达到这些阈值时会触发回调函数
  25. };
  26. const observer = new IntersectionObserver(callback, options);
复制代码
3. 一些常见的应用场景
  1. // 图片懒加载
  2. const observer = new IntersectionObserver((entries) => {
  3.     entries.forEach(entry => {
  4.         if (entry.isIntersecting) {
  5.             const img = entry.target;
  6.             img.src = img.dataset.lazy;
  7.             observer.unobserve(img);
  8.         }
  9.     });
  10. });
  11. document.querySelectorAll('img[data-lazy]').forEach(img => {
  12.     observer.observe(img);
  13. });

  14. // 无线滚动加载
  15. const observer = new IntersectionObserver((entries) => {
  16.     entries.forEach(entry => {
  17.         if (entry.isIntersecting) {
  18.             loadMoreContent(); // 你的加载更多内容的函数
  19.             observer.unobserve(entry.target); // 如果你只想加载一次,你可以在这里停止观察
  20.         }
  21.     });
  22. });
  23. const loadMoreTrigger = document.querySelector('.load-more-trigger');
  24. observer.observe(loadMoreTrigger);
复制代码
方式三 如果前2种方式不可行,可试试这一种
  1. <template>
  2.     <div class="tip-info" @scroll.passive="handleScroll">
  3.         <div class="sn-f-c-c tip-blank" :key="i" v-for="(item, i) in caption">
  4.             {{item}}
  5.         </div>
  6.     </div>
  7. </template>

  8. <script>
  9.     data() {
  10.         return {
  11.             caption: []
  12.         };
  13.     },
  14.     methods: {
  15.         // 接口返回数据
  16.         interface() {
  17.             this.caption = ''; // 接口返回数据
  18.             if (this.caption.length > 0) {
  19.                 this.$nextTick(() => {
  20.                   this.handleScroll({
  21.                     target: document.querySelector('.tip-info')
  22.                   });
  23.                 });
  24.             }
  25.         },
  26.         handleScroll(e) {
  27.           const { scrollTop, clientHeight, scrollHeight } = e.target;
  28.             // 条件判断(scrollHeight - (scrollTop + clientHeight)) / clientHeight <= 0.05
  29.             // 是在计算滚动条距离底部的距离与可视区域高度的比例。如果这个比例小于或等于5%(0.05),则认为滚动条已经非常接近底部。
  30.             if ((scrollHeight - (scrollTop + clientHeight)) / clientHeight <= 0.05) {
  31.                 console.log('内容到底了');
  32.             }      
  33.         }  
  34.     }
  35. </script>
复制代码
以上就是vue判断内容是否滑动到底部的三种方式的详细内容,更多关于vue判断内容是否滑动到底部的资料请关注脚本之家其它相关文章!

来源:https://www.jb51.net/javascript/319049sh2.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具