vue实现纯前端表格滚动分页加载

本文实例为大家分享了vue实现表格滚动分页加载的具体代码,供大家参考,具体内容如下

实现效果

实现过程

<div
    style="width: 100%; overflow: hidden; position: relative"
    id="container"
    ref="container"
    @mousewheel="handleScroll"
    :style="{ height: pageHeight + 'px' }">
  <!--    表格-->
  <div class="loading-bottom" v-show="visibleLoading">
      <a-spin :spinning="visibleLoading" style="margin-right: 10px"></a-spin>正在加载数据
    </div>
</div>
data() {
  return {
    visibleLoading: false,
  }
},
mounted() {
  //ref指向对应div,不建议对window全局监听,会影响子div的滚动
  this.$refs.container.addEventListener('scroll', this.handleScroll);
},
beforeUnmount() {
  this.$refs.container.removeEventListener('scroll', this.handleScroll);
},
methods:{
  //滚轮监听
  handleScroll() {
    let listAllHeight =
      document.documentElement.scrollTop ||
      document.body.scrollTop + document.documentElement.scrollHeight ||
      document.body.scrollHeight;
    let containerHeight = document.querySelector('#container').scrollHeight;
    //46 + 62 + 75是表格距离页面顶部的剩余距离,跟个人布局有关
    let fieldHeight = document.querySelector('#left-field').scrollHeight + 46 + 62 + 75;
    if (
      (fieldHeight >= containerHeight && this.pageHeight !== fieldHeight) ||
      (containerHeight > fieldHeight && this.pageHeight !== containerHeight)
    ) {
      this.visibleLoading = true;
    }

    setTimeout(() => {
      if (listAllHeight === this.pageHeight && this.pageHeight < containerHeight) {
        this.pageHeight = this.pageHeight + 750;
      }
      if (this.pageHeight > containerHeight && containerHeight > fieldHeight) {
        this.pageHeight = containerHeight;
      }
      if (this.pageHeight > fieldHeight && fieldHeight >= containerHeight) {
        this.pageHeight = fieldHeight;
      }
      this.visibleLoading = false;
    }, 500);
  },
}
.loading-bottom {
  position: absolute;
  left: 255px;
  bottom: 0;
  height: 30px;
  padding: 5px 0;
  background-color: #d3dae6;
  width: calc(100% - 270px);
  text-align: center;
  font-size: 14px;
  font-weight: 500;
  border-radius: 2px;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • vue中实现滚动加载更多的示例

    在以前的前端刀耕火种时代要实现滚动加载更多想要大家都是很快实现了,在vue会有一点麻烦,最近自己研究了一下,做了一个简单的demo,供大家参考: <template> <div> <ul> <li v-for="item in articles"> <h2>{{item.title}}</h2> <img :src="item.images" alt=""> &l

  • vue.js 表格分页ajax 异步加载数据

    Vue.js是一个轻巧.高性能.可组件化的MVVM库,同时拥有非常容易上手的API. 分页一般和表格一起用,分页链接作为表格的一部分,将分页链接封装成一个独立的组件,然后作为子组件嵌入到表格组件中,这样比较合理. 1.注册一个组件 js Vue.component('pagination',{ template:'#paginationTpl', replace:true, props:['cur','all','pageNum'], methods:{ //页码点击事件 btnClick: f

  • Vue.js 的移动端组件库mint-ui实现无限滚动加载更多的方法

    通过多次爬坑,发现了这些监听滚动来加载更多的组件的共同点, 因为这些加载更多的方法是绑定在需要加载更多的内容的元素上的, 所以是进入页面则直接触发一次,当监听到滚动事件之后,继续加载更多, 所以对于无限滚动加载不需要写首次载入列表的函数, 代码如下: html: //父组件 <div v-infinite-scroll="loadMore" infinite-scroll-disabled="loading" infinite-scroll-distance=

  • vue实现的上拉加载更多数据/分页功能示例

    本文实例讲述了vue实现的上拉加载更多数据/分页功能.分享给大家供大家参考,具体如下: 加载状态 <div v-if='has_log == 0'> <load-more tip="上拉加载" :show-loading="false" background-color="#fbf9fe"></load-more> </div> <div v-if='has_log == 1'> <

  • Vue 无限滚动加载指令实现方法

    也不存在什么加载咯, 就是一个判断滚动条是否到达浏览器底部了. 如果到了就触发事件,米到就不处理. 计算公式提简单的   底部等于(0) =  滚动条高度 - 滚动条顶部距离 - 可视高度.  反正结果就是0. 一.获取滚动条位置 class Scroll { static get top() { return Math.max(document.documentElement.scrollTop || document.body.scrollTop); } static get clientH

  • vue实现分页加载效果

    本文实例为大家分享了vue分页加载的具体代码,供大家参考,具体内容如下 HTML <!-- 商品 页数--> <div class="pagenavi"> <div class="all-number"> <span>共{{data.length}}个商品</span> </div> <p class="pageArea" data-countpage="1&

  • vue element-ui table表格滚动加载方法

    添加全局注册事件,用来监听滚动事件 window.Vue.directive('loadmore', { bind(el, binding) { const selectWrap = el.querySelector('.el-table__body-wrapper') selectWrap.addEventListener('scroll', function() { let sign = 100 const scrollDistance = this.scrollHeight - this.

  • Javascript vue.js表格分页,ajax异步加载数据

    分页一般和表格一起用,分页链接作为表格的一部分,将分页链接封装成一个独立的组件,然后作为子组件嵌入到表格组件中,这样比较合理. 效果: 代码: 1.注册一个组件 js Vue.component('pagination',{ template:'#paginationTpl', replace:true, props:['cur','all','pageNum'], methods:{ //页码点击事件 btnClick: function(index){ if(index != this.cu

  • Vue.js实现无限加载与分页功能开发

    本篇文章是一篇Vue.js的教程,目标在于用一种常见的业务场景--分页/无限加载,帮助读者更好的理解Vue.js中的一些设计思想.与许多Todo List类的入门教程相比,更全面的展示使用Vue.js完成一个需求的思考过程:与一些构建大型应用的高阶教程相比,又更专注于一些零碎细节的实现,方便读者快速掌握.致用. 需求分析 当一个页面中信息量过大时(例如一个新闻列表中有200条新闻需要展示),就会产生问题,例如: >数据量过大,影响加载速度 >用户体验差,很难定位到之前自己看过的某篇文章 >

  • Vue.js上下滚动加载组件的实例代码

    由于工作的需要并鉴于网上的vue.js滚动加载方案不合适,自己写了一个简单实用的.就短短的150行代码. 组件代码 // scrollLoader.vue // 滚动加载组件 <style scoped> .container-main {margin: 0 auto; overflow: auto; overflow-x: hidden; padding: 0;} .loading{ width: 100%; height: 40px; position: relative; overflo

随机推荐