image-swiper.vue
4.08 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
<template>
<div class="image-swipe">
<div class="swipe-wrap" @click.prevent="showcase()">
<swiper :options="swiperOption" v-ref:swiper>
<swiper-slide v-for="item in goodsList">
<img title="{{item.title}}"
:src="item.color_image | resize 580 770"
width="100%">
</swiper-slide>
</swiper>
</div>
<div class="pagination-wrapper">
<ul class="page-items">
<li v-for="index in slideCount" class="page-item" :class="{active: index == activeIndex}"></li>
</ul>
</div>
</div>
</template>
<script>
const _ = require('lodash');
const yoho = require('yoho');
const vas = require('vue-awesome-swiper');
module.exports = {
props: {
goods: [Object]
},
data() {
let vm = this;
return {
slideCount: 0,
activeIndex: 0,
goodsList: [],
swiperOption: {
loop: true,
slidesPerView: 3,
onSlideChangeEnd(swiper) {
vm.setActiveSlide(swiper.realIndex)
}
}
};
},
components: {
swiper: vas.swiper,
swiperSlide: vas.swiperSlide,
notNextTick: true
},
computed: {
swiper() {
return this.$refs.swiper.swiper
}
},
watch: {
goods(val) {
let temp = val.slice();
const len = temp.length;
if (len > 1 && len < 4) {
temp.unshift(temp.splice(len - 1)[0]);
}
if (len >= 4) {
const sp = temp.splice(0, 2);
temp.push(sp[0]);
temp.push(sp[1]);
}
this.goodsList = temp;
this.slideCount = this.goodsList.length;
}
},
methods: {
showcase: function() {
const realIndex = this.swiper.realIndex;
const index = this.calcIndex(realIndex);
const opts = {
index,
images: this.goods.map((item) => {
return item.color_image;
}).filter(image => image),
};
yoho.goImageBrowser(opts);
},
setActiveSlide(idx){
this.activeIndex = this.calcIndex(idx);
},
calcIndex(realIdx){
const len = this.goods.length;
if (len >= 4) {
if (realIdx === len -1) {
realIdx = 0;
} else {
realIdx += 1;
}
if (realIdx < len -2) {
realIdx += 2;
} else if (realIdx === len -2) {
realIdx = 0;
} else {
realIdx = 1;
}
}
return realIdx;
}
}
};
</script>
<style>
.image-swipe {
overflow: hidden;
.swipe-wrap {
width: 1800px;
transform: translateX(-520px);
}
.swiper-slide {
width: 600px;
height: 770px;
padding: 0 10px;
}
.swiper-container {
overflow: visible;
}
.pagination-wrapper {
position: relative;
height: 60px;
text-align: center;
background-color: #f7f7f7;
.page-items {
top: 12px;
left: 50%;
line-height: 55px;
}
.page-item {
display: inline-block;
width: 10px;
height: 10px;
margin: 0 5px;
border-radius: 0;
background-color: #b0b0b0;
}
.page-item.active {
width: 20px;
background-color: #000;
}
}
}
</style>