Skip to content

Instantly share code, notes, and snippets.

@rohitpaulk
Created May 12, 2014 21:14
Show Gist options
  • Select an option

  • Save rohitpaulk/4c643fb20674aaedbf1a to your computer and use it in GitHub Desktop.

Select an option

Save rohitpaulk/4c643fb20674aaedbf1a to your computer and use it in GitHub Desktop.
jQuery Background Panning
$('.panbg').mousemove(function(e){
//Let's find the midpoints of the screen
var midpointX = $(window).width()/2
var midpointY = $(window).height()/2
//Now the pointer's deviation as a percentage.
var mousePosX = ((e.pageX-midpointX)/$(window).width())*100;
var mousePosY = ((e.pageY-midpointY)/$(window).height())*100;
//Let's dilute it by a factor of 40
var xPos = mousePosX/40 + 50;
var yPos = mousePosY/40 + 50;
//Finally, let's move the background :)
$('.panbg').css('backgroundPosition', xPos+'% '+ yPos+'%');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment