Guest User

Untitled

a guest
Nov 24th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. // Select all links with hashes
  2. $('a.YOUR_CLASS_NAME')
  3. .click(function(event) {
  4. // On-page links
  5. if (
  6. location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '')
  7. &&
  8. location.hostname == this.hostname
  9. ) {
  10. // Figure out element to scroll to
  11. var target = $(this.hash);
  12. target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
  13. // Does a scroll target exist?
  14. if (target.length) {
  15. // Only prevent default if animation is actually gonna happen
  16. event.preventDefault();
  17. $('html, body').animate({
  18. scrollTop: target.offset().top
  19. }, 1000, function() {
  20. // Callback after animation
  21. // Must change focus!
  22. var $target = $(target);
  23. $target.focus();
  24. if ($target.is(":focus")) { // Checking if the target was focused
  25. return false;
  26. } else {
  27. $target.attr('tabindex','-1'); // Adding tabindex for elements not focusable
  28. $target.focus(); // Set focus again
  29. };
  30. });
  31. }
  32. }
  33. });
Add Comment
Please, Sign In to add comment