Last active
June 14, 2017 19:42
-
-
Save joshapgar/6882561494f297f21d49aa5af5723bb6 to your computer and use it in GitHub Desktop.
ScrollTo With Checks
This file contains hidden or 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
// | |
// -------------------------------------------------------------------------- | |
// 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