hookehuyr

新增水平图片展示组件

1 +<!--
2 + * @Date: 2024-09-29 16:27:30
3 + * @LastEditors: hookehuyr hookehuyr@gmail.com
4 + * @LastEditTime: 2024-09-29 16:48:19
5 + * @FilePath: /hager/src/components/hagerHCarousel.vue
6 + * @Description: 文件描述
7 +-->
8 +<template>
9 + <div class="hager-h-carousel">
10 + <div class="carousel-container">
11 + <div v-if="items.length > 4" @click="prevItem" class="arrow left-arrow"><i class="el-icon-arrow-left"></i></div>
12 + <div class="carousel">
13 + <div class="carousel-track" :style="trackStyle">
14 + <div v-for="item in items" :key="item.id" class="carousel-item">
15 + <!-- <img :src="item.image" :alt="item.name" style="width: 100%;"> -->
16 + <div class="product-item">
17 + <el-image style="width: 14rem; height: 14rem;" :src="item.image" fit="fit"></el-image>
18 + </div>
19 + <h3>{{ item.name }}</h3>
20 + <p>{{ item.description }}</p>
21 + </div>
22 + </div>
23 + </div>
24 + <div v-if="items.length > 4" @click="nextItem" class="arrow right-arrow"><i class="el-icon-arrow-right"></i></div>
25 + </div>
26 + </div>
27 +</template>
28 +
29 +<script>
30 +import mixin from 'common/mixin';
31 +
32 +export default {
33 + mixins: [mixin.init],
34 + data () {
35 + return {
36 + items: [{
37 + id: 1,
38 + image: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
39 + name: 'Item 1',
40 + description: 'This is the description for Item 1.'
41 + }, {
42 + id: 2,
43 + image: 'https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg',
44 + name: 'Item 2',
45 + description: 'This is the description for Item 2.'
46 + }, {
47 + id: 3,
48 + image: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
49 + name: 'Item 3',
50 + description: 'This is the description for Item 3.'
51 + }, {
52 + id: 4,
53 + image: 'https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg',
54 + name: 'Item 4',
55 + description: 'This is the description for Item 4.'
56 + }, {
57 + id: 5,
58 + image: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
59 + name: 'Item 5',
60 + description: 'This is the description for Item 5.'
61 + }, {
62 + id: 6,
63 + image: 'https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg',
64 + name: 'Item 6',
65 + description: 'This is the description for Item 6.'
66 + }],
67 + currentIndex: 0,
68 + }
69 + },
70 + mounted () {
71 +
72 + },
73 + computed: {
74 + trackStyle() {
75 + return {
76 + transform: `translateX(-${this.currentIndex * 25}%)`
77 + };
78 + }
79 + },
80 + methods: {
81 + nextItem() {
82 + if (this.currentIndex < this.items.length - 4) {
83 + this.currentIndex++;
84 + } else {
85 + this.currentIndex = 0; // Loop back to start
86 + }
87 + },
88 + prevItem() {
89 + if (this.currentIndex > 0) {
90 + this.currentIndex--;
91 + } else {
92 + this.currentIndex = this.items.length - 4; // Loop to end
93 + }
94 + }
95 + }
96 +}
97 +</script>
98 +
99 +<style lang="less">
100 +.hager-h-carousel {
101 + .carousel-container {
102 + position: relative;
103 + width: 100%;
104 + // max-width: 1200px;
105 + margin: 0 auto;
106 + display: flex;
107 + align-items: center;
108 + justify-content: center;
109 + }
110 +
111 + .carousel {
112 + overflow: hidden;
113 + width: 100%; /* Adjust based on design */
114 + }
115 +
116 + .carousel-track {
117 + display: flex;
118 + transition: transform 0.3s ease-in-out;
119 + }
120 +
121 + .carousel-item {
122 + flex: 0 0 25%; /* 4 items, so each takes 25% */
123 + box-sizing: border-box;
124 + padding: 10px;
125 + padding-left: 0;
126 + // text-align: center;
127 + .product-item {
128 + height: auto;
129 + padding: 1.5rem;
130 + text-align: center;
131 + color: #333;
132 + border-radius: 8px;
133 + background-color: #f3f3f3;
134 + }
135 + }
136 +
137 + .arrow {
138 + background-color: #fff;
139 + // border: 1px solid #ccc;
140 + // padding: 10px;
141 + cursor: pointer;
142 + font-size: 1.5rem;
143 + }
144 +
145 + .left-arrow {
146 + position: absolute;
147 + left: -2rem;
148 + }
149 +
150 + .right-arrow {
151 + position: absolute;
152 + right: -2rem;
153 + }
154 +}
155 +</style>