Skip to content

Instantly share code, notes, and snippets.

@javiereguiluz
Created November 30, 2023 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save javiereguiluz/d6dfac1572abdd2fa30d76514a2d4de4 to your computer and use it in GitHub Desktop.
Save javiereguiluz/d6dfac1572abdd2fa30d76514a2d4de4 to your computer and use it in GitHub Desktop.
<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