横向滑块切换图片
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
<template
><div class="card-carousel-wrapper">
<div
class="card-carousel--nav__left"
@click="moveCarousel(-1)"
:disabled="atHeadOfList"
></div>
<div class="card-carousel">
<div class="card-carousel--overflow-container">
<div
class="card-carousel-cards"
:style="{
transform: 'translateX' + '(' + currentOffset + 'px' + ')',
}"
>
<div
class="card-carousel--card"
v-for="(item, index) in images"
:key="index"
>
<viewer>
<img :src="item" class="gally-p" alt="" />
</viewer>
</div>
</div>
</div>
</div>
<div
class="card-carousel--nav__right"
@click="moveCarousel(1)"
:disabled="atEndOfList"
></div>
</div>
</template>
<script>
export default {
components: {},
props: {
images: {
required: true,
type: Array,
},
},
data() {
return {
currentOffset: 0,
// 一次展示的图片张数
windowSize: 2,
// 容器切换的offset
paginationFactor: 295,
};
},
computed: {
atEndOfList() {
return (
this.currentOffset <=
this.paginationFactor * -1 * (this.images.length - this.windowSize)
);
},
atHeadOfList() {
return this.currentOffset === 0;
},
},
mounted() {
// TODO: 监听容器大小来设置 paginationFactor
this.paginationFactor =
(document.getElementsByClassName("gally-p")[0].clientWidth || 275) + 20;
},
methods: {
moveCarousel(direction) {
// Find a more elegant way to express the :style. consider using props to make it truly generic
if (direction === 1 && !this.atEndOfList) {
this.currentOffset -= this.paginationFactor;
} else if (direction === -1 && !this.atHeadOfList) {
this.currentOffset += this.paginationFactor;
}
},
},
};
</script>
<style scoped lang="scss">
.gally-p {
width: 275px;
}
.card-carousel-wrapper {
display: flex;
align-items: center;
justify-content: center;
color: #666a73;
}
.card-carousel {
display: flex;
justify-content: center;
width: 580px;
}
.card-carousel--overflow-container {
overflow: hidden;
}
.card-carousel--nav__left,
.card-carousel--nav__right {
display: inline-block;
width: 15px;
height: 15px;
padding: 10px;
box-sizing: border-box;
border-top: 2px solid #0560ba;
border-right: 2px solid #0560ba;
cursor: pointer;
margin: 0 10px;
transition: transform 150ms linear;
}
.card-carousel--nav__left[disabled],
.card-carousel--nav__right[disabled] {
opacity: 0.2;
border-color: black;
}
.card-carousel--nav__left {
transform: rotate(-135deg);
}
.card-carousel--nav__left:active {
transform: rotate(-135deg) scale(0.9);
}
.card-carousel--nav__right {
transform: rotate(45deg);
}
.card-carousel--nav__right:active {
transform: rotate(45deg) scale(0.9);
}
.card-carousel-cards {
display: flex;
transition: transform 150ms ease-out;
transform: translatex(0px);
}
.card-carousel-cards .card-carousel--card {
margin: 0 10px;
cursor: pointer;
// box-shadow: 0 4px 15px 0 rgba(40, 44, 53, 0.06),
// 0 2px 2px 0 rgba(40, 44, 53, 0.08);
background-color: #fff;
border-radius: 4px;
z-index: 3;
margin-bottom: 2px;
}
.card-carousel-cards .card-carousel--card:first-child {
margin-left: 0;
}
.card-carousel-cards .card-carousel--card:last-child {
margin-right: 0;
}
.card-carousel-cards .card-carousel--card img {
vertical-align: bottom;
// border-top-left-radius: 4px;
// border-top-right-radius: 4px;
transition: opacity 150ms linear;
user-select: none;
}
.card-carousel-cards .card-carousel--card img:hover {
opacity: 0.5;
}
.card-carousel-cards .card-carousel--card--footer {
border-top: 0;
padding: 7px 15px;
}
.card-carousel-cards .card-carousel--card--footer p {
padding: 3px 0;
margin: 0;
margin-bottom: 2px;
font-size: 19px;
font-weight: 500;
color: #2c3e50;
user-select: none;
}
.card-carousel-cards .card-carousel--card--footer p:nth-of-type(2) {
font-size: 12px;
font-weight: 300;
padding: 6px;
background: rgba(40, 44, 53, 0.06);
display: inline-block;
position: relative;
margin-left: 4px;
color: #666a73;
}
.card-carousel-cards .card-carousel--card--footer p:nth-of-type(2):before {
content: "";
float: left;
position: absolute;
top: 0;
left: -12px;
width: 0;
height: 0;
border-color: transparent rgba(40, 44, 53, 0.06) transparent transparent;
border-style: solid;
border-width: 12px 12px 12px 0;
}
.card-carousel-cards .card-carousel--card--footer p:nth-of-type(2):after {
content: "";
position: absolute;
top: 10px;
left: -1px;
float: left;
width: 4px;
height: 4px;
border-radius: 2px;
background: white;
box-shadow: -0px -0px 0px #004977;
}
h1 {
font-size: 3.6em;
font-weight: 100;
text-align: center;
margin-bottom: 0;
color: #42b883;
}
</style>
This post is licensed under CC BY 4.0 by the author.