hagerCarousel.vue
5.54 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
<!--
* @Date: 2024-09-27 19:49:03
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2024-09-29 14:58:52
* @FilePath: /hager/src/components/hagerCarousel.vue
* @Description: 文件描述
-->
<template>
<div class="hager-carousel">
<div class="image-switcher">
<!-- 缩略图区域 -->
<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>
<!-- 右边大图区域 -->
<div class="main-image" :class="{ 'fade-out': isTransitioning, 'fade-in': !isTransitioning }">
<img :src="images[selectedImage]" alt="main image">
</div>
</div>
</div>
</template>
<script>
import mixin from "common/mixin";
export default {
mixins: [mixin.init],
data() {
return {
selectedImage: 0, // 当前选中的图片索引
startIndex: 0, // 当前显示的缩略图起始索引
thumbnailsToShow: 4, // 每次显示4个缩略图
isTransitioning: false, // 控制大图的切换动画
images: [
"https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg",
"https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg",
"https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg",
"https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg",
"https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg",
"https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg",
"https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg",
],
};
},
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;
} else if (this.selectedImage > this.endIndex) {
// 如果选中的缩略图在当前显示范围的下方,则向下滚动
this.startIndex = this.selectedImage - this.thumbnailsToShow + 1;
}
},
// 切换到下一张图片
nextImage() {
if (this.selectedImage < this.images.length - 1) {
this.selectImage(this.selectedImage + 1);
}
},
// 切换到上一张图片
prevImage() {
if (this.selectedImage > 0) {
this.selectImage(this.selectedImage - 1);
}
}
},
};
</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: 220px; /* 限制缩略图容器的高度 */
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 #00f; /* 选中的缩略图边框样式 */
}
.main-image {
width: 300px;
height: 300px;
position: relative;
transition: opacity 0.5s ease-in-out; /* 大图切换时的淡入淡出效果 */
opacity: 1;
}
.main-image img {
width: 100%;
height: 100%;
object-fit: cover;
position: absolute;
}
.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;
}
}
</style>