Skip to content

Instantly share code, notes, and snippets.

@beovulf
Created February 9, 2016 11:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beovulf/27d69e5c88cd8b1dbd03 to your computer and use it in GitHub Desktop.
Save beovulf/27d69e5c88cd8b1dbd03 to your computer and use it in GitHub Desktop.
smooth scroll with links to other pages offset
// Smooth Scroll
$(function() {
$('a[href*="#"]:not([href="#"])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html, body').animate({
scrollTop: target.offset().top-100
}, 1000);
return false;
}
}
});
});
// Links to other pages offset
if ( window.location.hash ) {
var hash = window.location.hash,
$hash = $(hash).children('div:first-of-type');
$hash.parent().removeAttr('id');
$hash.before('<div id="'+hash.slice(1)+'" class="hashlink"></div>');
window.location.hash = hash;
}
});
// CSS to links to other pages
.hashlink {
height: 100px;
width: 100%;
visibility: hidden;
position: absolute;
top:-100px;
left:0;
z-index:1;
}
// Remember to add a relative positioning to parent container
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment