-
Notifications
You must be signed in to change notification settings - Fork 0
/
slider.js
46 lines (38 loc) · 1.26 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
export default function slider(options) {
validate(options)
initSlider(options)
insertSlides(options)
swipeSlides(options)
}
function swipeSlide(index) {
const slider = document.querySelector('.slider')
const shift = -750 * index
slider.style.transform=`translateX(${shift}px)`
}
function swipeSlides(options) {
const delay = options.delay || 2500
options.slides.forEach((currentElement, index) => {
setTimeout(() => {
swipeSlide(index)
}, index * delay)
})
}
function insertSlides(options) {
const slider = document.querySelector('.slider')
const width = options.width || 750
const slidesHTML = options.slides.map((item) =>
`<div class='slider__item' style='background: ${item.color}; min-width: ${width}px'>${item.text}</div>`).join('')
slider.innerHTML = slidesHTML
}
function validate(options) {
if (options.root == undefined) { throw 'The root is required.' }
if (options.slides == undefined) { throw 'The slides are required.' }
}
function initSlider(options) {
const wrapper = document.querySelector(options.root)
wrapper.innerHTML = '<div class="slider"></div>'
const width = options.width || 750
const height = options.height || 400
wrapper.style.width = `${width}px`
wrapper.style.height = `${height}px`
}