Last active
August 29, 2015 14:16
-
-
Save codfish/37649196ea42a528c269 to your computer and use it in GitHub Desktop.
Simple jQuery snippet for a "scroll to" animation. Nice replacement for anchor links, rather than abruptly jumping to that spot on the page, this will scroll the page nicely to the desired anchor.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* jQuery animated scroll to | |
* | |
* @example | |
* <a class="animated-scroll" data-target=".elem-selector-scroll-to" data-offset="60">Go to top</a> | |
* | |
* @param {data-target} the jquery selector of the dom element you want to scroll to | |
* @param {data-offset} how far from the top of the window you want the element to be (in pixels). Default is 20px. | |
*/ | |
$(document).on('click', '.animated-scroll', function(e) { | |
e.preventDefault(); | |
var $this = $(this), | |
elementClicked = $this.data('target'), | |
destination = $(elementClicked).offset().top, | |
offset = $this.data('offset') || 20; | |
$('html:not(:animated), body:not(:animated)').animate({ | |
scrollTop: destination - offset | |
}, 500); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment