如题,使用swiper实现抖音的效果。但是如果直接不停的往swiper里面添加swiper-item的话,数据过多会造成性能问题。所以,这里使用数据分页加载的方法进行数据替换,而不改变swiper-item的数量。
// wxml
<view>
<swiper
indicator-dots="{{false}}"
autoplay="{{ false }}"
vertical
bindchange="handleChange"
circular
current="{{swiperCur}}"
>
<block wx:for="{{ 10 }}" wx:key="index">
<swiper-item>
<view class="item">第{{ list[index + (curPage-1)*10] }}屏</view>
</swiper-item>
</block>
</swiper>
</view>
// wxss
swiper {
height: 100vh;
}
.item {
height: 100vh;
width: 100vw;
font-size: 22px;
font-weight: lighter;
text-align: center;
color: black;
line-height: 100vh;
letter-spacing: 12px;
}
// index.ts
// 获取应用实例
const list: number[] = []
Page({
data: {
list: list,
page: 1, // 请求数据的页码
displayNum: 1, // 当前显示第几个页面
lastCur: 0, // swpier上一个滑动的current
curPage: 1, // 当前显示的页面属于第几页,10个一页
swiperCur: 0 // swiper的current
},
onLoad () {
this.setList(this.data.page)
},
setList (page: number) {
let list = this.data.list
for(let i=1; i<=10; i++) {
list.push(i + (page - 1)*10)
}
console.log(list)
this.setData({
list: list
})
},
handleChange (event: any) {
const current = event.detail.current
// 禁止第一个视频往上滑
if (current == 9 && this.data.lastCur == 0 && this.data.curPage == 1) {
this.setData({
swiperCur: 0,
})
return
}
if (current + (this.data.curPage-1)*10 + 2 == this.data.list.length) {
this.data.page ++
this.setList(this.data.page)
}
if (current > this.data.lastCur) {
if (current == 9 && this.data.lastCur == 0) {
this.data.displayNum --
}else {
this.data.displayNum ++
}
}else {
if (current == 0 && this.data.lastCur == 9) {
this.data.displayNum ++
}else {
this.data.displayNum --
}
}
if (this.data.curPage == 0 || this.data.displayNum == 0) {
this.data.curPage = 1
this.data.displayNum = 1
}
this.setData({
curPage: Math.ceil(this.data.displayNum / 10),
swiperCur: current
})
this.data.lastCur = current
}
})
如果有什么bug话大家就看着改吧!!!
禁止第一个视频往上滑有点问题 current变了轮播图还是切换了