首页 > web前端 > js教程 > 在vue2中如何实现上拉加载功能

在vue2中如何实现上拉加载功能

亚连
发布: 2018-06-23 18:02:45
原创
2359 人浏览过

这篇文章主要为大家详细介绍了基于vue2实现上拉加载功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了vue2实现上拉加载展示的具体代码,供大家参考,具体内容如下

因为我们项目中,还用了swiper。很多都是滑动切换的,但是又得上拉加载,所以导致,很多UI框架,我们用了,都有不同的bug出现,没办法,最后写了一个。代码如下(这个因为很多地方会用,所以建议放在components/common下面):

<template>
  <p class="loadmore">
    <slot></slot>
    <slot name="bottom">
    </slot>
  </p>
</template>

<style>
  .loadmore{
    width:100%;
  }
</style>

<script>
  export default {
    name: &#39;loadmore&#39;,
    props: {
      maxDistance: {
        type: Number,
        default: 0
      },
      autoFill: {
        type: Boolean,
        default: true
      },
      distanceIndex: {
        type: Number,
        default: 2
      },
      bottomPullText: {
        type: String,
        default: &#39;上拉刷新&#39;
      },
      bottomDropText: {
        type: String,
        default: &#39;释放更新&#39;
      },
      bottomLoadingText: {
        type: String,
        default: &#39;加载中...&#39;
      },
      bottomDistance: {
        type: Number,
        default: 70
      },
      bottomMethod: {
        type: Function
      },
      bottomAllLoaded: {
        type: Boolean,
        default: false
      },
    },
    data() {
      return {
        // 最下面出现的p的位移
        translate: 0,
        // 选择滚动事件的监听对象
        scrollEventTarget: null,
        containerFilled: false,
        bottomText: &#39;&#39;,
        // class类名
        bottomDropped: false,
        // 获取监听滚动元素的scrollTop
        bottomReached: false,
        // 滑动的方向  down---向下互动;up---向上滑动
        direction: &#39;&#39;,
        startY: 0,
        startScrollTop: 0,
        // 实时的clientY位置
        currentY: 0,
        topStatus: &#39;&#39;,
        // 上拉加载的状态  &#39;&#39;   pull: 上拉中
        bottomStatus: &#39;&#39;,
      };
    },
    watch: {
      // 改变当前加载在状态
      bottomStatus(val) {
        this.$emit(&#39;bottom-status-change&#39;, val);
        switch (val) {
          case &#39;pull&#39;:
            this.bottomText = this.bottomPullText;
            break;
          case &#39;drop&#39;:
            this.bottomText = this.bottomDropText;
            break;
          case &#39;loading&#39;:
            this.bottomText = this.bottomLoadingText;
            break;
        }
      }
    },
    methods: {
      onBottomLoaded() {
        this.bottomStatus = &#39;pull&#39;;
        this.bottomDropped = false;
        this.$nextTick(() => {
          if (this.scrollEventTarget === window) {
          document.body.scrollTop += 50;
        } else {
          this.scrollEventTarget.scrollTop += 50;
        }
        this.translate = 0;
      });
        // 注释
        if (!this.bottomAllLoaded && !this.containerFilled) {
          this.fillContainer();
        }
      },

      getScrollEventTarget(element) {
        let currentNode = element;
        while (currentNode && currentNode.tagName !== &#39;HTML&#39; &&
        currentNode.tagName !== &#39;BODY&#39; && currentNode.nodeType === 1) {
          let overflowY = document.defaultView.getComputedStyle(currentNode).overflowY;
          if (overflowY === &#39;scroll&#39; || overflowY === &#39;auto&#39;) {
            return currentNode;
          }
          currentNode = currentNode.parentNode;
        }
        return window;
      },
      // 获取scrollTop
      getScrollTop(element) {
        if (element === window) {
          return Math.max(window.pageYOffset || 0, document.documentElement.scrollTop);
        } else {
          return element.scrollTop;
        }
      },
      bindTouchEvents() {
        this.$el.addEventListener(&#39;touchstart&#39;, this.handleTouchStart);
        this.$el.addEventListener(&#39;touchmove&#39;, this.handleTouchMove);
        this.$el.addEventListener(&#39;touchend&#39;, this.handleTouchEnd);
      },
      init() {
        this.bottomStatus = &#39;pull&#39;;
        // 选择滚动事件的监听对象
        this.scrollEventTarget = this.getScrollEventTarget(this.$el);
        if (typeof this.bottomMethod === &#39;function&#39;) {
          // autoFill 属性的实现  注释
          this.fillContainer();
          // 绑定滑动事件
          this.bindTouchEvents();
        }
      },
      // autoFill 属性的实现  注释
      fillContainer() {
        if (this.autoFill) {
          this.$nextTick(() => {
            if (this.scrollEventTarget === window) {
            this.containerFilled = this.$el.getBoundingClientRect().bottom >=
                document.documentElement.getBoundingClientRect().bottom;
          } else {
            this.containerFilled = this.$el.getBoundingClientRect().bottom >=
                this.scrollEventTarget.getBoundingClientRect().bottom;
          }
          if (!this.containerFilled) {
            this.bottomStatus = &#39;loading&#39;;
            this.bottomMethod();
          }
        });
        }
      },
      // 获取监听滚动元素的scrollTop
      checkBottomReached() {
        if (this.scrollEventTarget === window) {
          return document.body.scrollTop + document.documentElement.clientHeight >= document.body.scrollHeight;
        } else {
          // getBoundingClientRect用于获得页面中某个元素的左,上,右和下分别相对浏览器视窗的位置。 right是指元素右边界距窗口最左边的距离,bottom是指元素下边界距窗口最上面的距离。
          return this.$el.getBoundingClientRect().bottom <= this.scrollEventTarget.getBoundingClientRect().bottom + 1;
        }
      },
      // ontouchstart 事件
      handleTouchStart(event) {
        // 获取起点的y坐标
        this.startY = event.touches[0].clientY;
        this.startScrollTop = this.getScrollTop(this.scrollEventTarget);
        this.bottomReached = false;
        if (this.bottomStatus !== &#39;loading&#39;) {
          this.bottomStatus = &#39;pull&#39;;
          this.bottomDropped = false;
        }
      },
      // ontouchmove事件
      handleTouchMove(event) {
        if (this.startY < this.$el.getBoundingClientRect().top && this.startY > this.$el.getBoundingClientRect().bottom) {
          // 没有在需要滚动的范围内滚动,不再监听scroll
          return;
        }
        // 实时的clientY位置
        this.currentY = event.touches[0].clientY;
        // distance 移动位置和开始位置的差值    distanceIndex---
        let distance = (this.currentY - this.startY) / this.distanceIndex;
        // 根据 distance 判断滑动的方向 并赋予变量  direction down---向下互动;up---向上滑动
        this.direction = distance > 0 ? &#39;down&#39; : &#39;up&#39;;
        if (this.direction === &#39;up&#39;) {
          // 获取监听滚动元素的scrollTop
          this.bottomReached = this.bottomReached || this.checkBottomReached();
        }
        if (typeof this.bottomMethod === &#39;function&#39; && this.direction === &#39;up&#39; &&
            this.bottomReached && this.bottomStatus !== &#39;loading&#39; && !this.bottomAllLoaded) {
          // 有加载函数,是向上拉,有滚动距离,不是正在加载ajax,没有加载到最后一页
          event.preventDefault();
          event.stopPropagation();
          if (this.maxDistance > 0) {
            this.translate = Math.abs(distance) <= this.maxDistance
                ? this.getScrollTop(this.scrollEventTarget) - this.startScrollTop + distance : this.translate;
          } else {
            this.translate = this.getScrollTop(this.scrollEventTarget) - this.startScrollTop + distance;
          }
          if (this.translate > 0) {
            this.translate = 0;
          }
          this.bottomStatus = -this.translate >= this.bottomDistance ? &#39;drop&#39; : &#39;pull&#39;;
        }
      },
      // ontouchend事件
      handleTouchEnd() {
        if (this.direction === &#39;up&#39; && this.bottomReached && this.translate < 0) {
          this.bottomDropped = true;
          this.bottomReached = false;
          if (this.bottomStatus === &#39;drop&#39;) {
            this.translate = &#39;-50&#39;;
            this.bottomStatus = &#39;loading&#39;;
            this.bottomMethod();
          } else {
            this.translate = &#39;0&#39;;
            this.bottomStatus = &#39;pull&#39;;
          }
        }
        this.direction = &#39;&#39;;
      }
    },
    mounted() {
      this.init();
    }
  };
</script>
登录后复制

然后哪个页面需要,在哪个页面导入即可:import LoadMore from './../common/loadmore.vue';在需要引入他的页面写法如下:

<template>
 <section class="finan">
  <!-- 上拉加载更多 -->
  <load-more
  :bottom-method="loadBottom"
  :bottom-all-loaded="allLoaded"
  :bottomPullText=&#39;bottomText&#39;
  :auto-fill="false"
  @bottom-status-change="handleBottomChange"
  ref="loadmore">
    <p>
  这里写你需要的另外的模块
    </p>
    <p v-show="loading" slot="bottom" class="loading"> 这个p是为让上拉加载的时候显示一张加载的gif图
     <img src="./../../assets/main/uploading.gif">
    </p>
  </load-more>
 </section>
</template>
登录后复制

然后在此页面的data里和methods设置如下:

  export default {
    name: &#39;FinancialGroup&#39;,
    props:{
 
    },
    data () {
      return {
        // 上拉加载数据
        scrollHeight: 0,
        scrollTop: 0,
        containerHeight: 0,
        loading: false,
        allLoaded: false,
        bottomText: &#39;上拉加载更多...&#39;,
        bottomStatus: &#39;&#39;,
        pageNo: 1,
        totalCount: &#39;&#39;,
      }
    },
    methods: {
    /* 下拉加载 */
    _scroll: function(ev) {
      ev = ev || event;
      this.scrollHeight = this.$refs.innerScroll.scrollHeight;
      this.scrollTop = this.$refs.innerScroll.scrollTop;
      this.containerHeight = this.$refs.innerScroll.offsetHeight;
    },
    loadBottom: function() {
      this.loading = true;
      this.pageNo += 1;  // 每次更迭加载的页数
      if (this.pageNo == this.totalGetCount) {
        // 当allLoaded = true时上拉加载停止
        this.loading = false;
        this.allLoaded = true;
      }
      api.commonApi(后台接口,请求参数) 这个api是封装的axios有不懂的可以看vue2+vuex+axios那篇文章
          .then(res => {
        setTimeout(() => {
      要使用的后台返回的数据写在setTimeout里面
         this.$nextTick(() => {
          this.loading = false;
        })
      }, 1000)
     });
    },
    handleBottomChange(status) {
      this.bottomStatus = status;
    },
  }
登录后复制

上面是我整理给大家的,希望今后会对大家有帮助。

相关文章:

使用JavaScript如何实现寄生组合式继承

在JS中如何实现非首屏图片延迟加载

在jQuery中有关于库的引用方法有哪些

在js中如何生成word图片

以上是在vue2中如何实现上拉加载功能的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板