Created
September 28, 2013 11:46
-
-
Save psflannery/6741289 to your computer and use it in GitHub Desktop.
swap images as the mouse moves around the screen...
This file contains hidden or 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($){ | |
$.fn.bgMouse = function(argOptions) { | |
var FILENAME = argOptions.folder; | |
var FILE_COUNT = argOptions.count; | |
return this.each(function(index, theContainer) { | |
var $container = $(theContainer); | |
// Add zeros | |
lpad = function(s, width, char) { | |
return (s.length >= width) ? s : (new Array(width).join(char) + s).slice(-width); | |
} | |
// Prepare array with images | |
var imgFilenames = []; | |
for (var i=0; i<FILE_COUNT; i++) { | |
imgFilenames.push(FILENAME + lpad(i, 4, '0') + '.jpg'); | |
} | |
// Method to preload images | |
var preloadImages = function(arr){ | |
var newimages=[], loadedimages=0 | |
var postaction=function(){} | |
var arr=(typeof arr!="object")? [arr] : arr | |
function imageloadpost(){ | |
loadedimages++ | |
if (loadedimages==arr.length){ | |
postaction(newimages) //call postaction and pass in newimages array as parameter | |
} | |
} | |
for (var i=0; i<arr.length; i++){ | |
newimages[i]=new Image() | |
newimages[i].src=arr[i] | |
newimages[i].onload=function(){ | |
imageloadpost() | |
} | |
newimages[i].onerror=function(){ | |
imageloadpost() | |
} | |
} | |
return { //return blank object with done() method | |
done:function(f){ | |
postaction=f || postaction //remember user defined callback functions to be called when images load | |
} | |
} | |
} | |
// Method to set BG | |
var setBackground = function(theImages, theIndex) { | |
$container.css('backgroundImage', 'url(' + theImages[theIndex].src + ')'); | |
} | |
// Preloading done | |
preloadImages(imgFilenames).done(function(theImages){ | |
// By default, set to first img | |
setBackground(theImages, 0); | |
// Mouse position | |
$(window).mousemove(function(theEvent) { | |
var windowWidth = $(window).width(); | |
var mouseX = theEvent.pageX; | |
var imgIndex = Math.min(Math.round(mouseX / windowWidth * FILE_COUNT), FILE_COUNT-1); | |
// Set background image | |
setBackground(theImages, imgIndex); | |
}); | |
}); | |
}); | |
} | |
})( jQuery ); |
This file contains hidden or 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
$('div.bg-animation').bgMouse({ | |
folder: "/assets/burger-animation/burger_", | |
count: 78 | |
}); | |
/* <div class="bg-animation"></div> */ | |
/* background-image: url(http://therewillbeburgers.com/assets/burger-animation/burger_0055.jpg); | |
}*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment