/** * jquery.scrollfollow.js * copyright (c) 2008 net perspective (http://kitchen.net-perspective.com/) * licensed under the mit license (http://www.opensource.org/licenses/mit-license.php) * * @author r.a. ray * * @projectdescription jquery plugin for allowing an element to animate down as the user scrolls the page. * * @version 0.4.0 * * @requires jquery.js (tested with 1.2.6) * @requires ui.core.js (tested with 1.5.2) * * @optional jquery.cookie.js (http://www.stilbuero.de/2006/09/17/cookie-plugin-for-jquery/) * @optional jquery.easing.js (http://gsgd.co.uk/sandbox/jquery/easing/ - tested with 1.3) * * @param speed int - duration of animation (in milliseconds) * default: 500 * @param offset int - number of pixels box should remain from top of viewport * default: 0 * @param easing string - any one of the easing options from the easing plugin - requires jquery easing plugin < http://gsgd.co.uk/sandbox/jquery/easing/ > * default: 'linear' * @param container string - id of the containing div * default: box's immediate parent * @param killswitch string - id of the on/off toggle element * default: 'killswitch' * @param ontext string - killswitch text to be displayed if sliding is enabled * default: 'turn slide off' * @param offtext string - killswitch text to be displayed if sliding is disabled * default: 'turn slide on' * @param relativeto string - scroll animation can be relative to either the 'top' or 'bottom' of the viewport * default: 'top' * @param delay int - time between the end of the scroll and the beginning of the animation in milliseconds * default: 0 */ ( function( $ ) { $.scrollfollow = function ( box, options ) { // convert box into a jquery object box = $( box ); // 'box' is the object to be animated var position = box.css( 'position' ); function ani() { // the script runs on every scroll which really means many times during a scroll. // we don't want multiple slides to queue up. box.queue( [ ] ); // a bunch of values we need to determine where to animate to var viewportheight = parseint( $( window ).height() ); var pagescroll = parseint( $( document ).scrolltop() ); var parenttop = parseint( box.cont.offset().top ); var parentheight = parseint( box.cont.attr( 'offsetheight' ) ); var boxheight = parseint( box.attr( 'offsetheight' ) + ( parseint( box.css( 'margintop' ) ) || 0 ) + ( parseint( box.css( 'marginbottom' ) ) || 0 ) ); var anitop; // make sure the user wants the animation to happen if ( isactive ) { // if the box should animate relative to the top of the window if ( options.relativeto == 'top' ) { // don't animate until the top of the window is close enough to the top of the box if ( box.initialoffsettop >= ( pagescroll + options.offset ) ) { anitop = box.initialtop; } else { anitop = math.min( ( math.max( ( -parenttop ), ( pagescroll - box.initialoffsettop + box.initialtop ) ) + options.offset ), ( parentheight - boxheight - box.paddingadjustment ) ); } } // if the box should animate relative to the bottom of the window else if ( options.relativeto == 'bottom' ) { // don't animate until the bottom of the window is close enough to the bottom of the box if ( ( box.initialoffsettop + boxheight ) >= ( pagescroll + options.offset + viewportheight ) ) { anitop = box.initialtop; } else { anitop = math.min( ( pagescroll + viewportheight - boxheight - options.offset ), ( parentheight - boxheight ) ); } } // checks to see if the relevant scroll was the last one // "-20" is to account for inaccuracy in the timeout if ( ( new date().gettime() - box.lastscroll ) >= ( options.delay - 20 ) ) { box.animate( { top: anitop }, options.speed, options.easing ); } } }; // for user-initiated stopping of the slide var isactive = true; if ( $.cookie != undefined ) { if( $.cookie( 'scrollfollowsetting' + box.attr( 'id' ) ) == 'false' ) { var isactive = false; $( '#' + options.killswitch ).text( options.offtext ) .toggle( function () { isactive = true; $( this ).text( options.ontext ); $.cookie( 'scrollfollowsetting' + box.attr( 'id' ), true, { expires: 365, path: '/'} ); ani(); }, function () { isactive = false; $( this ).text( options.offtext ); box.animate( { top: box.initialtop }, options.speed, options.easing ); $.cookie( 'scrollfollowsetting' + box.attr( 'id' ), false, { expires: 365, path: '/'} ); } ); } else { $( '#' + options.killswitch ).text( options.ontext ) .toggle( function () { isactive = false; $( this ).text( options.offtext ); box.animate( { top: box.initialtop }, 0 ); $.cookie( 'scrollfollowsetting' + box.attr( 'id' ), false, { expires: 365, path: '/'} ); }, function () { isactive = true; $( this ).text( options.ontext ); $.cookie( 'scrollfollowsetting' + box.attr( 'id' ), true, { expires: 365, path: '/'} ); ani(); } ); } } // if no parent id was specified, and the immediate parent does not have an id // options.container will be undefined. so we need to figure out the parent element. if ( options.container == '') { box.cont = box.parent(); } else { box.cont = $( '#' + options.container ); } // finds the default positioning of the box. box.initialoffsettop = parseint( box.offset().top ); box.initialtop = parseint( box.css( 'top' ) ) || 0; // hack to fix different treatment of boxes positioned 'absolute' and 'relative' if ( box.css( 'position' ) == 'relative' ) { box.paddingadjustment = parseint( box.cont.css( 'paddingtop' ) ) + parseint( box.cont.css( 'paddingbottom' ) ); } else { box.paddingadjustment = 0; } // animate the box when the page is scrolled $( window ).scroll( function () { // sets up the delay of the animation $.fn.scrollfollow.interval = settimeout( function(){ ani();} , options.delay ); // to check against right before setting the animation box.lastscroll = new date().gettime(); } ); // animate the box when the page is resized $( window ).resize( function () { // sets up the delay of the animation $.fn.scrollfollow.interval = settimeout( function(){ ani();} , options.delay ); // to check against right before setting the animation box.lastscroll = new date().gettime(); } ); // run an initial animation on page load box.lastscroll = 0; ani(); }; $.fn.scrollfollow = function ( options ) { options = options || {}; options.relativeto = options.relativeto || 'top'; options.speed = options.speed || 500; options.offset = options.offset || 0; options.easing = options.easing || 'swing'; options.container = options.container || this.parent().attr( 'id' ); options.killswitch = options.killswitch || 'killswitch'; options.ontext = options.ontext || 'turn slide off'; options.offtext = options.offtext || 'turn slide on'; options.delay = options.delay || 0; this.each( function() { new $.scrollfollow( this, options ); } ); return this; }; })( jquery );