Created
October 8, 2020 21:39
-
-
Save behnammodi/480e17bfbc8a723f29db922ebf19127a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<style> | |
#box { | |
width: 100px; | |
height: 100px; | |
background-color: lightseagreen; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="box"></div> | |
</body> | |
<script> | |
const box = document.querySelector("#box"); | |
let x = 0; | |
let id = null; | |
let firstX = 0; | |
let start = false; | |
function easeOutQuint(x) { | |
const c1 = 1.70158; | |
const c2 = c1 * 1.525; | |
return x < 0.5 | |
? (Math.pow(2 * x, 2) * ((c2 + 1) * 2 * x - c2)) / 2 | |
: (Math.pow(2 * x - 2, 2) * ((c2 + 1) * (x * 2 - 2) + c2) + 2) / 2; | |
} | |
const a = 0; | |
window.addEventListener("mousedown", (event) => { | |
firstX = event.x; | |
start = true; | |
requestAnimationFrame(obs); | |
}); | |
window.addEventListener("mousemove", (event) => { | |
if (start) { | |
if (event.x <= 1000) { | |
x = | |
event.x * easeOutQuint(event.x / 1000) - | |
firstX * easeOutQuint(firstX / 1000); | |
} else { | |
start = false; | |
cancelAnimationFrame(id); | |
} | |
} | |
}); | |
window.addEventListener("mouseup", (event) => { | |
start = false; | |
cancelAnimationFrame(id); | |
}); | |
function obs(time) { | |
console.log(time); | |
box.style.transform = `translateX(${x}px)`; | |
id = requestAnimationFrame(obs); | |
} | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment