Is it possible to create a rotating effect on a div element based on the position of the mouse pointer?
I envision the div displaying <-
when the mouse is on the left side, and ->
when the mouse moves to the right, regardless of direction.
If anyone has a solution for this dynamic rotation effect, I would greatly appreciate any help!
EDIT:
<style>
body {
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
}
</style>
<div id="arrow">></div>
<script type="text/javascript">
var arrow = document.querySelector("#arrow");
var arrowRects = arrow.getBoundingClientRect();
var arrowX = arrowRects.left + arrowRects.width / 2;
var arrowY = arrowRects.top + arrowRects.height / 2;
addEventListener("mousemove", function(event) {
arrow.style.transform = "rotate(" + Math.atan2(event.y - arrowY, event.x - arrowX) + "rad)";
});
</script>