Skip to content

Instantly share code, notes, and snippets.

@joshapgar
Last active June 14, 2017 19:42
Show Gist options
  • Save joshapgar/6882561494f297f21d49aa5af5723bb6 to your computer and use it in GitHub Desktop.
Save joshapgar/6882561494f297f21d49aa5af5723bb6 to your computer and use it in GitHub Desktop.
ScrollTo With Checks
//
// --------------------------------------------------------------------------
// To Top Function
// --------------------------------------------------------------------------
//
function toTop(elementClass) {
var chapterToTop = $(elementClass).offset();
chapterToTop = Math.round(chapterToTop.top);
var windowScrollPosition = $(document).scrollTop();
windowScrollPosition = windowScrollPosition + 120;
var page = $("html, body");
if (windowScrollPosition !== chapterToTop) {
page.on("scroll mousedown wheel DOMMouseScroll mousewheel keyup touchmove", function(){
page.stop();
});
page.animate({ scrollTop: $(elementClass).offset().top - 120 }, 2000, function(){
page.off("scroll mousedown wheel DOMMouseScroll mousewheel keyup touchmove");
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment