-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
133 lines (100 loc) · 3.03 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
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
const mainElement = document.querySelector('main')
const content = document.querySelector('.content')
const images = [...document.querySelectorAll('.img')]
images.forEach((image, idx) => {
image.style.backgroundImage = `url(${idx + 1}.jpg)`
image.addEventListener('click', () => {
image.classList.toggle('active')
})
})
//measure content translate pixel amount
let current = 0
//current slide number
let slide = 0
//set app height to be window.innerheight because vh works differently on mobile devices
const doc = document.documentElement;
const appHeight = () => {
doc.style.setProperty('--app-height', `${window.innerHeight}px`)
current = -slide * window.innerHeight;
content.style.transform = `translateX(${current}px)`
}
window.addEventListener('resize', appHeight);
appHeight();
mainElement.addEventListener('touchstart', startTouch, {passive: false} )
mainElement.addEventListener('touchend', endTouch, {passive: false})
mainElement.addEventListener('touchmove', moveTouch, {passive: false})
mainElement.addEventListener('mousedown', startMouseDown, {passive: false})
mainElement.addEventListener('mouseup', startMouseUp, {passive: false})
mainElement.addEventListener('wheel', wheelfun), {passive: false}
//mouse grab/ mouse wheel/ track pads
let canSwipe = true;
function wheelfun(e){
// console.log(e.deltaX)
if(canSwipe){
//swipe Up
if(e.deltaY > 30 && current !== -(window.innerHeight * 6)){
canSwipe = false
current -= window.innerHeight
slide++
content.style.transform = `translateY(${current}px)`
setTimeout(() =>{
canSwipe = true
}, 1000)
}
//swipe down
if(e.deltaY < -30 && current !== 0){
canSwipe = false
current += window.innerHeight
slide--
content.style.transform = `translateY(${current}px)`
setTimeout(() =>{
canSwipe = true
}, 1000)
}
}
}
let initialStart = 0;
let initialEnd = 0;
let initialY = 0;
let endY = 0;
function startMouseDown(e){
initialStart = Date.now()
initialY = e.clientY
}
function startMouseUp(e){
initialEnd = Date.now()
endY = e.clientY
if(initialEnd - initialStart < 800){
swipe()
}
}
//touch screen
function startTouch(e){
initialStart = Date.now()
initialY = e.touches[0].clientY
}
function endTouch(e){
initialEnd = Date.now()
endY = e.changedTouches[0].clientY
if(initialEnd - initialStart < 800){
swipe()
}
}
function swipe() {
//drag up / swipe up
if(endY - initialY < -30) {
if(current != -(window.innerHeight * 6)) {
current -= window.innerHeight
slide++
}
}else if(endY - initialY > 30){
if(current !== 0){
current += window.innerHeight
slide--
}
}
content.style.transform = `translateY(${current}px)`
}
function moveTouch(e){
e.preventDefault()
}