Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
;( 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
You can’t perform that action at this time.