|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <title>JavaScript Animation Example</title> |
| 6 | + <style> |
| 7 | + body { |
| 8 | + font-family: 'Georgia', serif; |
| 9 | + line-height: 1.7; |
| 10 | + padding: 20px; |
| 11 | + background-color: #fcfcfc; |
| 12 | + color: #2c2c2c; |
| 13 | + } |
| 14 | + |
| 15 | + h2, h3 { |
| 16 | + color: #1a237e; |
| 17 | + } |
| 18 | + |
| 19 | + #box { |
| 20 | + width: 50px; |
| 21 | + height: 50px; |
| 22 | + background-color: royalblue; |
| 23 | + position: relative; |
| 24 | + left: 0; |
| 25 | + top: 0; |
| 26 | + z-index: 1000; |
| 27 | + } |
| 28 | + |
| 29 | + .controls { |
| 30 | + margin: 20px 0; |
| 31 | + } |
| 32 | + |
| 33 | + button { |
| 34 | + margin: 10px 10px 10px 0; |
| 35 | + padding: 8px 16px; |
| 36 | + font-size: 16px; |
| 37 | + border: none; |
| 38 | + background-color: #3949ab; |
| 39 | + color: white; |
| 40 | + cursor: pointer; |
| 41 | + border-radius: 4px; |
| 42 | + font-family: 'Georgia', serif; |
| 43 | + } |
| 44 | + |
| 45 | + button:hover { |
| 46 | + background-color: #303f9f; |
| 47 | + } |
| 48 | + |
| 49 | + .code-container { |
| 50 | + position: relative; |
| 51 | + margin-top: 20px; |
| 52 | + } |
| 53 | + |
| 54 | + pre { |
| 55 | + background-color: #f4f4f4; |
| 56 | + color: #333; |
| 57 | + padding: 15px; |
| 58 | + border-left: 4px solid #3949ab; |
| 59 | + border-radius: 4px; |
| 60 | + overflow-x: auto; |
| 61 | + white-space: pre-wrap; |
| 62 | + } |
| 63 | + |
| 64 | + code { |
| 65 | + font-family: 'Courier New', monospace; |
| 66 | + font-size: 15px; |
| 67 | + } |
| 68 | + |
| 69 | + .copy-btn { |
| 70 | + position: absolute; |
| 71 | + top: 10px; |
| 72 | + right: 10px; |
| 73 | + background-color: #1a237e; |
| 74 | + color: #fff; |
| 75 | + padding: 6px 12px; |
| 76 | + font-size: 14px; |
| 77 | + border: none; |
| 78 | + border-radius: 3px; |
| 79 | + cursor: pointer; |
| 80 | + } |
| 81 | + |
| 82 | + .copy-btn:hover { |
| 83 | + background-color: #0d1548; |
| 84 | + } |
| 85 | + </style> |
| 86 | +</head> |
| 87 | +<body> |
| 88 | + |
| 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> |
| 140 | + |
| 141 | +</body> |
| 142 | +</html> |
0 commit comments