-
Notifications
You must be signed in to change notification settings - Fork 0
/
people-scroll.html
142 lines (138 loc) · 5.05 KB
/
people-scroll.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
body {
margin: 0;
overflow: hidden;
white-space: nowrap;
-ms-overflow-style: none; /* IE and Edge */
scrollbar-width: none;
}
.container {
position: fixed;
left: var(--fixed-x);
display: flex;
flex-direction: row;
/* justify-content: center; */
}
.scroll-item {
width: 200px;
height: 200px;
align-self: center;
padding: 0;
margin: 0 1rem;
background-size: cover;
background-position: center;
}
</style>
</head>
<body>
<div class="container">
<div class="scroll-item"></div>
<div class="scroll-item"></div>
<div class="scroll-item"></div>
<div class="scroll-item"></div>
<div class="scroll-item"></div>
<div class="scroll-item"></div>
<div class="scroll-item"></div>
<div class="scroll-item"></div>
<div class="scroll-item"></div>
<div class="scroll-item"></div>
<div class="scroll-item"></div>
<div class="scroll-item"></div>
<div class="scroll-item"></div>
<div class="scroll-item"></div>
<div class="scroll-item"></div>
<div class="scroll-item"></div>
<div class="scroll-item"></div>
<div class="scroll-item"></div>
<div class="scroll-item"></div>
<div class="scroll-item"></div>
<div class="scroll-item"></div>
<div class="scroll-item"></div>
<div class="scroll-item"></div>
<div class="scroll-item"></div>
<script>
container = document.getElementsByClassName('container')[0];
items = document.getElementsByClassName('scroll-item');
for (var i in items) {
if (i == items.length-1) {
items[i].remove();
break;
}
items[i].style.backgroundImage = 'url("https://luna-coding.github.io/luna-dimigoin/assets/images/people/' + (Number(i)+1) + '.jpg")'
}
// itemW = items[0].offsetWidth + items[0].style.marginLeft + items[0].style.marginRight; // item의 width는 동일해야 함
containerW = container.offsetWidth;
itemW = containerW / items.length;
console.log(itemW);
fixedX = 0;
justMoved = false;
justMovedClearTimeoutId = null;
checkOvered = () => {
// 아이콘이 왼쪽을 넘어갔는지 확인
while (-fixedX >= itemW) {
let ele = items[0].cloneNode();
container.appendChild(ele);
items[0].remove();
items = document.getElementsByClassName('scroll-item');
fixedX += itemW;
container.style.left = fixedX + 'px';
}
// 아이콘이 오른쪽을 넘어갔는지 확인
while (fixedX >= 0) {
let ele = items[items.length-1].cloneNode();
container.insertBefore(ele, container.firstChild);
items[items.length-1].remove();
items = document.getElementsByClassName('scroll-item');
fixedX -= itemW;
container.style.left = fixedX + 'px';
}
};
setInterval(
() => {
if (!justMoved) {
container.style.left = fixedX + 'px';
fixedX -= 1;
checkOvered();
}
},
20
);
document.body.addEventListener('wheel', (event) => {
var delta = (event.deltaX == 0) ? event.deltaY : event.deltaX;
if (Math.abs(delta) >= 10) {
var alpha = 1 * delta / Math.abs(delta);
var gamma = alpha;
step = () => {
fixedX -= gamma;
container.style.left = fixedX + 'px';
checkOvered();
gamma += alpha;
if (Math.abs(gamma) < Math.abs(delta/10)) {
setTimeout(step, 5);
}
}
setTimeout(step, 5);
}
fixedX -= delta;
container.style.left = fixedX + 'px';
checkOvered();
justMoved = true;
if (justMovedClearTimeoutId !== null) {
clearTimeout(justMovedClearTimeoutId);
}
justMovedClearTimeoutId = setTimeout(() => {
justMoved = false;
}, 1000);
event.preventDefault();
return false;
}, {passive: false});
</script>
</body>
</html>