hagerCarousel.vue
9.19 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
<!--
* @Date: 2024-09-27 19:49:03
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2024-11-01 08:20:35
* @FilePath: /hager/src/components/hagerCarousel.vue
* @Description: 文件描述
-->
<template>
<div class="hager-carousel">
<div class="image-switcher">
<el-row :gutter="0">
<el-col :span="4">
<!-- 缩略图区域 -->
<div class="thumbnail-container">
<!-- 上箭头 -->
<div class="arrow-up" @click="prevImage"><i class="el-icon-arrow-up" style="font-size: 1.35rem;"></i></div>
<!-- 缩略图列表 -->
<div class="thumbnails">
<div
v-for="(image, index) in visibleThumbnails"
:key="index"
:class="['thumbnail', { active: selectedImage === (startIndex + index) }]"
@click="selectImage(startIndex + index)"
>
<img :src="image" alt="thumbnail">
</div>
</div>
<!-- 下箭头 -->
<div class="arrow-down" @click="nextImage"><i class="el-icon-arrow-down" style="font-size: 1.35rem;"></i></div>
</div>
</el-col>
<el-col :span="20">
<!-- 右边大图区域 -->
<div class="main-image" :class="{ 'fade-out': isTransitioning, 'fade-in': !isTransitioning }" :style="{ backgroundImage: `url(${images[selectedImage]})`}">
</div>
<!--<div class="ImageMagnifier">
<div @mouseover="handOver" @mousemove.stop="handMove" @mouseout="handOut">
<div ref="img" class="main-image" :class="{ 'fade-out': isTransitioning, 'fade-in': !isTransitioning }" :style="{ backgroundImage: `url(${images[selectedImage]})`}"></div>
<!~~ 放大效果 ~~>
<!~~ 遮罩层 ~~>
<div
v-show="showMask"
class="magnifier-box"
:style="{
width: magnifierConfigs.width + 'px',
height: magnifierConfigs.height + 'px',
opacity:magnifierConfigs.opacity,
top:magnifierConfigs.top+ 'px',
left:magnifierConfigs.left+ 'px',
}"
/>
<!~~ 放大图 ~~>
<div
v-show="showMask"
class="big-box"
:style="{
width: bigConfigs.width + 'px',
height: bigConfigs.height + 'px',
top:bigConfigs.top+'px',
left:bigConfigs.left+'px'
}"
>
<div
class="image"
:style="{
width: bigConfigs.width + 'px',
height: bigConfigs.height + 'px',
transform: bigImageConfigs.transformImage
}"
>
<img
:style="{
width: bigImageConfigs.width + 'px',
height:bigImageConfigs.height+'px'
}"
:src="images[selectedImage]"
alt="图片不见了"
>
</div>
</div>
</div>
</div>-->
</el-col>
</el-row>
</div>
</div>
</template>
<script>
import mixin from "common/mixin";
export default {
mixins: [mixin.init],
props: {
// 父组件传递的图片列表
images: {
type: Array,
default: () => [],
},
},
data() {
return {
selectedImage: 0, // 当前选中的图片索引
startIndex: 0, // 当前显示的缩略图起始索引
thumbnailsToShow: 3, // 每次显示4个缩略图
isTransitioning: false, // 控制大图的切换动画
// 配置
magnifierConfigs: { // 遮罩层
width: 80,
height: 80,
opacity: 0.3,
top: 0,
left: 0
},
bigConfigs: { // 放大的盒子
width: 200,
height: 200,
top: 0,
left: 0
},
bigImageConfigs: { // 放大图片
width: 500,
height: 400,
transformImage: 'translate(0px, 0px)'
},
showMask: false
};
},
mounted() {},
computed: {
// 计算当前显示的缩略图
visibleThumbnails() {
return this.images.slice(this.startIndex, this.startIndex + this.thumbnailsToShow);
},
endIndex() {
return this.startIndex + this.thumbnailsToShow - 1;
}
},
methods: {
// 选择某个图片并更新大图和选中状态
selectImage(index) {
this.isTransitioning = true; // 开始大图的淡出动画
setTimeout(() => {
this.selectedImage = index; // 切换大图
this.isTransitioning = false; // 动画结束
this.ensureVisibleThumbnail(); // 保证选中的缩略图在可视区域
}, 500); // 动画时间为500ms
},
// 控制缩略图上下滚动
scrollThumbnails(direction) {
if (direction === 'up' && this.startIndex > 0) {
this.startIndex--;
} else if (direction === 'down' && this.endIndex < this.images.length - 1) {
this.startIndex++;
}
},
// 确保选中的缩略图在可见范围内
ensureVisibleThumbnail() {
if (this.selectedImage < this.startIndex) {
// 如果选中的缩略图在当前显示范围的上方,则向上滚动
this.startIndex = this.selectedImage - this.thumbnailsToShow + 1;
} else if (this.selectedImage > this.endIndex) {
// 如果选中的缩略图在当前显示范围的下方,则向下滚动
this.startIndex = this.selectedImage - this.thumbnailsToShow + 3;
}
},
// 切换到下一张图片
nextImage() {
// if (this.selectedImage < this.images.length - 1) {
// this.selectImage(this.selectedImage + 1);
// }
if (this.startIndex < this.images.length - 3) {
this.startIndex += 3
this.selectImage(this.startIndex);
}
},
// 切换到上一张图片
prevImage() {
// if (this.selectedImage > 0) {
// this.selectImage(this.selectedImage - 1);
// }
if (this.startIndex > 0) {
this.startIndex -= 3;
this.selectImage(this.startIndex);
}
},
handOver() {
this.showMask = true
},
handMove(event) {
// this.showMask = true
const rect = this.$refs.img.getBoundingClientRect()
// 确定遮罩层坐标
const maskY = event.clientY - rect.top - this.magnifierConfigs.height / 2
this.magnifierConfigs.top = maskY
const maskX = event.clientX - rect.left - this.magnifierConfigs.width / 2
this.magnifierConfigs.left = maskX
// 确定放大图坐标
this.bigConfigs.top = event.clientY - rect.top - this.bigConfigs.height / 2
this.bigConfigs.left = event.clientX - rect.left + this.magnifierConfigs.width / 2
// 确定图像
this.bigImageConfigs.transformImage = `translate(-${maskX * 2}px,-${maskY * 2}px)`
},
handOut() {
this.showMask = false
}
},
};
</script>
<style lang="less" scoped>
.hager-carousel {
.image-switcher {
// display: flex;
// align-items: center;
}
.thumbnail-container {
display: flex;
flex-direction: column;
align-items: center;
margin-right: 20px;
}
.arrow-up,
.arrow-down {
cursor: pointer;
padding: 5px;
font-size: 18px;
user-select: none;
}
.thumbnails {
display: flex;
flex-direction: column;
align-items: center;
max-height: 190px; /* 限制缩略图容器的高度 */
overflow: hidden;
position: relative; /* 为缩略图滚动效果提供基础 */
}
.thumbnail {
width: 50px;
height: 50px;
margin-bottom: 10px;
cursor: pointer;
border: 2px solid transparent;
transition: border 0.3s;
}
.thumbnail img {
width: 100%;
height: 100%;
object-fit: cover;
transition: transform 0.5s ease-in-out; /* 缩略图切换时的动画效果 */
}
.thumbnail.active {
border: 2px solid @primary-color; /* 选中的缩略图边框样式 */
}
.main-image {
width: 100%;
height: 12rem;
position: relative;
transition: opacity 0.5s ease-in-out; /* 大图切换时的淡入淡出效果 */
opacity: 1;
background-position: center;
background-size: contain;
background-repeat: no-repeat;
}
.main-image img {
width: 80%;
height: 80%;
object-fit: cover;
position: absolute;
top: 20%;
}
.main-image.fade-out {
opacity: 0; /* 当切换时,将大图的透明度渐变为0 */
}
.main-image.fade-in {
opacity: 1; /* 切换后,图片渐渐显现 */
}
.thumbnails-wrapper {
transition: transform 0.5s ease-in-out; /* 缩略图滑动时的动画效果 */
display: flex;
flex-direction: column;
}
.ImageMagnifier{
position: relative;
.magnifier-box{
background-color: rgb(16, 101, 186);
position: absolute;
z-index: 999;
pointer-events: none; /* 让子元素在事件上透明 */
}
.big-box{
background-color: #fff;
// border: 1px solid black;
position: absolute;
overflow: hidden;
z-index: 999;
pointer-events: none; /* 让子元素在事件上透明 */
}
}
}
</style>