-
-
Save anteprimorac/d20e5ec53afd21d163a5 to your computer and use it in GitHub Desktop.
This file contains 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
;( function( $ ) { | |
var $background, | |
mouse = { | |
x: 0, // Current x-axis position of mouse | |
y: 0, // Current y-axis position of mouse | |
lx: -1, // Last iteration x-axis position of mouse | |
ly: -1, // Last iteration y-axis position of mouse | |
}; | |
$( document ).on( 'mousemove', function( event ) { | |
mouse.x = event.clientX || event.pageX; | |
mouse.y = event.clientY || event.pageY; | |
} ); | |
var mouse_loop = function() { | |
// Avoid calculations if not needed | |
if ( mouse.x == mouse.lx && mouse.y == mouse.ly ) { | |
rAF( mouse_loop ); | |
return false; | |
} | |
else { | |
mouse.lx = mouse.x; | |
mouse.ly = mouse.y; | |
} | |
$background.css( { | |
'transform': 'translate( ' + ( ( $( window ).width() / 2 - mouse.lx ) / $( window ).width() * 100 ) + 'px, ' + ( ( $( window ).height() / 2 - mouse.ly ) / $( window ).height() * 100 ) + 'px )' | |
} ); | |
rAF( mouse_loop ); | |
}; | |
$( document ).on( 'ready', function() { | |
$background = $( '.background-wrapper' ); | |
mouse_loop(); | |
} ); | |
} ) ( jQuery ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment