Skip to content

Instantly share code, notes, and snippets.

@wireinet
Created July 1, 2019 08:55
Show Gist options
  • Save wireinet/ea8d4f28fb6a118ba6e65819741f2c2a to your computer and use it in GitHub Desktop.
Save wireinet/ea8d4f28fb6a118ba6e65819741f2c2a to your computer and use it in GitHub Desktop.
$(function(){
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
}, 2000, 'swing');
hashTagActive = this.hash;
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment