Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- adjustScroll = function () {
- // Sticky nav selector (you'll have to provide your own selector)
- const nav = document.querySelector('header>nav');
- if (location.href.indexOf("#") >= 0) {
- // Find the name of the anchor
- let n = location.href.substr(location.href.indexOf("#")+1);
- // Find the anchor by name, if it exists
- let a = document.querySelector('a[name="'+n+'"]');
- if (!a) {
- return;
- }
- // Set y value as y-value of the anchor, offset by the header height
- let y = a.offsetTop;
- y -= nav.height + 10;
- // Scroll to the y position
- window.scrollTo(0, y);
- }
- }
- // Call it wherever you need to call it
- adjustScroll();
Add Comment
Please, Sign In to add comment