Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. let scrollTOId = function(scrollToId){
  2. // Figure out element to scroll to
  3. let target = $(scrollToId);
  4. console.log(target);
  5. // Does a scroll target exist?
  6. if (target.length) {
  7. // Only prevent default if animation is actually gonna happen
  8. event.preventDefault();
  9. $('html, body').animate({
  10. scrollTop: target.offset().top - 70
  11. }, 1000, function() {
  12. // Callback after animation
  13. // Must change focus!
  14. let $target = $(target);
  15. $target.focus();
  16. if ($target.is(":focus")) { // Checking if the target was focused
  17. return false;
  18. } else {
  19. $target.attr('tabindex','-1'); // Adding tabindex for elements not focusable
  20. $target.focus(); // Set focus again
  21. };
  22. });
  23. }
  24. }
  25.  
  26. //
  27. const scrollId = $('#element_Id');
  28. scrollTOId(scrollId);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement