hagerHCarousel.vue
3.86 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
<!--
* @Date: 2024-09-29 16:27:30
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2024-09-29 16:48: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">
<!-- <img :src="item.image" :alt="item.name" style="width: 100%;"> -->
<div class="product-item">
<el-image style="width: 14rem; height: 14rem;" :src="item.image" fit="fit"></el-image>
</div>
<h3>{{ item.name }}</h3>
<p>{{ item.description }}</p>
</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: 'Item 1',
description: 'This is the description for Item 1.'
}, {
id: 2,
image: 'https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg',
name: 'Item 2',
description: 'This is the description for Item 2.'
}, {
id: 3,
image: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
name: 'Item 3',
description: 'This is the description for Item 3.'
}, {
id: 4,
image: 'https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg',
name: 'Item 4',
description: 'This is the description for Item 4.'
}, {
id: 5,
image: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
name: 'Item 5',
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: 10px;
padding-left: 0;
// text-align: center;
.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>