Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. <script type="text/javascript">
  2. $(document).ready(function(){
  3.  
  4. //regExp for current domain (http://example.com)
  5. var domainRegExp = new RegExp(window.location.hostname, ‘’);
  6.  
  7. //regExp for relative link to current domain (/page - relative to current domain)
  8. var relativeRegExp = new RegExp(‘^\/[^\/]’, ‘’);
  9.  
  10. // Add smooth scrolling to all links
  11. $("a").on('click', function(event) {
  12.  
  13. // Make sure this.hash has a value before overriding default behavior
  14. if (this.hash !== "") {
  15.  
  16. //Prevent anchor click behavior
  17. if (!domainRegExp.test(this.href) && !relativeRegExp.test(this.href)) {
  18. return;
  19. }
  20.  
  21. // Prevent default anchor click behavior
  22. event.preventDefault();
  23.  
  24. // Store hash
  25. var hash = this.hash;
  26.  
  27. // Using jQuery's animate() method to add smooth page scroll
  28. // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
  29. $('html, body').animate({
  30. scrollTop: $(hash).offset().top
  31. }, 800, function(){
  32.  
  33. // Add hash (#) to URL when done scrolling (default click behavior)
  34. window.location.hash = hash;
  35. });
  36. } // End if
  37. });
  38. });
  39. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement