Skip to content

Instantly share code, notes, and snippets.

@anteprimorac
Created January 23, 2015 15:18
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 anteprimorac/d20e5ec53afd21d163a5 to your computer and use it in GitHub Desktop.
Save anteprimorac/d20e5ec53afd21d163a5 to your computer and use it in GitHub Desktop.
;( 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