-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathswpier-vant.html
414 lines (397 loc) · 10.8 KB
/
swpier-vant.html
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
// 适配方案
(function(window, document){
function setRem() {
let deviceWidth = document.documentElement.clientWidth;
deviceWidth = deviceWidth > 750 ? 750 : deviceWidth;
document.documentElement.style.fontSize = deviceWidth / 7.5 + "px";
}
setRem();
window.addEventListener("resize", setRem);
window.addEventListener('pageshow', function (e) {
if (e.persisted) {
setRem();
}
})
})(window, document)
</script>
<style type="text/css">
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
[v-cloak] {
display: none;
}
html, body {
overflow: hidden;
}
#app {
width: 302px;
/*min-height: 100vh;*/
margin: 0 auto;
/*overflow: hidden;*/
border: 1px solid black;
}
.swiper-container {
width: 100%;
height: 3rem;
position: relative;
/*overflow: hidden;*/
}
.swiper-wrapper {
width: auto;
height: 100%;
/*position: absolute;
left: 0;
top: 0;*/
display: flex;
}
.swiper-item {
width: 100%;
height: 100%;
flex-shrink: 0;
position: relative;
}
.swiper-item.active {
display: block;
transform: translate3d(0, 0, 0);
}
.content {
height: 100%;
font-size: 30px;
display: flex;
justify-content: center;
align-items: center;
}
.content-1 {
background: green;
color: #fff;
}
.content-2 {
background: pink;
color: #fff;
}
.content-3 {
background: skyblue;
color: #fff;
}
.content-4 {
background: yellowgreen;
color: #fff;
}
.swiper-indicators {
width: 100%;
display: flex;
justify-content: center;
align-items: center;
position: absolute;
left: 0;
bottom: 10px;
z-index: 100;
}
.indicator {
width: 8px;
height: 8px;
background: rgba(255, 255, 255, 0.5);
border-radius: 50%;
margin: 0 3px;
}
.indicator.active {
background: #fff;
}
.swiper-btn .prev {
position: absolute;
left: 0;
top: 50%;
transform: translateY(-50%);
}
.swiper-btn .next {
position: absolute;
right: 0;
top: 50%;
transform: translateY(-50%);
}
</style>
</head>
<body>
<div id="app" v-cloak>
<div class="swiper-container" ref="swiper">
<div class="swiper-wrapper" ref="wrapper" :style="wrapperStyle">
<div class="swiper-item" :class="{'active': i===currentIndex}" v-for="(item, i) in swiperList">
<div :class="['content', 'content-'+(i+1)]" @click="clickItem(i)">{{ i + 1 }}</div>
</div>
</div>
<div class="swiper-indicators" v-if="indicator">
<div :class="['indicator', {'active': i===currentIndex}]" v-for="(item, i) in swiperList" @click.stop="slideTo(i)"></div>
</div>
<div class="swiper-btn" v-if="true">
<div class="prev" @click="slidePrev"><</div>
<div class="next" @click="slideNext">></div>
</div>
</div>
</div>
<script src="https://unpkg.com/vue@next"></script>
<script>
console.log(Vue)
const App = {
props: {
disabled: { // 禁止拖动
type: Boolean,
default: false
},
loop: { // 是否循环
type: Boolean,
default: true
},
autoplay: { // 自动播放时间间隔
type: Number,
default: 4000
},
vertical: {
type: Boolean,
default: false
},
speed: { // 过度时间
type: Number,
default: 500
},
threshold: { // 移动超过百分比则换页
type: Number,
default: 0.5
},
// 是否显示索引
indicator: {
type: Boolean,
default: true
},
// 默认索引
defaultIndex: {
type: Number,
default: 1
}
},
data () {
return {
swiperList: [
{ type: 1, name: 'slid-1', pic: 'https://i1.mifile.cn/a4/xmad_15535933141925_ulkYv.jpg' },
{ type: 2, name: 'slid-2', pic: 'https://i1.mifile.cn/a4/xmad_15532384207972_iJXSx.jpg' },
{ type: 3, name: 'slid-3', pic: 'https://i1.mifile.cn/a4/xmad_15517939170939_oiXCK.jpg' },
{ type: 4, name: 'slid-4', pic: 'https://i1.mifile.cn/a4/xmad_15532384207972_iJXSx.jpg' },
],
pageList: [], // 轮播列表
draging: false, // 是否正在拖动
currentIndex: 0, // 当前项索引
currentOffset: 0, // 当前容器偏移量
dragState: {}, // 拖动详情
timerIns: null, // 定时器
}
},
computed: {
// swiper页数
count () {
return this.pageList.length
},
// 容器宽
swiperWidth () {
return this.$refs.swiper.getBoundingClientRect().width;
},
// 偏移的最小值
minOffset () {
return -this.swiperWidth * (this.pageList.length - 1)
},
wrapperStyle () {
return {
transform: `translate${this.vertical ? 'Y' : 'X'}(${this.currentOffset}px)`,
transition: `transform ${this.draging ? 0 : this.speed}ms ease`
}
},
// 是否循环
isLoop () {
return this.loop && this.pageList.length > 1;
}
},
mounted () {
this.$nextTick(() => {
this.init()
})
},
beforeDestroy() {
this.timerIns && clearInterval(this.timeIns);
},
methods: {
clickItem (i) {
console.log("click>>>", i)
},
init () {
const swiper = this.$refs.swiper;
const wrapper = this.$refs.wrapper;
// 获取所有轮播列表
const children = wrapper.querySelectorAll('.swiper-item');
this.pageList = children;
// 处理默认索引
this.draging = true;
this.currentIndex = this.defaultIndex;
this.currentOffset = -this.defaultIndex * this.swiperWidth
// 自动轮播
this.startAutoplay();
// 禁止拖动
if (this.disabled || this.pageList.length <= 1) {
return
}
wrapper.addEventListener('touchstart', this.dragStartEvent);
wrapper.addEventListener('touchmove', this.dragMoveEvent);
wrapper.addEventListener('touchend', this.dragEndEvent);
// wrapper.addEventListener('transitionend', this.transitionEndEvent)
// swiper.addEventListener('mousedown', this.dragStartEvent);
// swiper.addEventListener('mousemove', this.dragMoveEvent);
// swiper.addEventListener('mouseup', this.dragEndEvent);
window.addEventListener('visibilitychange', this.onVisibilityChange)
},
startAutoplay () {
console.log('start开始播放')
if (this.autoplay) {
this.timerIns && clearTimeout(this.timerIns)
this.timerIns = setTimeout(() => {
this.slideNext();
this.startAutoplay()
}, this.autoplay)
}
},
stopAutoplay () {
this.timerIns && clearTimeout(this.timerIns)
console.log('stop自动播放')
},
// 页面可见性
onVisibilityChange () {
console.log(document.visibilityState);
if (document.visibilityState === 'visible') {
this.startAutoplay()
} else {
this.stopAutoplay()
}
},
dragStartEvent (e) {
console.log(e, this.currentIndex, this.currentOffset)
// 开始拖动则停止自动轮播
this.stopAutoplay()
const touch = e.changedTouches ? e.changedTouches[0] : e;
// 开始拖动标志
this.draging = true;
// 记录开始时间
this.dragState.startTime = Date.now();
// 记录开始X坐标
this.dragState.startX = touch.pageX;
},
dragMoveEvent (e) {
const touch = e.changedTouches ? e.changedTouches[0] : e;
// 记录移动的当前坐标x
this.dragState.endX = touch.pageX;
const distance = this.dragState.endX - this.dragState.startX;
// 拖动
this.translate(distance);
},
dragEndEvent (e) {
const touch = e.changedTouches ? e.changedTouches[0] : e;
// 结束拖动标志
this.draging = false;
// 记录结束坐标x
this.dragState.endX = touch.pageX;
// console.log(this.dragState);
const dragDuration = Date.now() - this.dragState.startTime;
const distance = this.dragState.endX - this.dragState.startX;
// if (this.dragState.startX === this.dragState.endX) {
// return
// }
// 满足切换条件
if (Math.abs(distance/dragDuration) >= 0.25 || Math.abs(distance) >= this.swiperWidth * this.threshold) {
distance < 0 ? this.currentIndex++ : this.currentIndex--
this.translate(0)
} else {
// 不满足还原
this.translate(0);
}
this.startAutoplay()
},
translate (distance) {
// 目标索引
let targetIndex = (this.currentIndex + this.pageList.length) % this.pageList.length
// 滑动的目标距离
let targetOffset = -(this.currentIndex * this.swiperWidth) + distance
// 循环处理
if (this.isLoop) {
if (targetOffset <= this.minOffset) {
this.pageList[0].style = `transform: translateX(${this.pageList.length * this.swiperWidth}px`
} else {
this.pageList[0].removeAttribute('style')
}
if (targetOffset >= 0) {
this.pageList[this.pageList.length - 1].style = `transform: translateX(-${this.pageList.length * this.swiperWidth}px`
} else {
this.pageList[this.pageList.length - 1].removeAttribute('style')
}
} else {
// 不循环时偏移和当前索引的边界处理
targetOffset = targetOffset >= 0 ? 0 : (targetOffset <= this.minOffset ? this.minOffset : targetOffset)
targetIndex = this.currentIndex > this.pageList.length - 1 ? this.pageList.length - 1 : (this.currentIndex < 0 ? 0 : targetIndex)
}
// console.log("targetIndex>>", targetIndex)
this.currentIndex = targetIndex
this.currentOffset = targetOffset
},
// 修改偏移位置
correctionPosition () {
// console.log("correctionPosition>>>", this.currentIndex, this.currentOffset)
if (this.currentOffset === this.swiperWidth || this.currentOffset === -this.swiperWidth * this.pageList.length) {
this.draging = true
let targetOffset = -this.currentIndex * this.swiperWidth
this.currentOffset = targetOffset
this.pageList[0].removeAttribute('style')
this.pageList[this.pageList.length - 1].removeAttribute('style')
}
},
// 点击跳转到指定位置
slideTo (i) {
if (this.currentIndex === i) {
return
}
this.correctionPosition()
this.currentIndex = i
requestAnimationFrame(() => {
this.draging = false
this.translate(0)
})
},
// 上一张
slidePrev () {
this.correctionPosition()
this.currentIndex--
requestAnimationFrame(() => {
this.draging = false
this.translate(0)
})
},
// 下一张
slideNext () {
this.correctionPosition()
this.currentIndex++
requestAnimationFrame(() => {
requestAnimationFrame(() => {
this.draging = false
this.translate(0)
})
})
}
}
}
Vue.createApp(App).mount('#app')
</script>
</body>
</html>