-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSlides.html
133 lines (121 loc) · 3.16 KB
/
Slides.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<meta name="viewport" content="width=device-width" />
<title>Slides</title>
<style>
.mySlides{
overflow: hidden;
position: relative;
}
.mySlides ol{
list-style: none;
margin: 0;
padding: 0;
display: flex;
transition: transform 0.5s;
}
.mySlides ol>li img{
vertical-align: top;
}
.mySlides-prev{
position: absolute;
top: 50%;
left: 0;
transform: translateY(-50%);
}
.mySlides-next{
position: absolute;
top: 50%;
right: 0;
transform: translateY(-50%);
}
</style>
</head>
<body>
<div class=slides>
<ol>
<li><img src="https://i.loli.net/2018/01/03/5a4c93e92b0e1.png">
</li>
<li><img src="https://i.loli.net/2018/01/03/5a4c93e931f93.png">
</li>
<li><img src="https://i.loli.net/2018/01/03/5a4c93e938b6b.png">
</li>
<ol>
</div>
</body>
<script>
class Slide {
constructor(options) {
this.options = options
this.$element = $(this.options.element)
this.$element.addClass('mySlides')
this.timer = undefined
this.initHtml()
this.bindEvents()
this.go(0)
if (this.options.autoPlay) {
this.play()
}
}
initHtml() {
this.width = this.$element.children('ol').children('li').width()
this.$element.width(this.width)
this.$prev = $('<button class="mySlides-prev">上一张</button>')
this.$element.append(this.$prev)
this.$next = $('<button class="mySlides-next">下一张</button>')
this.$element.append(this.$next)
}
bindEvents() {
this.$prev.on('click', () => this.prev())
this.$next.on('click', () => this.next())
this.$element.on('mouseenter', () => {
this.stop()
}).on('mouseleave', () => {
this.play()
})
}
go(index) {
let $ol = this.$element.children('ol')
let $items = $ol.children('li')
if (index >= $items.length) {
index = 0
} else if (index < 0) {
index = $items.length - 1
}
$ol.css({
transform: `translateX(${-index*this.width}px)`
})
this.current = index
}
next() {
this.go(this.current + 1)
}
prev() {
this.go(this.current - 1)
}
play() {
this.timer = setInterval(() => {
this.go(this.current + 1)
}, 2000)
console.log('this.timer')
console.log(this)
}
stop() {
console.log(this)
window.clearInterval(this.timer)
}
}
// -----------------------------------------------------
// ----------------------- 使用 -------------------------
// -----------------------------------------------------
var slide = new Slide({
element: '.slides',
autoPlay: false,
controls: false,
pager: false
})
</script>
</html>