Created
June 14, 2018 00:50
-
-
Save muks999/e466ed3235ac13a480421f669c90e296 to your computer and use it in GitHub Desktop.
Scroll to Element
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('.scroll-link') | |
.not('[href="#"]') | |
.not('[href="#0"]') | |
.click(function (event) { | |
if ( | |
location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') | |
&& | |
location.hostname == this.hostname | |
) { | |
var target = $(this.hash); | |
target = target.length ? target : $('[name=' + this.hash.slice(1) + ']'); | |
if (target.length) { | |
event.preventDefault(); | |
$('html, body').animate({ | |
scrollTop: target.offset().top - 50 | |
}, 1000, function () { | |
var $target = $(target); | |
$target.focus(); | |
if ($target.is(":focus")) { | |
return false; | |
} else { | |
$target.attr('tabindex', '-1'); | |
$target.focus(); | |
} | |
; | |
}); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment