Advertisement
Guest User

Nicepage smoothscroll & back to top

a guest
Jul 5th, 2019
938
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 0.86 KB | None | 0 0
  1. <script>
  2. window.onload = function() {
  3.  
  4. ///////////////////
  5. // smooth scroll //
  6. ///////////////////
  7.  
  8.     document.body.addEventListener('click', e => {
  9.      
  10.       const href = e.target.href;
  11.       if (!href) return;
  12.      
  13.       const id = href.split('#').pop();
  14.       const target = document.getElementById(id);
  15.  
  16.       if (!target) return;
  17.       e.preventDefault();
  18.       history.pushState({}, document.title, href);
  19.       target.scrollIntoView({
  20.         behavior: 'smooth',
  21.         block: 'start'
  22.       });
  23.  
  24.     });
  25.  
  26. /////////////////
  27. // back to top //
  28. /////////////////
  29.  
  30.     var btn = $('#bttbutton');
  31.  
  32.     $(window).scroll(function() {
  33.       if ($(window).scrollTop() > 300) {
  34.         btn.addClass('show');
  35.       } else {
  36.         btn.removeClass('show');
  37.       }
  38.     });
  39.  
  40.     btn.on('click', function(e) {
  41.       e.preventDefault();
  42.       $('html, body').animate({scrollTop:0}, '300');
  43.     });
  44.  
  45.  
  46. }
  47. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement