-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
265 lines (204 loc) · 6.24 KB
/
index.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
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
'use strict'
var assign = require('lodash.assign')
var isElement = require('lodash.iselement')
var EventEmitter = require('events').EventEmitter
var inherits = require('util').inherits
module.exports = SyncSliders
SyncSliders.defaults = {
autoplay: true,
autoplaySpeed: 3000,
prevButton: '',
nextButton: '',
dotsSelector: '',
breakpoint: 768
}
function SyncSliders (container, options) {
if (!(this instanceof SyncSliders)) {
return new SyncSliders(container, options)
}
this.container = this.getElement(container)
this.options = assign({}, SyncSliders.defaults, options)
this.syncSliders = this.container.querySelectorAll('.sync-slider')
this.firstSlider = this.syncSliders[0]
this.secondSlider = this.syncSliders[1]
this.firstTrack = this.firstSlider.querySelector('.sync-slider-track')
this.secondTrack = this.secondSlider.querySelector('.sync-slider-track')
this.activeIndex = 0
this.totalSlides = this.firstTrack.children.length
this.firstTrack.classList.add('transition-active')
this.secondTrack.classList.add('transition-active')
this.createDots()
this.createButtons()
this.slideTo(0, true)
window.addEventListener('resize', this.onResize.bind(this))
// Only start when the height is set
this.once('set height', function () {
this.createInterval()
})
this.setHeight()
if (window.innerWidth <= this.options.breakpoint) this.mountMobile()
}
inherits(SyncSliders, EventEmitter)
var fn = SyncSliders.prototype
fn.getElement = function (el) {
if (isElement(el)) return el
return document.querySelector(el)
}
fn.slideTo = function (index, noTransition) {
if (noTransition) {
this.firstTrack.classList.remove('transition-active')
this.secondTrack.classList.remove('transition-active')
}
var currentIndex = this.activeIndex
this.emit('beforeChange', currentIndex, index)
this.moveTrack(this.firstTrack, index * 100)
var invertedActiveIndex = (this.totalSlides - 1) - index
this.moveTrack(this.secondTrack, invertedActiveIndex * 100)
this.activeIndex = index
this.emit('afterChange', index, currentIndex)
if (noTransition) {
setTimeout(function () {
this.firstTrack.classList.add('transition-active')
this.secondTrack.classList.add('transition-active')
}.bind(this), 10)
}
}
fn.moveTrack = function (track, percentage) {
var prop = ''
if (this.mobileMounted) {
prop = 'translateX(-' + percentage + '%)'
} else {
prop = 'translateY(-' + percentage + '%)'
}
track.style.transform = prop
}
fn.next = function () {
var nextIndex = (this.activeIndex + 1) % this.totalSlides
this.pause()
this.slideTo(nextIndex)
this.resume()
}
fn.prev = function () {
var prevIndex = this.activeIndex === 0
? this.totalSlides - 1
: this.activeIndex - 1
this.pause()
this.slideTo(prevIndex)
this.resume()
}
fn.setHeight = function () {
var img = this.firstTrack.querySelector('img')
if (img) {
img.addEventListener('load', this.updateHeight.bind(this))
if (img.complete) this.updateHeight()
} else {
this.updateHeight()
}
window.addEventListener('resize', this.updateHeight.bind(this))
}
fn.onResize = function () {
this.updateHeight()
var width = window.innerWidth
if (width <= this.options.breakpoint) {
this.mountMobile()
} else {
this.mountDesktop()
}
}
fn.mountDesktop = function () {
if (!this.mobileMounted) return
this.secondTrackSlides.forEach(function (slide) {
this.secondTrack.appendChild(slide)
}, this)
this.secondSlider.style.display = 'block'
this.container.classList.remove('is-mobile')
delete this.mobileMounted
delete this.secondTrackSlides
this.totalSlides = this.firstTrack.children.length
this.rebuildDots()
this.slideTo(0, true)
}
fn.mountMobile = function () {
if (this.mobileMounted) return
var secondTrackSlides = this.secondTrack.children
secondTrackSlides = [].slice.apply(secondTrackSlides)
secondTrackSlides.forEach(function (slide) {
this.firstTrack.appendChild(slide)
}, this)
this.secondTrack.innerHTML = ''
this.secondSlider.style.display = 'none'
this.container.classList.add('is-mobile')
this.mobileMounted = true
this.secondTrackSlides = secondTrackSlides
this.totalSlides = this.firstTrack.children.length
this.rebuildDots()
this.slideTo(0, true)
}
fn.updateHeight = function () {
var slide = this.firstTrack.children[0]
var height = window.getComputedStyle(slide).height
this.firstSlider.style.height = height
this.secondSlider.style.height = height
this.emit('set height')
}
fn.createInterval = fn.resume = function () {
if (!this.options.autoplay) return
this.interval = setInterval(function () {
this.next()
}.bind(this), this.options.autoplaySpeed)
}
fn.pause = function () {
if (!this.options.autoplay) return
clearInterval(this.interval)
}
fn.createDots = function () {
if (!this.options.dotsSelector) return
var dots = this.dots = this.getElement(this.options.dotsSelector)
var ul = document.createElement('ul')
var activeLi = null
ul.classList.add('sync-sliders-dots')
dots.appendChild(ul)
for (var i = 0; i < this.totalSlides; i += 1) {
var li = document.createElement('li')
var button = document.createElement('button')
button.innerHTML = i + 1
li.appendChild(button)
ul.appendChild(li)
button.addEventListener('click', function (index) {
this.pause()
this.slideTo(index)
this.resume()
}.bind(this, i))
}
function afterChange (currentIndex) {
var target = ul.children[currentIndex]
if (activeLi) activeLi.classList.remove('active')
activeLi = target
activeLi.classList.add('active')
}
this.on('afterChange', afterChange)
this.once('destroy dots', function () {
this.removeListener('afterChange', afterChange)
})
}
fn.destroyDots = function () {
if (!this.dots) return
this.dots.innerHTML = ''
delete this.dots
this.emit('destroy dots')
}
fn.rebuildDots = function () {
this.destroyDots()
this.createDots()
}
fn.createButtons = function () {
var options = this.options
if (options.prevButton) {
this.getElement(options.prevButton)
.addEventListener('click', this.prev.bind(this))
}
if (options.nextButton) {
this.getElement(options.nextButton)
.addEventListener('click', this.next.bind(this))
}
}