<template>
<swiper
class="swipercard"
:circular="true"
:autoplay="false"
:current="swiperCurrentIndex"
@change="eventHandle"
:duration="300"
>
<block v-for="(item, index) in swiperList" :key="item.id">
<swiper-item class="swiperItem">
<view>{{item.name}}</view>
</swiper-item>
</block>
</swiper>
</template>
<script>
export default {
data() {
return {
questionList: [ { id: 1, name: 'Question 1' }, ... ], // Full dataset
swiperList: [], // The 3-item sliding window
questionListIndex: 0, // Index of current item in full dataset
swiperCurrentIndex: 0, // Current index within the 3-item swiper
};
},
onLoad() {
this.upDateSwiperList();
},
methods: {
eventHandle(e) {
let current = e.detail.current;
// Detect forward slide
if (this.swiperCurrentIndex - current == 2 || this.swiperCurrentIndex - current == -1) {
this.questionListIndex = (this.questionListIndex + 1 == this.questionList.length) ? 0 : this.questionListIndex + 1;
this.swiperCurrentIndex = (this.swiperCurrentIndex + 1 == 3) ? 0 : this.swiperCurrentIndex + 1;
this.upDateSwiperList();
}
// Detect backward slide
else if (this.swiperCurrentIndex - current == -2 || this.swiperCurrentIndex - current == 1) {
this.questionListIndex = (this.questionListIndex - 1 == -1) ? this.questionList.length - 1 : this.questionListIndex - 1;
this.swiperCurrentIndex = (this.swiperCurrentIndex - 1 == -1) ? 2 : this.swiperCurrentIndex - 1;
this.upDateSwiperList();
}
},
upDateSwiperList() {
let displayList = [];
// Current item
displayList[this.swiperCurrentIndex] = this.questionList[this.questionListIndex];
// Previous item
let prevIdx = this.swiperCurrentIndex - 1 == -1 ? 2 : this.swiperCurrentIndex - 1;
let prevDataIdx = this.questionListIndex - 1 == -1 ? this.questionList.length - 1 : this.questionListIndex - 1;
displayList[prevIdx] = this.questionList[prevDataIdx];
// Next item
let nextIdx = this.swiperCurrentIndex + 1 == 3 ? 0 : this.swiperCurrentIndex + 1;
let nextDataIdx = this.questionListIndex + 1 == this.questionList.length ? 0 : this.questionListIndex + 1;
displayList[nextIdx] = this.questionList[nextDataIdx];
this.swiperList = displayList;
}
}
};
</script>