uniapp向上滚动虚拟列表加载更多无缝丝滑滚动如何实现?
网友回复

通过scroll监听和虚拟列表技术实现,完整代码如下:
<template>
<view class="virtual-list">
<scroll-view
class="scroll-view"
scroll-y
:scroll-top="scrollTop"
:style="{ height: height + 'px' }"
@scroll="onScroll"
:scroll-with-animation="false"
>
<view class="loading" v-if="isLoading">加载中...</view>
<view class="list-content" :style="{ height: totalHeight + 'px' }">
<view
v-for="item in visibleData"
:key="item.id"
class="list-item"
:style="{ transform: `translateY(${item.top}px)`, position: 'absolute', width: '100%' }"
>
<view :class="['message', item.isSelf ? 'self' : 'other']">
<view class="avatar">{{item.isSelf ? '我' : '对方'}}</view>
<view class="content">{{item.content}}</view>
</view>
</view>
</view>
</scroll-view>
</view>
</template>
<script>
export default {
data() {
return {
height: 0,
itemHeight: 80,
visibleCount: 0,
bufferCount: 5,
listData: [],
visibleData: [],
isLoading: false,
scrollTop: 0,
startIndex: 0,
loadMoreThreshold: 500,
lastLoadTime: 0,
loadCooldown: 1000,
totalHeight: 0,
lastScrollTop: 0,
scrollTimeout: null
}
},
async mounted() {
const sysInfo = uni.getSystemInfoSync()
this.height = sysInfo.windowHeight
this.visibleCount = Math.ceil(this.height / this.itemHeight) + this.bufferCount * 2
await this.initData()
},
methods: {
async initData() {
const initList = Array(30).fill(0).map((_, index) => ({
id: index,
content: `Message ${index}`,
isSelf: Math.random() > 0.5,
top: index * this.itemHeight
}))
this.listData = initList
this.totalHeight = this.listData.length * this.itemHeight
this.updateVisibleData()
},
updateVisibleData() {
...点击查看剩余70%
python能写一个检测nginx rewrite高危漏洞的工具代码?
css如何给video视频进行mask遮罩?
windows如何同时允许两个用户远程桌面连接同一个电脑?
nginx升级到1.30.1导致无法启动 [emerg] SSL_CTX_new() failed怎么办?
什么是ASLR(地址随机化)?
有没有不依赖embedding向量的RAG技术?
有没有支持实时打断语音通话并后台帮你执行任何的ai模型?
开源ai大模型文件格式GGUF、MLX、Safetensors、 ONNX 有什么区别?
出海挣钱支付收款PayPal、Wise 、PingPong、Stripe如何选择?
如何实现类似google的图片隐形水印添加和识别技术?


