Skip to content

Instantly share code, notes, and snippets.

@johanmendezb
Created June 25, 2018 15:08
Show Gist options
  • Save johanmendezb/00d2a3d5cd1fe4e5597e6e3f36dfc383 to your computer and use it in GitHub Desktop.
Save johanmendezb/00d2a3d5cd1fe4e5597e6e3f36dfc383 to your computer and use it in GitHub Desktop.
Simple snipet to add smooth scroll to href's target
$(document).ready(function () {
$('a[href*="#"]')
.not('[href="#"]')
.click(function (e) {
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) {
e.preventDefault()
$('html, body').animate({
scrollTop: target.offset().top
}, 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