Skip to content

Commit 090a3a9

Browse files
committed
Js animation
1 parent 1fc01d0 commit 090a3a9

2 files changed

Lines changed: 97 additions & 0 deletions

File tree

mix/Anim-with-setInterval.html

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Animation With setInterval</title>
8+
</head>
9+
<style>
10+
* {
11+
margin: 0;
12+
padding: 0;
13+
}
14+
body {
15+
height: 100vh;
16+
display: grid;
17+
place-items: center;
18+
}
19+
div {
20+
width: 100px;
21+
height: 100px;
22+
border-radius: 50%;
23+
background: red;
24+
transition: all 0.5s cubic-bezier(0.075, 0.82, 0.165, 1);
25+
}
26+
</style>
27+
<body>
28+
<div id="circle"></div>
29+
<script>
30+
const circle = document.getElementById("circle");
31+
let x = 0;
32+
let limit = 200;
33+
34+
const move = setInterval(() => {
35+
if (x > limit) {
36+
clearInterval(move);
37+
}
38+
x += 1;
39+
if (x == 100) {
40+
circle.style.background = "green";
41+
}
42+
circle.style.transform = `translateX(${x}px)`;
43+
}, 10);
44+
</script>
45+
</body>
46+
</html>

mix/reqAnimation.html

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Animation With requestAnimation</title>
8+
</head>
9+
<style>
10+
* {
11+
margin: 0;
12+
padding: 0;
13+
}
14+
body {
15+
height: 100vh;
16+
display: grid;
17+
place-items: center;
18+
}
19+
div {
20+
width: 100px;
21+
height: 100px;
22+
border-radius: 50%;
23+
background: red;
24+
transition: all 0.5s cubic-bezier(0.075, 0.82, 0.165, 1);
25+
}
26+
</style>
27+
<body>
28+
<div id="circle"></div>
29+
<script>
30+
const circle = document.getElementById("circle");
31+
let x = 0;
32+
let limit = 200;
33+
let rafId = 0;
34+
const move = (_) => {
35+
x += 1;
36+
circle.style.transform = `translateX(${x}px)`;
37+
38+
if (x === limit) {
39+
cancelAnimationFrame(rafId);
40+
circle.style.background = "red";
41+
} else {
42+
rafId = requestAnimationFrame(move);
43+
}
44+
if (x == 100) {
45+
circle.style.background = "green";
46+
}
47+
};
48+
window.requestAnimationFrame(move);
49+
</script>
50+
</body>
51+
</html>

0 commit comments

Comments
 (0)