html:
<div class="list" ref="scrollTopList"> <div class="listsmall" v-for="(item,index) of list" :key="index" @click="getDeviceInfo(item.id)"> <span class="state" :class="{'state1':item.status==1,'state0':item.status==0,'state2':item.status==2,'state3':item.status==3}"></span> <span class="text textcolor">【{{item.code||item.name}}】</span> <span class="text">{{item.name}}</span> </div> </div>
js:
先写滚动事件
handleScroll(){ let scrollTop = this.$refs.scrollTopList.scrollTop, clientHeight = this.$refs.scrollTopList.clientHeight, scrollHeight = this.$refs.scrollTopList.scrollHeight, height = 50; //根据项目实际定义 if(scrollTop +clientHeight >= scrollHeight - height){ if(this.pageSize > this.total){ return false }else{ this.pageSize = this.pageSize +10 //显示条数新增 this.getpageList() //请求列表list 接口方法 } }else{ return false } },
method中写节流函数
throttle(func, wait) { let lastTime = null let timeout return () => { let context = this; let now = new Date(); let arg = arguments; if (now - lastTime - wait > 0) { if (timeout) { clearTimeout(timeout) timeout = null } func.apply(context, arg) lastTime = now } else if (!timeout) { timeout = setTimeout(() => { func.apply(context, arg) }, wait) } } },
mounted中调用
mounted(){ this.$refs.scrollTopList.addEventListener("scroll",this.throttle(this.handleScroll,500),true) },
//-------------------------------------------------------------------------------------------第二种写法
html:
添加滚动事件
<div class="tablelist-box" @scroll="scrollEvent($event)"> <div class="tablelist" :class="{'active':listDevicesDetailIndex==index}" v-for="(item,index) of deviceList" :key="index" v-if="deviceList.length !== 0" @click="deviceDetail(item,index)" > <span class="tablelist-status"> <i :class="{zx:item.status==1,lx:item.status==2, wjh:item.status==0,gj:item.status==3}" ></i> </span> <span class="tablelist-bg">{{item.code != null ?item.code:"/"}}</span> </div> <div class="list-more" v-show="!deviceListIsFinish">{{deviceTip}}</div> <div class="list-more" v-show="deviceListIsFinish">{{deviceTip}}</div> </div>
css:
tablelist-box{
height: //根据实际项目取
overflow:auto //必须 不然判断有问题
}
css 定义
js
写入滚动事件
scrollEvent(e) { if (e instanceof Event) { let el = e.target; let scrollTop = el.scrollTop; // 获取可视区的高度 let clientHeight = el.clientHeight; // 获取滚动条的总高度 let scrollHeight = el.scrollHeight; let height = 50; //到底了 // console.log(this.deviceListIsLoad, this.deviceListIsFinish); // console.log(scrollTop, clientHeight, scrollHeight); //是否继续加载且已完成加载 if ( scrollTop + clientHeight >= scrollHeight && this.deviceListIsLoad && !this.deviceListIsFinish ) { // 把距离顶部的距离加上可视区域