当前位置: 移动技术网 > IT编程>脚本编程>vue.js > vue2滚动条加载更多数据实现代码

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

2018年05月07日  | 移动技术网IT编程  | 我要评论

mmboxmyunicomcn,外挂作坊,试卷分析怎么写

解析:

判断滚动条到底部,需要用到dom的三个属性值,即scrolltop、clientheight、scrollheight。

scrolltop为滚动条在y轴上的滚动距离。

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

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

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

代码:

1.vue的实现

html:

<div class="questionlist-content-list">
   <ul>
    <li v-for="item in questionlistdata" @click="godetail(item.id)">
     {{item.create_time}}
     [{{item.level_value}}]
    {{item.description}}
     {{item.status_value}}
    </li>
   </ul>
  </div>

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',
      'status': '0',
      'status_value': '未解决',
      'level': '0203',
      'level_value': '高',
      'content': '秩序性支出v型从v',
      'userid': 'lxzx_hdsx',
      'create_time': 1480296155000,
      'images': null
     })
     self.questionlistdata.push({
      'id': 'b5c61d990f962570c34b8ee607ca1384',
      'type': '0104',
      'type_value': '页面问题',
      'description': '回复的文本框和字体有点丑',
      'status': '0',
      'status_value': '未解决',
      'level': '0203',
      'level_value': '高',
      'content': '回复的文本框和字体有点丑',
      'userid': 'lxzx_hdsx',
      'create_time': 1480064620000,
      'images': null
     })
     self.questionlistdata.push({
      'id': '279f9571cb8dc36f1dea5c8773f1793c',
      'type': '0103',
      'type_value': '设计问题',
      'description': '设计bug,不应该这样设计。',
      'status': '0',
      'status_value': '未解决',
      'level': '0204',
      'level_value': '非常高',
      'content': '设计bug,不应该这样设计。你看。',
      'userid': 'lxzx_hdsx',
      'create_time': 1480064114000,
      'images': null
     })
     self.questionlistdata.push({
      'id': '80e365710cb9157db24f08c8d2039473',
      'type': '0102',
      'type_value': '数据问题',
      'description': '数据列表滚动条问题',
      'status': '0',
      'status_value': '未解决',
      'level': '0202',
      'level_value': '中',
      'content': '数据列表在数据条数比较多的情况下无滚动条',
      'userid': 'lxzx_hdsx',
      'create_time': 1480034606000,
      'images': null
     })
     console.log(self.questionlistdata)
    }
   })
  },

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

2: 普通js的实现

html:

<div id="content" style="height:960px" class="questionlist-content-list"> 
  <ul> 
    <li class="list"> 
   <span测试1</span>
     <span>测试2</span>
     <span>测试3</span>
     <span>测试4</span>
     <span>测试5</span>
     <span>测试6</span>
     <span>测试7</span>
     <span>测试8</span>
     <span>测试9</span>
     <span>测试10</span>
     <span>测试11</span>
    </li> 
  </ul> 
</div>

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 += '<li>page: ' + i + ', data index: ' + i + ' </li>'
    }
    $('#content ul').append(html);
   }
  });

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网