Skip to content

Commit 8bde318

Browse files
author
highflyer910
committed
moveElement.js
1 parent 79ff6e8 commit 8bde318

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

moveElement.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function moveElement(duration, distance, element){
2+
const start = performance.now();
3+
4+
function move(currentTime){
5+
const elapsed = currentTime - start;
6+
const progress = elapsed / duration;
7+
const amountToMove = progress * distance;
8+
9+
element.style.transform = `translateX(${ amountToMove }px)`;
10+
11+
if (amountToMove < distance){
12+
requestAnimationFrame(move);
13+
}
14+
}
15+
16+
requestAnimationFrame(move);
17+
}

0 commit comments

Comments
 (0)