MaterialIndex.vue
12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<template>
<view class="material-box">
<template v-if="(moduleList && moduleList.length) || (imagelist && imagelist.length)">
<!-- 轮播容器:使用 movable-area 实现自定义滑动与自动播放 -->
<movable-area class="swiper-carousel" @touchstart="starts" @touchend="ends">
<!-- 展示位:根据 item.class 切换左/中/右位置,只有中间可拖拽/点击 -->
<movable-view v-for="(item, index) in imagelist" :key="index" :class="['carousel-block', item.class]"
direction="horizontal" :disabled="item.class != 'block-center' || moduleList.length == 1"
:x="item.class != 'block-center' ? 0 : viewX" @change="change" @click.stop="handleClick(item)">
<image class="image-block" :src="item.imgUrl" />
</movable-view>
</movable-area>
<!-- 标题:与当前中心卡片 materialName 联动 -->
<view class="module-title flex">
{{ moduleTitle +'1'}}
</view>
<!-- 指示点:与 currentIndex 同步高亮 -->
<view class="module-dots flex">
<view v-for="(i, index) in moduleList" :key="index" :class="['dot', { 'active': index == currentIndex }]">
</view>
</view>
</template>
<template v-else>
<view class="empty-box">
<image
class="empty-img"
src="@/assets/images/common/nothing.webp"
mode="aspectFit"
/>
<view class="empty-text">暂无数据</view>
</view>
</template>
</view>
</template>
<script>
export default {
// 自定义三卡片轮播组件:实现自动播放与手势滑动
name: 'MaterialIndex',
props: {
moduleList: {
type: Array,
default: () => []
}
},
data() {
return {
// 轮播数据快照(与 moduleList 同步)
datalist: [],
// 渲染用的三卡片列表(左/中/右)
imagelist: [],
// 三卡片位置与类名映射
style_object: [
{ index: 0, class: 'block-left' },
{ index: 1, class: 'block-center' },
{ index: 2, class: 'block-right' }
],
// 拖拽与位置控制
blockX: 0,
viewX: 0,
ismove: 0,
// 自动播放控制参数与状态
autoplay: true,
interval: 3000,
autoPlayTimer: null,
endTimer: null,
playStatus: true,
currentIndex: 0
}
},
computed: {
// 当前模块标题与中心卡片联动
moduleTitle() {
return this.moduleList[this.currentIndex]?.materialName || ''
}
},
watch: {
moduleList: {
handler(val) {
this.getDatalist()
},
deep: true
}
},
mounted() {
// 初始化轮播并开启自动播放
this.getDatalist()
if (this.autoplay) this.autoPlayFunction()
},
methods: {
// 初始化 imagelist(最多三项)并更新 currentIndex
getDatalist() {
this.datalist = this.moduleList
const list = JSON.parse(JSON.stringify(this.style_object))
const len = this.datalist.length
// 自动播放控制:仅当素材数量大于1时才开启
if (len > 1 && this.autoplay) {
this.autoPlayFunction()
} else {
if (this.autoPlayTimer) {
clearTimeout(this.autoPlayTimer)
this.autoPlayTimer = null
}
}
if (!len) {
this.imagelist = []
return
}
if (len === 1) {
this.imagelist = [
{ isToor: 1, class: 'block-center', imgUrl: this.datalist[0].materialUrl, isIndex: 0, index: 1 }
]
return
}
if (len === 2) {
// 两张素材:居中 + 右侧
this.imagelist = [
{ isToor: 1, class: 'block-center', imgUrl: this.datalist[0].materialUrl, isIndex: 0, index: 1 },
{ isToor: 2, class: 'block-right', imgUrl: this.datalist[1].materialUrl, isIndex: 1, index: 2 }
]
this.currentIndex = 0
return
}
const count = Math.min(3, len)
// 初始化:
// block-left: len-1 (Last)
// block-center: 0 (First)
// block-right: 1 (Second)
this.imagelist = Array.from({ length: count }).map((_, i) => {
let isIndex
if (i === 0) isIndex = len - 1
else if (i === 1) isIndex = 0
else isIndex = 1
const cfg = list[i]
return {
isToor: cfg.index,
class: cfg.class,
imgUrl: this.datalist[isIndex].materialUrl,
isIndex: isIndex,
index: cfg.index
}
})
const ci = this.imagelist.findIndex(item => item.index === 1)
if (ci !== -1) this.currentIndex = this.imagelist[ci].isIndex
},
// 自动播放控制:递归定时触发 updateCarouselPositions
autoPlayFunction() {
if (!this.autoplay || this.datalist.length <= 1) return
if (this.autoPlayTimer) clearTimeout(this.autoPlayTimer)
this.autoPlayTimer = setTimeout(() => {
if (!this.playStatus) {
clearTimeout(this.autoPlayTimer)
this.autoPlayTimer = null
return
}
this.updateCarouselPositions('left')
this.autoPlayFunction()
}, this.interval)
},
// 根据滑动方向更新三卡片位置与索引
updateCarouselPositions(direction = 'left') {
const { imagelist, datalist } = this
const len = datalist.length - 1
const list = JSON.parse(JSON.stringify(this.style_object))
if (datalist.length === 2) {
// 仅两张素材:在中心与右侧之间互换
const a = this.imagelist[0]
const b = this.imagelist[1]
const aWasCenter = a.class === 'block-center'
a.class = aWasCenter ? 'block-right' : 'block-center'
a.isToor = a.class === 'block-center' ? 1 : 2
a.index = a.isToor
a.x = 0
a.imgUrl = datalist[a.isIndex]?.materialUrl || ''
b.class = aWasCenter ? 'block-center' : 'block-right'
b.isToor = b.class === 'block-center' ? 1 : 2
b.index = b.isToor
b.x = 0
b.imgUrl = datalist[b.isIndex]?.materialUrl || ''
const ci2 = this.imagelist.findIndex(item => item.class === 'block-center')
if (ci2 !== -1) this.currentIndex = this.imagelist[ci2].isIndex
} else {
// 更新 currentIndex
if (direction === 'left') {
this.currentIndex = (this.currentIndex + 1) % len
} else {
this.currentIndex = (this.currentIndex - 1 + len) % len
}
this.imagelist.forEach(item => {
const toor = direction === 'left'
? (item.isToor - 1) < 0 ? imagelist.length - 1 : item.isToor - 1
: (item.isToor + 1) > imagelist.length - 1 ? 0 : item.isToor + 1
item.isToor = toor
item.class = list[toor].class
item.x = 0
// 根据新位置设置内容
// Center: currentIndex
// Left: (currentIndex - 1 + len) % len
// Right: (currentIndex + 1) % len
if (item.class === 'block-center') {
// 已经是正确的数据了,或者需要更新?
// 实际上如果是滑动过来的,它原本的数据就是我们想要的
// 比如 Left (prev) -> Center (current).
// 所以这里理论上不需要动 isIndex,除非我们需要“补”数据
// 但是为了确保数据绝对正确,我们可以强制赋值
// 注意:这样可能会导致图片闪烁,如果 DOM 复用而 src 变了。
// 只有当该卡片是从“不可见/替补”区域变过来时才需要更新 src。
// 在 3 卡片循环中,Left->Center, Center->Right, Right->Left (如果是左滑)
// Right 变成了 Left(替补位),此时它应该装载 (currentIndex - 1) 的数据吗?
// 如果是左滑(看下一张):
// Center(0) -> Left(0). Left(len-1) -> Right(1). Right(1) -> Center(1).
// 此时:
// 新 Left 是旧 Center(0)。期望是 (1-1)=0。正确。
// 新 Center 是旧 Right(1)。期望是 1。正确。
// 新 Right 是旧 Left(len-1)。期望是 2。
// 所以只有“跳跃”的那张卡需要更新。
}
let isIndex = item.isIndex
if (direction === 'left' && item.class === 'block-right') {
// 原 Left 变成了 Right,需要装载下下张图
isIndex = (this.currentIndex + 1) % len
}
if (direction === 'right' && item.class === 'block-left') {
// 原 Right 变成了 Left,需要装载上上张图
isIndex = (this.currentIndex - 1 + len) % len
}
item.isIndex = isIndex
item.imgUrl = datalist[isIndex].materialUrl || ''
item.index = list[toor].index
})
}
// 这里的 ci 计算其实是多余的,因为我们已经手动维护了 currentIndex
// const ci = this.imagelist.findIndex(item => item.index === 1)
// if (ci !== -1) this.currentIndex = this.imagelist[ci].isIndex
},
// 触摸开始:暂停自动播放、记录起点
starts(e) {
this.ismove = e.touches[0].pageX
this.playStatus = false
if (this.endTimer) {
clearTimeout(this.endTimer)
this.endTimer = null
return
}
},
// 触摸结束:判断滑动阈值,切换或回弹,并恢复自动播放
ends(e) {
if (this.moduleList.length === 1) return
const pageX = e.changedTouches[0].pageX
const nums = this.ismove - pageX
if (nums === 0) return
if (!(nums > 50 || this.blockX < -30) || !(nums < -50 || this.blockX > 30)) {
this.viewX = -10
setTimeout(() => { this.viewX = 0 }, 16)
}
if (nums > 50 || this.blockX < -30) this.updateCarouselPositions('left')
if (nums < -50 || this.blockX > 30) this.updateCarouselPositions('right')
this.endTimer = setTimeout(() => {
if (!this.playStatus) {
this.playStatus = true
this.autoPlayFunction()
}
}, this.interval)
},
// movable-view 位移变化回调,记录偏移值
change(e) {
const { x } = e.detail
this.blockX = x
},
// 点击中心卡片,向父组件抛出 itemClick
handleClick(item) {
console.log(item)
if (item.class !== 'block-center') return
const data = this.moduleList[item.isIndex]
this.$emit('itemClick', data, item.isIndex)
}
}
}
</script>
<style scoped lang="scss">
/* 布局容器 */
.material-box {
width: 100%;
height: 100%;
text-align: center;
}
.empty-box {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
color: #999999;
font-size: toRpx(28);
}
.empty-box .empty-img {
width: toRpx(160);
height: toRpx(160);
margin-bottom: toRpx(16);
}
.empty-box .empty-text {
line-height: toRpx(40);
}
/* 轮播外层:宽度 90%,上下留边距 */
.swiper-carousel {
width: 90%;
height: calc(90% - #{toRpx(96)});
position: relative;
overflow: hidden;
margin: toRpx(60) auto;
}
/* 三卡片基础样式 */
.carousel-block {
position: absolute;
overflow: hidden;
transition: all 0.2s ease;
border: toRpx(2) solid #ffffffb3;
box-shadow: 0 toRpx(3) toRpx(12) 0 #5294ff33;
border-radius: toRpx(24);
}
.image-block {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
object-fit: cover;
}
/* 三个展示位:左/中/右的定位容器 */
.block-left,
.block-right,
.block-center {
position: absolute;
}
.block-left {
left: 0%;
top: 10%;
width: 80%;
height: 80%;
z-index: 10;
opacity: 0.9;
}
.block-right {
left: 20%;
top: 10%;
width: 80%;
height: 80%;
z-index: 15;
opacity: 0.9;
}
.block-center {
left: 14%;
top: 2%;
width: 72%;
height: 96%;
z-index: 20;
}
/* 标题与指示点区域 */
.module-title {
color: #333333;
font-size: toRpx(24);
margin-top: toRpx(24);
justify-content: center;
position: relative;
}
/* 指示点:靠右下角排列 */
.module-dots {
position: absolute;
bottom: toRpx(116);
right: toRpx(98);
gap: toRpx(12);
.dot {
width: toRpx(12);
height: toRpx(12);
opacity: 1;
background: #0007400d;
border-radius: 100%;
}
.active {
background: #000740;
}
}
</style>