/* plugin: jquery parallax version 1.1.3 author: ian lunn twitter: @ianlunn author url: http://www.ianlunn.co.uk/ plugin url: http://www.ianlunn.co.uk/plugins/jquery-parallax/ dual licensed under the mit and gpl licenses: http://www.opensource.org/licenses/mit-license.php http://www.gnu.org/licenses/gpl.html */ (function( jquery ){ var jquerywindow = jquery(window); var windowheight = jquerywindow.height(); jquerywindow.resize(function () { windowheight = jquerywindow.height(); }); jquery.fn.parallax = function(xpos, speedfactor, outerheight) { var jquerythis = jquery(this); var getheight; var firsttop; var paddingtop = 0; //get the starting position of each element to have parallax applied to it jquerythis.each(function(){ firsttop = jquerythis.offset().top; }); if (outerheight) { getheight = function(jqo) { return jqo.outerheight(true); }; } else { getheight = function(jqo) { return jqo.height(); }; } // setup defaults if arguments aren't specified if (arguments.length < 1 || xpos === null) xpos = "50%"; if (arguments.length < 2 || speedfactor === null) speedfactor = 0.1; if (arguments.length < 3 || outerheight === null) outerheight = true; // function to be called whenever the window is scrolled or resized function update(){ var pos = jquerywindow.scrolltop(); jquerythis.each(function(){ var jqueryelement = jquery(this); var top = jqueryelement.offset().top; var height = getheight(jqueryelement); // check if totally above or totally below viewport if (top + height < pos || top > pos + windowheight) { return; } jquerythis.css('backgroundposition', xpos + " " + math.round((top - pos) * speedfactor) + "px"); }); } jquerywindow.bind('scroll', update).resize(update); update(); }; })(jquery);