Skip to content

Instantly share code, notes, and snippets.

@edele
Last active October 25, 2015 07:08
Show Gist options
  • Select an option

  • Save edele/085bd1fa7f9d585d5fbf to your computer and use it in GitHub Desktop.

Select an option

Save edele/085bd1fa7f9d585d5fbf to your computer and use it in GitHub Desktop.
function setHash (hash) {
hash = hash.replace(/^#/, '')
var node = $('#' + hash)
if (node.length) node.attr('id', '')
document.location.hash = hash
if (node.length) node.attr('id', hash)
hash = hash.replace(/^#/, '')
var fx
node = $('#' + hash)
if (node.length) {
node.attr('id', '')
fx = $('<div></div>')
.css({
position: 'absolute',
visibility: 'hidden',
top: $(document).scrollTop() + 'px'
})
.attr('id', hash)
.appendTo(document.body)
}
document.location.hash = hash
if (node.length) {
fx.remove()
node.attr('id', hash)
}
}
$('a').click(function (e) {
var href = $(this).attr('href')
var $root = $('html, body')
if (href[0] !== '#' || href.length === 1) {
return true
}
e.preventDefault()
$root.animate({
scrollTop: $(href).offset().top - 72
}, 500, function () {
setHash(href)
})
return false
})
@kolmakovruslan
Copy link
Copy Markdown

37

href[0] !== "#" || href.length === 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment