/* 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( $ ){ var $window = $(window); var windowheight = $window.height(); $window.resize(function () { windowheight = $window.height(); }); $.fn.parallax2 = function(xpos, speedfactor, outerheight) { var $this = $(this); var getheight; var firsttop; var paddingtop = 0; //get the starting position of each element to have parallax applied to it $this.each(function(){ firsttop = $this.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 = $window.scrolltop(); $this.each(function(){ var $element = $(this); var top = $element.offset().top; var height = getheight($element); // check if totally above or totally below viewport if (top + height < pos || top > pos + windowheight) { return; } $this.css('backgroundposition', xpos + " " + math.round((firsttop - pos) * speedfactor) + "px"); }); } $window.bind('scroll', update).resize(update); update(); }; })(jquery);