-
Notifications
You must be signed in to change notification settings - Fork 1
/
single-box-animation.html
54 lines (49 loc) · 1.15 KB
/
single-box-animation.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
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/common.css" />
<style>
body {
height: 50vh;
width: 100%;
overflow: hidden;
}
.box-world {
display: flex;
height: 100%;
}
.box {
width: 100px;
height: 100px;
position: relative;
cursor: pointer;
background-color: rgb(var(--green));
margin: auto 0;
}
</style>
</head>
<body>
<div class="box-world">
<div class="box"></div>
</div>
<script>
const box = document.querySelector(".box")
const rate = 10
let startTime
box.addEventListener("click", () => {
requestAnimationFrame((time) => (startTime = time))
requestAnimationFrame(sine)
})
function sine(currentTime) {
const elapsedTime = currentTime - startTime
const x = elapsedTime / rate
const y = Math.sin(elapsedTime / 500) * 100
box.style.left = `${x}px`
box.style.top = `${y}px`
if (x + 50 < window.innerWidth) {
requestAnimationFrame(sine)
}
}
</script>
</body>
</html>