-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
53 lines (45 loc) · 1.61 KB
/
script.js
File metadata and controls
53 lines (45 loc) · 1.61 KB
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
window.onload = function () {
scrollMove();
};
function scrollMove() {
const mainSections = document.querySelectorAll(".main-section");
let currentPosition = 0; // 0: header, 1: first section, 2: second section, ...
let isScrolling = false;
const headerHeight = 10 * parseFloat(getComputedStyle(document.documentElement).fontSize);
document.addEventListener(
"wheel",
(e) => {
e.preventDefault();
if (isScrolling) return;
const clientHeight = window.innerHeight;
if (e.deltaY > 0) { // 아래로 스크롤
if (currentPosition <= mainSections.length) {
currentPosition++;
scrollToPosition(currentPosition, clientHeight, headerHeight);
}
} else if (e.deltaY < 0) { // 위로 스크롤
if (currentPosition > 0) {
currentPosition--;
scrollToPosition(currentPosition, clientHeight, headerHeight);
}
}
isScrolling = true;
setTimeout(() => {
isScrolling = false;
}, 500);
},
{ passive: false }
);
function scrollToPosition(position, clientHeight, headerHeight) {
let targetPosition;
if (position === 0) { // 헤더로 이동
targetPosition = 0;
} else { // 각 섹션으로 이동
targetPosition = headerHeight + (clientHeight * (position - 1));
}
scrollTo({
top: targetPosition,
behavior: "smooth"
});
}
}