Skip to content

Instantly share code, notes, and snippets.

@donut
Created February 22, 2012 16:32
Show Gist options
  • Save donut/1885908 to your computer and use it in GitHub Desktop.
Save donut/1885908 to your computer and use it in GitHub Desktop.
A scroll to jQuery plugin
/** A "scroll to" plugin.
Scrolls to the first element in the jQuery object or to the passed
position.
@author Donovan Mueller ([email protected])
@url https://gist.github.com/1885908
------------------------------------------------------------------------ */
;(function donutScrollPlugin($) {
var viewport = ($.browser.opera) ? 'html' : 'html, body'
// Opera jumps to the top when 'html,body' is used.
$.fn.donutScroll = function(y, callback)
{
var docheight = $(document).height()
, winheight = $(window).height();
if (typeof y === 'undefined' || y === null) y = this.offset().top;
if (y < 0) y = 0;
// check that we aren't trying to scroll past what is visible
// otherwise the animation will be cut short and wont be as pretty
if ((docheight - y) < winheight) {
y = docheight - winheight;
}
// Wheeeeee!
$(viewport).stop().animate( {scrollTop: y}
, { duration: 600
, easing: 'easeOutExpo'
, complete: callback } );
}; // $.fn.donutScroll()
})(jQuery); // (donutScrollPlugin)()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment