-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathswpier-tdesign.html
468 lines (451 loc) · 11.7 KB
/
swpier-tdesign.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
<!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: 50px auto;
/*overflow: hidden;*/
border: 1px solid black;
}
.swiper-container {
width: 100%;
height: 3rem;
position: relative;
overflow: hidden;
}
.track-horizontal {
width: auto;
height: 100%;
display: flex;
}
.track-vertical {
width: 100%;
height: 100%;
/* display: flex;
flex-direction: column;*/
}
.swiper-item {
width: 100%;
height: 100%;
flex-shrink: 0;
position: relative;
}
.swiper-item img {
width: 100%;
height: 100%;
}
.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 {
display: flex;
justify-content: center;
align-items: center;
position: absolute;
z-index: 100;
}
.indicators-horizontal {
width: 100%;
left: 0;
bottom: 10px;
}
.indicators-vertical {
flex-direction: column;
height: 100%;
top: 0;
right: 10px;
}
.indicator {
width: 8px;
height: 8px;
background: rgba(255, 255, 255, 0.5);
border-radius: 50%;
margin: 3px;
}
.indicator.active {
background: #fff;
}
.swiper-btn .prev, .swiper-btn .next {
width: 20px;
height: 20px;
background: rgba(0, 0, 0, 0.5);
border-radius: 50%;
color: #fff;
font-size: 0;
display: flex;
justify-content: center;
align-items: center;
}
.swiper-btn .prev {
position: absolute;
left: 10px;
top: 50%;
transform: translateY(-50%);
}
.swiper-btn .prev::before {
content: '';
width: 6px;
height: 6px;
border: 2px solid #fff;
border-right: none;
border-top: none;
transform: translateX(1px) rotate(45deg);
}
.swiper-btn .next {
position: absolute;
right: 10px;
top: 50%;
transform: translateY(-50%);
}
.swiper-btn .next::before {
content: '';
width: 6px;
height: 6px;
border: 2px solid #fff;
border-left: none;
border-bottom: none;
transform: translateX(-1px) rotate(45deg);
}
</style>
</head>
<body>
<div id="app" v-cloak>
<div class="swiper-container" ref="swiper">
<div :class="[vertical ? 'track-vertical' : 'track-horizontal']" ref="track" :style="trackStyle">
<div class="swiper-item" v-for="(item, i) in swiperList" :data-index="i">
<!-- <div :class="['content', 'content-'+(i+1)]" @click="clickItem(i)">{{ i + 1 }}</div> -->
<img :src="item.pic" alt="">
</div>
</div>
<div :class="['swiper-indicators', vertical ? 'indicators-vertical' : 'indicators-horizontal']" 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="showButton">
<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: 400
},
threshold: { // 移动超过百分比则换页
type: Number,
default: 0.5
},
// 是否显示索引
indicator: {
type: Boolean,
default: true
},
// 默认索引
defaultIndex: {
type: Number,
default: 1
},
// 是否显示
showButton: {
type: Boolean,
default: true
}
},
data () {
return {
swiperList: [
{ type: 1, name: 'slid-1', pic: 'https://tdesign.gtimg.com/site/swiper/02.png' },
{ type: 2, name: 'slid-2', pic: 'https://tdesign.gtimg.com/site/swiper/03.png' },
{ type: 3, name: 'slid-3', pic: 'https://i1.mifile.cn/a4/xmad_15517939170939_oiXCK.jpg' },
],
pageList: [], // 轮播列表
draging: false, // 是否正在拖动
currentIndex: 0, // 当前项索引
currentOffset: 0, // 当前容器偏移量
dragState: {}, // 拖动详情
timerIns: null, // 定时器
}
},
computed: {
// swiper页数
count () {
return this.pageList.length
},
// 容器宽高
swiperSize () {
const direction = this.vertical ? 'height' : 'width';
return this.$refs.swiper.getBoundingClientRect()[direction];
},
trackStyle () {
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 track = this.$refs.track;
// 获取所有轮播列表
const children = track.querySelectorAll('.swiper-item');
this.pageList = children;
// 循环前后添加一个clone元素
if (this.isLoop) {
const firstClone = this.pageList[0].cloneNode(true);
const lastClone = this.pageList[this.pageList.length - 1].cloneNode(true);
track.insertBefore(lastClone, this.pageList[0])
track.appendChild(firstClone)
}
// 处理默认索引
this.currentIndex = this.defaultIndex;
this.correctionPosition();
// 自动轮播
this.startAutoplay();
// 禁止拖动
if (this.disabled || this.pageList.length <= 1) {
return
}
track.addEventListener('touchstart', this.dragStartEvent);
track.addEventListener('touchmove', this.dragMoveEvent);
track.addEventListener('touchend', this.dragEndEvent);
track.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.isLoop) {
this.stopAutoplay();
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('dragStartEvent')
// 开始拖动则停止自动轮播
this.stopAutoplay()
const touch = e.changedTouches ? e.changedTouches[0] : e;
// 开始拖动标志
this.draging = true;
// 记录开始时间
this.dragState.startTime = Date.now();
// 记录开始X、Y坐标
this.dragState.startX = touch.pageX;
this.dragState.startY = touch.pageY;
},
dragMoveEvent (e) {
const touch = e.changedTouches ? e.changedTouches[0] : e;
// 记录移动的当前X、Y坐标
this.dragState.endX = touch.pageX;
this.dragState.endY = touch.pageY;
const offset = this.vertical ? this.dragState.endY - this.dragState.startY : this.dragState.endX - this.dragState.startX;
// 拖动
this.move(offset);
},
dragEndEvent (e) {
console.log('dragEndEvent')
const touch = e.changedTouches ? e.changedTouches[0] : e;
// 结束拖动标志
this.draging = false;
// 记录结束X、Y坐标
this.dragState.endX = touch.pageX;
this.dragState.endY = touch.pageY;
// console.log(this.dragState);
const dragDuration = Date.now() - this.dragState.startTime;
const offset = this.vertical ? this.dragState.endY - this.dragState.startY : this.dragState.endX - this.dragState.startX;
// 满足切换条件则改变索引
if (Math.abs(offset / dragDuration) >= 0.25 || Math.abs(offset) >= this.swiperSize * this.threshold) {
offset < 0 ? this.currentIndex++ : this.currentIndex--
console.log('满足切换')
}
this.move(0);
this.startAutoplay()
},
move (offset) {
const num = this.isLoop ? 1 : 0;
// 目标索引
let targetIndex = (this.currentIndex + this.pageList.length) % this.pageList.length;
// 目标距离
let targetOffset = -(this.currentIndex + num) * this.swiperSize + offset;
// 非循环边界处理
if (!this.isLoop) {
if (this.currentIndex <= 0) {
targetIndex = 0;
}
if (this.currentIndex >= this.pageList.length - 1) {
targetIndex = this.pageList.length - 1
}
if (targetOffset >= 0) {
targetOffset = 0
}
if (targetOffset <= -(this.pageList.length - 1) * this.swiperSize) {
targetOffset = -(this.pageList.length - 1) * this.swiperSize
}
}
// console.log("targetIndex>>", targetIndex, targetOffset)
this.currentIndex = targetIndex
this.currentOffset = targetOffset
},
// transition结束事件
transitionEndEvent () {
// console.log('transitionEnd')
if (this.isLoop) {
this.correctionPosition()
}
this.startAutoplay()
},
// 修改偏移位置
correctionPosition () {
const num = this.isLoop ? 1 : 0;
// console.log("correctionPosition>>>", this.currentIndex, this.currentOffset)
if (this.currentOffset === 0 || this.currentOffset === -this.swiperSize * (this.pageList.length + 1)) {
console.log('正在修正')
this.draging = true
const targetOffset = -(this.currentIndex + num) * this.swiperSize
this.currentOffset = targetOffset
}
},
// 点击跳转到指定位置
slideTo (i) {
if (this.currentIndex === i) {
return
}
this.stopAutoplay()
this.currentIndex = i
requestAnimationFrame(() => {
this.draging = false
this.move(0)
})
},
// 上一张
slidePrev () {
this.stopAutoplay()
this.currentIndex--
requestAnimationFrame(() => {
this.draging = false
this.move(0)
})
},
// 下一张
slideNext () {
this.stopAutoplay()
this.currentIndex++
requestAnimationFrame(() => {
this.draging = false
this.move(0)
})
}
}
}
Vue.createApp(App).mount('#app')
</script>
</body>
</html>