Skip to content

Instantly share code, notes, and snippets.

@joviczarko
Created June 13, 2018 10:36
Show Gist options
  • Save joviczarko/3071c17a7c48f09dbb7cca25ff0d3501 to your computer and use it in GitHub Desktop.
Save joviczarko/3071c17a7c48f09dbb7cca25ff0d3501 to your computer and use it in GitHub Desktop.
Smooth scrolling to a hash plus a scroll class to scroll only those links
// Smooth scrolling to a hash (For links to work you need to add class .scroll)
var hashTagActive = "";
$(".scroll").on("click touchstart" , function (event) {
if(hashTagActive != this.hash) { //this will prevent if the user click several times the same link to freeze the scroll.
event.preventDefault();
//calculate destination place
var dest = 0;
if ($(this.hash).offset().top > $(document).height() - $(window).height()) {
dest = $(document).height() - $(window).height();
} else {
dest = $(this.hash).offset().top;
}
//go to destination
$('html,body').animate({
scrollTop: dest
}, 500, 'swing');
hashTagActive = this.hash;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment