From 1d8b8131e16d7871304375cc37b6eb9a559941de Mon Sep 17 00:00:00 2001 From: dharapj <30487321+dharapj@users.noreply.github.com> Date: 2025年1月21日 17:59:28 +0530 Subject: [PATCH] Update README.md Added definition and code for requestAnimationFrame question --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index cd7fe811..f346b7cd 100644 --- a/README.md +++ b/README.md @@ -8842,6 +8842,23 @@ The execution context is created when a function is called. The function's code **[⬆ Back to Top](#table-of-contents)** 466. ### What is the purpose of requestAnimationFrame method? +The requestAnimationFrame() method in JavaScript is used to schedule a function to be called before the next repaint of the browser window, allowing you to create smooth, efficient animations. It's primarily used for animations and visual updates, making it an essential tool for improving performance when you're animating elements on the web. +```javascript +const element = document.getElementById("myElement"); +function animate() { + let currentPosition = parseInt(window.getComputedStyle(element).left, 10); + + // Move the element 2px per frame + currentPosition += 2; + element.style.left = currentPosition + 'px'; + // If the element hasn't moved off-screen, request the next frame + if (currentPosition < window.innerWidth) { + requestAnimationFrame(animate); + } +} +// Start the animation +requestAnimationFrame(animate); +``` **[⬆ Back to Top](#table-of-contents)**

AltStyle によって変換されたページ (->オリジナル) /