zoukankan      html  css  js  c++  java
  • 如何在vue中监听scroll,从而实现滑动加载更多

    首先需要明确3个定义:

    文档高度:整个页面的高度

    可视窗口高度:你看到的浏览器可视屏幕高度

    滚动条滚动高度: 滚动条下滑过的高度

    当 文档高度 = 可视窗口高度 + 滚动条高度  时,滚动条正好到底。

    首先在mounted中添加监听:window.addEventListener('scroll', this.scrollFn);   // 监听scroll

    然后创建3个函数分别获取文档高度、可视窗口高度、滚动条高度:

    //文档高度

      getScrollTop(){
      var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
        if(document.body){
          bodyScrollTop = document.body.scrollTop;
        }
        if(document.documentElement){
          documentScrollTop = document.documentElement.scrollTop;
        }
        scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
        return scrollTop;
      }
     
      //可视窗口高度
     
      getWindowHeight(){
      var windowHeight = 0;
        if(document.compatMode == "CSS1Compat"){
          windowHeight = document.documentElement.clientHeight;
        }
        else{
          windowHeight = document.body.clientHeight;
        }
        return windowHeight;
      }
     
      //滚动条高度
     
      getScrollHeight(){
        var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
        if(document.body){
          bodyScrollHeight = document.body.scrollHeight;
        }  
        if(document.documentElement){
          documentScrollHeight = document.documentElement.scrollHeight;
        }
        scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
        return scrollHeight;
      }
     
      然后在scrollFn函数中判断:
     
      scrollFn(){
        if(this.getScrollTop() + this.getWindowHeight() == this.getScrollHeight()){
          这里执行动态获取数据的函数
        }
      }
     
      最后记得销毁监听:
      destroyed() {
        window.removeEventListener('scroll', this.scrollFn); // 销毁监听
      }
     
      如此即可实现滑动加载更多。
  • 相关阅读:
    ndt histogram_direction
    rplidar & hector slam without odometry
    点云的基本几何计算
    rplidar测试
    使用ZXing.Net生成与识别二维码(QR Code)
    C# 中使用 ThoughtWorks.QRCode.dll 生成指定尺寸和边框宽度的二维码
    .NET 二维码生成(ThoughtWorks.QRCode)
    zxing二维码的生成与解码(C#)
    netsh interface portproxy的一个简单例子
    Redis 客户端连接
  • 原文地址:https://www.cnblogs.com/cuiwan/p/11206319.html
Copyright © 2011-2022 走看看