Skip to content

Commit a2c0590

Browse files
Update animationbegin.html
1 parent 7c98384 commit a2c0590

File tree

1 file changed

+58
-53
lines changed

1 file changed

+58
-53
lines changed

api/animationbegin.html

Lines changed: 58 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<!DOCTYPE html>
22
<html>
3+
34
<head>
45
<meta charset="UTF-8">
56
<title>JavaScript Animation Example</title>
@@ -12,7 +13,8 @@
1213
color: #2c2c2c;
1314
}
1415

15-
h2, h3 {
16+
h2,
17+
h3 {
1618
color: #1a237e;
1719
}
1820

@@ -84,59 +86,62 @@
8486
}
8587
</style>
8688
</head>
89+
8790
<body>
8891

89-
<h2>JavaScript Animation Using <code>setInterval()</code></h2>
90-
91-
<p>
92-
This example demonstrates how to animate a box using JavaScript timers. The box moves horizontally using <code>setInterval</code>, and you can control the animation with start, stop, and reset buttons.
93-
</p>
94-
95-
<div class="controls">
96-
<button onclick="startAnimation()">Start</button>
97-
<button onclick="stopAnimation()">Stop</button>
98-
<button onclick="resetAnimation()">Reset</button>
99-
</div>
100-
101-
<div id="box"></div>
102-
103-
104-
<script>
105-
let box = document.getElementById("box");
106-
let pos = 0;
107-
let animation;
108-
109-
function startAnimation() {
110-
clearInterval(animation);
111-
animation = setInterval(function () {
112-
if (pos >= 300) {
113-
clearInterval(animation);
114-
} else {
115-
pos += 2;
116-
box.style.left = pos + "px";
117-
}
118-
}, 20);
119-
}
120-
121-
function stopAnimation() {
122-
clearInterval(animation);
123-
}
124-
125-
function resetAnimation() {
126-
clearInterval(animation);
127-
pos = 0;
128-
box.style.left = "0px";
129-
}
130-
131-
function copyCode() {
132-
const code = document.getElementById("codeBlock").innerText;
133-
navigator.clipboard.writeText(code).then(function () {
134-
alert("Code copied to clipboard!");
135-
}, function (err) {
136-
alert("Failed to copy code.");
137-
});
138-
}
139-
</script>
92+
<h2>JavaScript Animation Using <code>setInterval()</code></h2>
93+
94+
<p>
95+
This example demonstrates how to animate a box using JavaScript timers. The box moves horizontally using
96+
<code>setInterval</code>, and you can control the animation with start, stop, and reset buttons.
97+
</p>
98+
99+
<div class="controls">
100+
<button onclick="startAnimation()">Start</button>
101+
<button onclick="stopAnimation()">Stop</button>
102+
<button onclick="resetAnimation()">Reset</button>
103+
</div>
104+
105+
<div id="box"></div>
106+
107+
108+
<script>
109+
let box = document.getElementById("box");
110+
let pos = 0;
111+
let animation;
112+
113+
function startAnimation() {
114+
clearInterval(animation);
115+
animation = setInterval(function () {
116+
if (pos >= 300) {
117+
clearInterval(animation);
118+
} else {
119+
pos += 2;
120+
box.style.left = pos + "px";
121+
}
122+
}, 20);
123+
}
124+
125+
function stopAnimation() {
126+
clearInterval(animation);
127+
}
128+
129+
function resetAnimation() {
130+
clearInterval(animation);
131+
pos = 0;
132+
box.style.left = "0px";
133+
}
134+
135+
function copyCode() {
136+
const code = document.getElementById("codeBlock").innerText;
137+
navigator.clipboard.writeText(code).then(function () {
138+
alert("Code copied to clipboard!");
139+
}, function (err) {
140+
alert("Failed to copy code.");
141+
});
142+
}
143+
</script>
140144

141145
</body>
142-
</html>
146+
147+
</html>

0 commit comments

Comments
 (0)