網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
最近開(kāi)發(fā)的一個(gè)小程序中涉及一個(gè)答題頁(yè)面,打算使用小程序的swiper組件開(kāi)發(fā),題目可能數(shù)量過(guò)多,使用swiper一次性加載會(huì)造成卡頓,于是進(jìn)行了一些優(yōu)化
解決思路:頁(yè)面只展示3個(gè)swiper-item組件(小程序視頻輪播插件受到的啟發(fā)),每次輪播變化,都截取總數(shù)據(jù)里面的3條進(jìn)行展示
觀察規(guī)律:畫(huà)了個(gè)簡(jiǎn)單草圖,大家可以看下,每次輪播索引變化后,計(jì)算出當(dāng)前索引、前一個(gè)索引值、后一個(gè)索引值,以及分別對(duì)應(yīng)到數(shù)據(jù)列表上的索引和值
代碼實(shí)現(xiàn)
<template>
<view class="index-box">
<swiper class="swipe" :circular="circular" :current="swipeActiveIndex" @change="swipeChange">
<swiper-item v-for="(item, index) in swipeList" :key="index">
<view class="swipe-item">{{ item }}</view>
</swiper-item>
</swiper>
</view>
</template>
<script>
export default {
data() {
return {
circular: true, // 是否可以循環(huán)滾動(dòng)
swipeActiveIndex: 0, // 當(dāng)前輪播圖激活索引
currentIndex: 0, // 當(dāng)前展示數(shù)據(jù)在列表中的索引值
swipeLength: 3, // 總的輪播圖數(shù)量
dataList: [1, 2, 3, 4, 5, 6, 7] // 數(shù)據(jù)列表
}
},
computed: {
swipeList() {
// 獲取當(dāng)前值、下一個(gè)值、上一個(gè)值
let currentValue = this.dataList[this.currentIndex]
let nextValue = this.dataList[this.getDataIndex(this.currentIndex + 1)]
let prevValue = this.dataList[this.getDataIndex(this.currentIndex - 1)]
// 獲取當(dāng)前輪播索引對(duì)應(yīng)的值、下個(gè)索引對(duì)應(yīng)的值、上個(gè)索引對(duì)應(yīng)的值
let list = new Array(3)
list[this.swipeActiveIndex] = currentValue
list[this.getSwipeIndex(this.swipeActiveIndex + 1)] = nextValue
list[this.getSwipeIndex(this.swipeActiveIndex - 1)] = prevValue
return list
}
},
methods: {
swipeChange(event) {
let current = Number(event.detail.current)
if ([1, 1 - this.swipeLength].includes(current - this.swipeActiveIndex)) {
// 向左滑動(dòng)
this.currentIndex = this.getDataIndex(this.currentIndex + 1)
} else {
// 向右滑動(dòng)
this.currentIndex = this.getDataIndex(this.currentIndex - 1)
}
this.swipeActiveIndex = current
},
// 獲取當(dāng)前數(shù)據(jù)列表的索引
getDataIndex(index) {
if (index < 0) {
// 小于零,返回?cái)?shù)據(jù)列表末尾索引
return this.dataList.length - 1
} else if (index >= this.dataList.length) {
// 等于(或大于,一般不會(huì))數(shù)據(jù)列表長(zhǎng)度,返回?cái)?shù)據(jù)首位索引
return 0
} else {
return index
}
},
getSwipeIndex(index) {
if (index < 0) {
return this.swipeLength - 1
} else if (index >= this.swipeLength) {
return 0
} else {
return index
}
}
}
}
</script>
<style scoped lang="scss">
.index-box {
width: 100%;
.swipe {
width: 100%;
height: 300rpx;
&-item {
display: flex;
width: 100%;
height: 100%;
font-size: 80rpx;
color: #fff;
background: #222;
align-items: center;
justify-content: center;
}
}
}
</style>
原文鏈接:https://blog.csdn.net/qq_40026668/article/details/122236668
相關(guān)推薦
- 2022-10-23 Android性能優(yōu)化之ANR問(wèn)題定位分析_Android
- 2022-01-29 fastadmin uniapp跨域的問(wèn)題
- 2021-12-02 Android?Gson基本用法學(xué)習(xí)_Android
- 2021-12-02 C/C++?Qt數(shù)據(jù)庫(kù)SqlRelationalTable關(guān)聯(lián)表詳解_C 語(yǔ)言
- 2023-04-06 python?numpy.linalg.norm函數(shù)的使用及說(shuō)明_python
- 2022-10-05 教你使用RustDesk?搭建一個(gè)自己的遠(yuǎn)程桌面中繼服務(wù)器_服務(wù)器其它
- 2023-03-29 C語(yǔ)言交換奇偶位與offsetof宏的實(shí)現(xiàn)方法_C 語(yǔ)言
- 2023-02-01 C語(yǔ)言中聯(lián)合體與共用體和枚舉使用語(yǔ)法示例_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支