Skip to content

Instantly share code, notes, and snippets.

@ocmca
Last active August 29, 2015 14:20
Show Gist options
  • Save ocmca/e32f061167652e39409d to your computer and use it in GitHub Desktop.
Save ocmca/e32f061167652e39409d to your computer and use it in GitHub Desktop.
Function takes in an element to find the top of it's position, also the speed (in milliseconds) at which the scroll should occur. Defaults to the top of the document at a "slow" tempo.
var scrollToTop = function(el, speed) {
/**
* Function takes in the element to scroll to, as well as
* the speed in which the document should scroll animate to the top.
*
* Default is window top, and slow (respectively). Time (in milliseconds is allowed)
*/
"use strict";
var position;
if(el === undefined){
position = 0;
} else {
position = $(el).offset().top;
}
if(speed === undefined) speed = "slow"
$("html, body").animate({ scrollTop: position }, speed);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment