網站首頁 編程語言 正文
最近開發的一個小程序中涉及一個答題頁面,打算使用小程序的swiper組件開發,題目可能數量過多,使用swiper一次性加載會造成卡頓,于是進行了一些優化
解決思路:頁面只展示3個swiper-item組件(小程序視頻輪播插件受到的啟發),每次輪播變化,都截取總數據里面的3條進行展示
觀察規律:畫了個簡單草圖,大家可以看下,每次輪播索引變化后,計算出當前索引、前一個索引值、后一個索引值,以及分別對應到數據列表上的索引和值
代碼實現
<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, // 是否可以循環滾動
swipeActiveIndex: 0, // 當前輪播圖激活索引
currentIndex: 0, // 當前展示數據在列表中的索引值
swipeLength: 3, // 總的輪播圖數量
dataList: [1, 2, 3, 4, 5, 6, 7] // 數據列表
}
},
computed: {
swipeList() {
// 獲取當前值、下一個值、上一個值
let currentValue = this.dataList[this.currentIndex]
let nextValue = this.dataList[this.getDataIndex(this.currentIndex + 1)]
let prevValue = this.dataList[this.getDataIndex(this.currentIndex - 1)]
// 獲取當前輪播索引對應的值、下個索引對應的值、上個索引對應的值
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)) {
// 向左滑動
this.currentIndex = this.getDataIndex(this.currentIndex + 1)
} else {
// 向右滑動
this.currentIndex = this.getDataIndex(this.currentIndex - 1)
}
this.swipeActiveIndex = current
},
// 獲取當前數據列表的索引
getDataIndex(index) {
if (index < 0) {
// 小于零,返回數據列表末尾索引
return this.dataList.length - 1
} else if (index >= this.dataList.length) {
// 等于(或大于,一般不會)數據列表長度,返回數據首位索引
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
相關推薦
- 2022-12-08 Matlab實現獲取文件夾下所有指定后綴的文件_C 語言
- 2022-04-25 ASP.NET?Core?MVC中Tag?Helpers用法介紹_實用技巧
- 2022-10-04 python?Dataframe?合并與去重詳情_python
- 2023-01-01 關于Python去除字符串中空格的方法總結_python
- 2023-02-23 Golang中的錯誤處理深入分析_Golang
- 2023-05-23 docker-compose統一管理多個容器使用詳解_docker
- 2022-06-23 Python+Turtle制作獨特的表白圖_python
- 2022-11-29 Redis五大常用數據結構-string、list、set、hash、zset(筆記)
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支