Created
February 9, 2016 11:07
-
-
Save beovulf/27d69e5c88cd8b1dbd03 to your computer and use it in GitHub Desktop.
smooth scroll with links to other pages offset
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
// 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