vue2滚动条加载更多数据实现代码

解析:

判断滚动条到底部,需要用到DOM的三个属性值,即scrollTop、clientHeight、scrollHeight。

scrollTop为滚动条在Y轴上的滚动距离。

clientHeight为内容可视区域的高度。

scrollHeight为内容可视区域的高度加上溢出(滚动)的距离。

从这个三个属性的介绍就可以看出来,滚动条到底部的条件即为scrollTop + clientHeight == scrollHeight。(兼容不同的浏览器)。

代码

1.vue的实现

html:

rush:js;">
  • js:

    rush:js;"> created () { var self = this $(window).scroll(function () { let scrollTop = $(this).scrollTop() let scrollHeight = $(document).height() let windowHeight = $(this).height() if (scrollTop + windowHeight === scrollHeight) { self.questionListData.push({ 'id': '62564AED8A4FA7CCDBFBD0F9A11C97A8','type': '0102','type_value': '数据问题','description': '撒的划分空间撒电话费看见爱上对方见客户速度快解放哈萨克接电话发生的划分空间是的哈副科级哈师大空间划分可接受的后方可抠脚大汉房间卡收到货放假多少','status': '0','status_value': '未解决','level': '0203','level_value': '高','content': '过好几个号','userid': 'lxzx_hdsx','create_time': 1480296174000,'images': null }) self.questionListData.push({ 'id': 'D679611152737E675984D7681BC94F16','type': '0101','type_value': '需求问题','description': 'a阿斯顿发生丰盛的范德萨范德萨发十多个非官方阿道夫葛根粉v跟下载v','content': '秩序性支出V型从v','create_time': 1480296155000,'images': null }) self.questionListData.push({ 'id': 'B5C61D990F962570C34B8EE607CA1384','type': '0104','type_value': '页面问题','description': '回复的文本框和字体有点丑','content': '回复的文本框和字体有点丑','create_time': 1480064620000,'images': null }) self.questionListData.push({ 'id': '279F9571CB8DC36F1DEA5C8773F1793C','type': '0103','type_value': '设计问题','description': '设计bug,不应该这样设计。','level': '0204','level_value': '非常高','content': '设计bug,不应该这样设计。你看。','create_time': 1480064114000,'images': null }) self.questionListData.push({ 'id': '80E365710CB9157DB24F08C8D2039473','description': '数据列表滚动条问题','level': '0202','level_value': '中','content': '数据列表在数据条数比较多的情况下无滚动条','create_time': 1480034606000,'images': null }) console.log(self.questionListData) } }) },

    因为vue2 实现了m-v双向绑定,所以这里直接改变for循环数据源即可实现列表的数据刷新;

    2: 普通js的实现

    html:

    rush:xhtml;">
        
  • 测试2 测试3 测试4 测试5 测试6 测试7 测试8 测试9 测试10 测试11     
  •   

    js:

    rush:js;"> var html = '' //距下边界长度/单位px $(window).scroll(function () { var scrollTop = $(this).scrollTop(); var scrollHeight = $(document).height(); var windowHeight = $(this).height(); if (scrollTop + windowHeight == scrollHeight) { for(let i=0;i<10;i++){ html += '
  • Page: ' + i + ',Data Index: ' + i + '
  • ' } $('#content ul').append(html); } });

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

    相关文章

    这篇文章我们将通过debug源码的方式来带你搞清楚defineAsync...
    欧阳老老实实的更新自己的高质量vue源码文章,还被某2.6k st...
    前言 在Vue3.5版本中响应式 Props 解构终于正式转正了,这个...
    组合式 (Composition) API 的一大特点是“非常灵活”,但也因...
    相信你最近应该看到了不少介绍Vue Vine的文章,这篇文章我们...
    前言 在欧阳的上一篇 这应该是全网最详细的Vue3.5版本解读文...