MaterialIndex.vue
9.42 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
<template>
<view class="material-box">
<template v-if="(moduleList && moduleList.length) || (imagelist && imagelist.length)">
<!-- 轮播容器 -->
<view class="swiper-carousel" @touchstart="starts" @touchend="ends">
<view v-for="(item, index) in imagelist" :key="index" :class="['carousel-block', item.class]"
@click.stop="handleClick(item)">
<image class="image-block" :src="item.imgUrl" mode="aspectFill" />
<view class="mask"></view>
</view>
</view>
<!-- 标题 -->
<view class="module-title flex">
{{ moduleTitle}}
</view>
<!-- 指示点 -->
<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="require('@/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 {
datalist: [],
imagelist: [],
// 定义位置样式:左、中、右
style_object: [
{ index: 0, class: 'block-left' },
{ index: 1, class: 'block-center' },
{ index: 2, class: 'block-right' }
],
ismove: 0,
autoplay: true,
interval: 4000,
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: {
getDatalist() {
this.datalist = this.moduleList
const list = JSON.parse(JSON.stringify(this.style_object))
const len = this.datalist.length
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)
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
},
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
const list = JSON.parse(JSON.stringify(this.style_object))
if (len === 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
b.class = aWasCenter ? 'block-center' : 'block-right'
b.isToor = b.class === 'block-center' ? 1 : 2
const centerItem = a.class === 'block-center' ? a : b
this.currentIndex = centerItem.isIndex
} else {
if (direction === 'left') {
this.currentIndex = (this.currentIndex + 1) % len
} else {
this.currentIndex = (this.currentIndex - 1 + len) % len
}
this.imagelist.forEach(item => {
// 计算新的位置索引 0(Left), 1(Center), 2(Right)
// Left: isToor --;
const toor = direction === 'left'
? (item.isToor - 1) < 0 ? 2 : item.isToor - 1
: (item.isToor + 1) > 2 ? 0 : item.isToor + 1
item.isToor = toor
item.class = list[toor].class
// 动态更新图片逻辑:
// 只有当卡片移动到"替补"位置时才更新内容,以保持动画连贯
// 左滑 (Next): Right(2) -> Center(1) -> Left(0) -> Right(2)...
// 原 Left(0) 变成 Right(2),它需要加载下下张图
if (direction === 'left' && item.class === 'block-right') {
item.isIndex = (this.currentIndex + 1) % len
item.imgUrl = datalist[item.isIndex].materialUrl
}
// 右滑 (Prev): Left(0) -> Center(1) -> Right(2) -> Left(0)...
// 原 Right(2) 变成 Left(0),它需要加载上上张图
if (direction === 'right' && item.class === 'block-left') {
item.isIndex = (this.currentIndex - 1 + len) % len
item.imgUrl = datalist[item.isIndex].materialUrl
}
})
}
},
starts(e) {
this.ismove = e.touches[0].pageX
this.playStatus = false
if (this.endTimer) {
clearTimeout(this.endTimer)
this.endTimer = null
}
},
ends(e) {
if (this.moduleList.length <= 1) return
const pageX = e.changedTouches[0].pageX
const nums = this.ismove - pageX
if (Math.abs(nums) < 30) return // 阈值防误触
if (nums > 30) this.updateCarouselPositions('left')
if (nums < -30) this.updateCarouselPositions('right')
this.endTimer = setTimeout(() => {
this.playStatus = true
this.autoPlayFunction()
}, this.interval)
},
handleClick(item) {
if (item.class !== 'block-center') {
// 点击两侧卡片也可以切换
if (item.class === 'block-right') this.updateCarouselPositions('left')
if (item.class === 'block-left') this.updateCarouselPositions('right')
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;
position: relative;
overflow: hidden;
}
.empty-box {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
color: #999;
font-size: toRpx(28);
.empty-img {
width: toRpx(160);
height: toRpx(160);
margin-bottom: toRpx(16);
}
}
.swiper-carousel {
width: 100%;
height: calc(100% - #{toRpx(90)});
position: relative;
margin-top: toRpx(10);
perspective: 2000rpx; /* 3D透视 */
}
.carousel-block {
position: absolute;
top: 50%;
width: 65%;
height: 96%;
border-radius: toRpx(32);
background: #fff;
overflow: hidden;
/* 使用带回弹的贝塞尔曲线 */
transition: all 0.6s cubic-bezier(0.34, 1.56, 0.64, 1);
transform-style: preserve-3d;
box-shadow: 0 toRpx(10) toRpx(30) rgba(0, 0, 0, 0.05);
.image-block {
width: 100%;
height: 100%;
object-fit: cover;
border-radius: toRpx(32);
display: block;
}
.mask {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.3);
opacity: 0;
transition: opacity 0.6s;
pointer-events: none;
}
}
/* 中心卡片 */
.block-center {
left: 50%;
transform: translate3d(-50%, -50%, 0) scale(1);
z-index: 20;
opacity: 1;
box-shadow: 0 toRpx(30) toRpx(60) rgba(44, 102, 247, 0.35); /* 品牌色投影 */
.mask {
opacity: 0;
}
}
/* 左侧卡片 */
.block-left {
left: 30%;
transform: translate3d(-50%, -50%, -100rpx) scale(0.85) rotateY(15deg);
z-index: 10;
opacity: 0.8;
.mask {
opacity: 1;
}
}
/* 右侧卡片 */
.block-right {
left: 70%;
transform: translate3d(-50%, -50%, -100rpx) scale(0.85) rotateY(-15deg);
z-index: 10;
opacity: 0.8;
.mask {
opacity: 1;
}
}
.module-title {
color: #333;
font-size: toRpx(28);
font-weight: 600;
margin-top: toRpx(10);
justify-content: center;
}
.module-dots {
position: absolute;
bottom: toRpx(20);
right: toRpx(98);
gap: toRpx(12);
.dot {
width: toRpx(12);
height: toRpx(12);
opacity: 1;
background: #0007400d;
border-radius: 100%;
}
.active {
background: #000740;
}
}
</style>