hagerHCarousel.vue
4.27 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
<!--
* @Date: 2024-09-29 16:27:30
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2024-10-16 17:24:19
* @FilePath: /hager/src/components/hagerHCarousel.vue
* @Description: 文件描述
-->
<template>
<div class="hager-h-carousel">
<div class="carousel-container">
<div v-if="items.length > 4" @click="prevItem" class="arrow left-arrow"><i class="el-icon-arrow-left"></i></div>
<div class="carousel">
<div class="carousel-track" :style="trackStyle">
<div v-for="item in items" :key="item.id" class="carousel-item">
<div class="product-item">
<el-image style="width: 14rem; height: 14rem;" :src="item.image" fit="fit"></el-image>
</div>
<h3>{{ item.name }}</h3>
<div class="product-desc">
<p>{{ item.description }}</p>
<p>{{ item.description }}</p>
<p>{{ item.description }}</p>
<p>{{ item.description }}</p>
<p>{{ item.description }}</p>
</div>
</div>
</div>
</div>
<div v-if="items.length > 4" @click="nextItem" class="arrow right-arrow"><i class="el-icon-arrow-right"></i></div>
</div>
</div>
</template>
<script>
import mixin from 'common/mixin';
export default {
mixins: [mixin.init],
data () {
return {
items: [{
id: 1,
image: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
name: 'hW空气材路幸',
description: '额定电流范围:630-5000A'
}, {
id: 2,
image: 'https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg',
name: 'hW空气材路幸',
description: '额定电流范围:630-5000A'
}, {
id: 3,
image: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
name: 'hW空气材路幸',
description: '额定电流范围:630-5000A'
}, {
id: 4,
image: 'https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg',
name: 'hW空气材路幸',
description: 'This is the description for Item 4.'
}, {
id: 5,
image: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
name: 'hW空气材路幸',
description: 'This is the description for Item 5.'
}, {
id: 6,
image: 'https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg',
name: 'Item 6',
description: 'This is the description for Item 6.'
}],
currentIndex: 0,
}
},
mounted () {
},
computed: {
trackStyle() {
return {
transform: `translateX(-${this.currentIndex * 25}%)`
};
}
},
methods: {
nextItem() {
if (this.currentIndex < this.items.length - 4) {
this.currentIndex++;
} else {
this.currentIndex = 0; // Loop back to start
}
},
prevItem() {
if (this.currentIndex > 0) {
this.currentIndex--;
} else {
this.currentIndex = this.items.length - 4; // Loop to end
}
}
}
}
</script>
<style lang="less">
.hager-h-carousel {
.carousel-container {
position: relative;
width: 100%;
// max-width: 1200px;
margin: 0 auto;
display: flex;
align-items: center;
justify-content: center;
}
.carousel {
overflow: hidden;
width: 100%; /* Adjust based on design */
}
.carousel-track {
display: flex;
transition: transform 0.3s ease-in-out;
}
.carousel-item {
flex: 0 0 25%; /* 4 items, so each takes 25% */
box-sizing: border-box;
padding: 1rem;
padding-left: 0;
// text-align: center;
h3 {
font-size: 1.15rem;
color: @secondary-color;
margin: 0.5rem 0;
}
.product-desc {
color: @text-color;
font-size: 0.85rem;
p {
margin-bottom: 0.25rem;
}
}
.product-item {
height: auto;
padding: 1.5rem;
text-align: center;
color: #333;
border-radius: 8px;
background-color: #f3f3f3;
}
}
.arrow {
background-color: #fff;
// border: 1px solid #ccc;
// padding: 10px;
cursor: pointer;
font-size: 1.5rem;
}
.left-arrow {
position: absolute;
left: -2rem;
}
.right-arrow {
position: absolute;
right: -2rem;
}
}
</style>