-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
51 lines (39 loc) · 1.19 KB
/
app.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
const upBtn = document.querySelector('.up-button')
const downBtn = document.querySelector('.down-button')
const sidebar = document.querySelector('.sidebar')
const mainSlide = document.querySelector('.main-slide')
const slidesCount = mainSlide.querySelectorAll('div').length
const container = document.querySelector('.container')
let activeSlideIndex = 0
sidebar.style.top = `-${(slidesCount - 1) * 100}vh`
upBtn.addEventListener('click', () => {
changeSlide('up')
})
downBtn.addEventListener('click', () => {
changeSlide('down')
})
document.addEventListener('keydown', event => {
if (event.key === 'ArrowUp') {
changeSlide('up')
} else if (event.key === 'ArrowDown') {
changeSlide('down')
}
})
function changeSlide(direction) {
if (direction === 'up') {
activeSlideIndex++
if (activeSlideIndex === slidesCount)
{
activeSlideIndex = 0
}
} else if (direction === 'down') {
activeSlideIndex--
if (activeSlideIndex < 0)
{
activeSlideIndex = slidesCount - 1
}
}
const height = container.clientHeight
mainSlide.style.transform = `translateY(-${activeSlideIndex * height}px)`
sidebar.style.transform = `translateY(${activeSlideIndex * height}px)`
}