Advertisement
shuvocse

As browsers automatically detect the hash and take you to th

Dec 10th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // to top right away
  2. if ( window.location.hash ) scroll(0,0);
  3. // void some browsers issue
  4. setTimeout( function() { scroll(0,0); }, 1);
  5.  
  6. $(function() {
  7.  
  8.     // your current click function
  9.     $('.scroll').on('click', function(e) {
  10.         e.preventDefault();
  11.         $('html, body').animate({
  12.             scrollTop: $($(this).attr('href')).offset().top + 'px'
  13.         }, 1000, 'swing');
  14.     });
  15.  
  16.     // *only* if we have anchor on the url
  17.     if(window.location.hash) {
  18.  
  19.         // smooth scroll to the anchor id
  20.         $('html, body').animate({
  21.             scrollTop: $(window.location.hash).offset().top + 'px'
  22.         }, 1000, 'swing');
  23.     }
  24.  
  25. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement