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%
如何让ai帮我自动在小红书或抖音上自动根据需求截流与潜在客户聊天拉客?
如果用go编写一个在virtualbox中启动的简单操作系统?
go如何搭建一个零信任网络?
如何用python实现一个公网代理访问软件?
如何用go实现一个公网代理访问软件?
如何用python实现一个内网穿透打洞程序,实现内网的80端口暴露到公网上可以访问?
如何用go实现一个内网穿透打洞程序,实现内网的80端口暴露到公网上可以访问?
何为Shadowsocks 代理?
python如何实现类似php的opendir目录相互隔离的fastcgi多租户虚拟空间?
nodejs如何实现类似php的opendir目录相互隔离的fastcgi多租户虚拟空间?


