Created
November 30, 2023 12:21
-
-
Save javiereguiluz/d6dfac1572abdd2fa30d76514a2d4de4 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
<script> | |
document.addEventListener('DOMContentLoaded', function() { | |
const card = document.querySelector('#intro-image-wrapper'); | |
const motionMatchMedia = window.matchMedia('(prefers-reduced-motion)'); | |
const buffer = 10; | |
function handleHover(e) { | |
const { clientX, clientY, currentTarget } = e; | |
const { clientWidth, clientHeight, offsetLeft, offsetTop } = currentTarget; | |
const horizontalRatio = (clientX - offsetLeft) / clientWidth; | |
const verticalRatio = (clientY - offsetTop) / clientHeight; | |
const xRotation = (buffer / 2 - horizontalRatio * buffer).toFixed(2); | |
const yRotation = (verticalRatio * buffer - buffer / 2).toFixed(2); | |
card.style.animation = 'none'; | |
card.style.transform = `perspective(${clientWidth}px) rotateX(${yRotation}deg) rotateY(${xRotation}deg) scale3d(1, 1, 1)`; | |
} | |
function resetStyles(e) { | |
card.style.transform = `perspective(${e.currentTarget.clientWidth}px) rotateX(0deg) rotateY(0deg)`; | |
card.style.animation = 'rotateAnimation 4.5s linear infinite'; | |
} | |
if (!motionMatchMedia.matches) { | |
card.addEventListener('mousemove', handleHover); | |
card.addEventListener('mouseleave', resetStyles); | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment