This repository has been archived by the owner on Oct 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSlider.js
220 lines (193 loc) · 5.28 KB
/
Slider.js
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
import './Slider.css'
export default {
props: {
loop: {
type: Boolean,
default: false
},
selected: {
type: Number,
default: 0
},
showArrows: {
type: Boolean,
default: true
},
showNav: {
type: Boolean,
default: true
}
},
data: function () {
return {
dragStart: 0,
dragStartTime: new Date(),
transition: false,
index: this.$props.selected || 0,
lastIndex: this.$props.selected || 0,
mouseDown: false
}
},
methods: {
goToSlide: function (index, event) {
const children = this.$slots.default
const {
loop,
} = this.$props
if (event) {
event.preventDefault()
event.stopPropagation()
}
if (index < 0) {
index = loop ? children.length - 1 : 0
} else if (index >= children.length) {
index = loop ? 0 : children.length - 1
}
this.index = index
this.lastIndex = index
this.transition = true
},
getDragX: function (event, isTouch) {
return isTouch ?
event.touches[0].pageX :
event.pageX
},
handleDragStart: function (event, isTouch) {
const x = this.getDragX(event, isTouch)
this.dragStart = x
this.dragStartTime = new Date()
this.transition = false
this.slideWidth = this.$refs.slider.offsetWidth
this.mouseDown = true
},
handleDragMove: function (event, isTouch) {
const {
dragStart,
lastIndex,
slideWidth
} = this
const x = this.getDragX(event, isTouch)
const offset = dragStart - x
const percentageOffset = offset / slideWidth
const newIndex = lastIndex + percentageOffset
const SCROLL_OFFSET_TO_STOP_SCROLL = 30
// Stop scrolling if you slide more than 30 pixels
if (Math.abs(offset) > SCROLL_OFFSET_TO_STOP_SCROLL) {
event.stopPropagation()
}
this.index = newIndex
},
handleDragEnd: function () {
const children = this.$slots.default
const {
dragStartTime,
index,
lastIndex
} = this
const timeElapsed = new Date().getTime() - dragStartTime.getTime()
const offset = lastIndex - index
const velocity = Math.round(offset / timeElapsed * 10000)
let newIndex = Math.round(index)
if (Math.abs(velocity) > 5) {
newIndex = velocity < 0 ? lastIndex + 1 : lastIndex - 1
}
if (newIndex < 0) {
newIndex = 0
} else if (newIndex >= children.length) {
newIndex = children.length - 1
}
this.dragStart = 0
this.index = newIndex
this.lastIndex = newIndex
this.transition = true
this.mouseDown = false
},
renderArrows: function (h) {
const children = this.$slots.default
const {
loop,
showNav,
} = this.$props
const {
lastIndex
} = this
const arrowsClasses = showNav ? 'Slider-arrows' : 'Slider-arrows Slider-arrows--noNav'
const leftArrowEl = h(
'button', {
class: 'Slider-arrow Slider-arrow--left',
on: {
click: (e) => this.goToSlide(lastIndex - 1, e)
}
}
)
const rightArrowEl = h(
'button', {
class: 'Slider-arrow Slider-arrow--right',
on: {
click: (e) => this.goToSlide(lastIndex + 1, e)
}
}
)
return (
<div class={arrowsClasses}>
{loop || lastIndex > 0 ? leftArrowEl : null}
{loop || lastIndex < children.length - 1 ? rightArrowEl : null}
</div>
)
},
renderNav: function (h) {
const children = this.$slots.default
const {
lastIndex
} = this
const nav = children.map((slide, i) => {
const buttonClasses = i === lastIndex ? 'Slider-navButton Slider-navButton--active' : 'Slider-navButton'
return (
<button class={buttonClasses} key={i} onClick={(e) => this.goToSlide(i, e)}>
</button>
)
})
return (
<div class='Slider-nav'>
{nav}
</div>
)
}
},
render(h) {
const children = this.$slots.default
const {
showArrows,
showNav
} = this.$props
const {
index,
transition,
mouseDown
} = this
const slidesStyles = {
width: `${100 * children.length}%`,
transform: `translateX(${-1 * index * (100 / children.length)}%)`,
}
const slidesClasses = transition ? 'Slider-slides Slider-slides--transition' : 'Slider-slides'
return (
<div class="Slider" ref="slider">
{showArrows ? this.renderArrows(h) : null}
{showNav ? this.renderNav(h) : null}
<div
class="Slider-inner"
onTouchstart={(e) => this.handleDragStart(e, true)}
onTouchmove={(e) => this.handleDragMove(e, true)}
onTouchend={() => this.handleDragEnd()}
onMousedown={(e) => this.handleDragStart(e, false)}
onMousemove={(e) => mouseDown ? this.handleDragMove(e, false) : null}
onMouseup={(e) => this.handleDragEnd()}
onMouseout={(e) => this.handleDragEnd()}>
<div class={slidesClasses} style={slidesStyles}>
{children}
</div>
</div>
</div>
)
}
}