Guest User

Untitled

a guest
Jun 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. adjustScroll = function () {
  2. // Sticky nav selector (you'll have to provide your own selector)
  3. const nav = document.querySelector('header>nav');
  4.  
  5. if (location.href.indexOf("#") >= 0) {
  6. // Find the name of the anchor
  7. let n = location.href.substr(location.href.indexOf("#")+1);
  8.  
  9. // Find the anchor by name, if it exists
  10. let a = document.querySelector('a[name="'+n+'"]');
  11. if (!a) {
  12. return;
  13. }
  14.  
  15. // Set y value as y-value of the anchor, offset by the header height
  16. let y = a.offsetTop;
  17. y -= nav.height + 10;
  18.  
  19. // Scroll to the y position
  20. window.scrollTo(0, y);
  21. }
  22. }
  23.  
  24. // Call it wherever you need to call it
  25. adjustScroll();
Add Comment
Please, Sign In to add comment