用于解决长列表dom渲染溢出造成的空白无法显示问题。
思路:借助scroll-view的scroll事件的事件对象中的scrollTop的值判断当前用户可视的第一个元素的index,用v-if控制当前可视元素的前10个item和后10个item渲染,其他的不渲染,达到局部渲染的效果,从而解决dom溢出的问题。item的作用是保留高度,使滚动条保持正常位置。
前提条件是list里面的item的height要一样,才能计算出当前可视的元素的index。
示意图如下:
1)不分页情况
html结构如下:
<scroll-view scroll-y="true" @scroll="scroll" :style="'height: 800rpx;background-color: #f0f0f6;'">
<view class="list">
<view class="item" v-for="(item,childIndex) in 100" :key="childIndex">
<view class="hide" v-if="currentItem - 10 < childIndex+1 && currentItem + 10 > childIndex+1">
123
</view>