Skip to content

Instantly share code, notes, and snippets.

@Spellhammer
Created April 20, 2022 14:16
Show Gist options
  • Save Spellhammer/625f9ba2063c383c783bb77cd32e0395 to your computer and use it in GitHub Desktop.
Save Spellhammer/625f9ba2063c383c783bb77cd32e0395 to your computer and use it in GitHub Desktop.
mouse.motion.parallax.effect.js
document.querySelectorAll('.mover').forEach( mover => {
if( window.angular ) return;
window.addEventListener('mousemove', (e) => {
if( !calculating ) {
var calculating = setTimeout( () => {
var divisor = mover.getAttribute('speed') ?? 32;
var windowMouseX = (e.clientX - (document.documentElement.clientWidth / 2));
var windowMouseY = (e.clientY - (document.documentElement.clientHeight / 2));
var computedCSS = '';
var translateX = (windowMouseX / divisor) * -1;
var translateY = (windowMouseY / divisor) * -1;
computedCSS += 'transform: translateX(' + translateX + 'px) translateY(' + translateY + 'px);';
mover.style.cssText = computedCSS;
clearTimeout(calculating);
}, 100);
}
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment