Skip to content

Instantly share code, notes, and snippets.

@noahrc
Created March 6, 2013 22:53
Show Gist options
  • Save noahrc/5103959 to your computer and use it in GitHub Desktop.
Save noahrc/5103959 to your computer and use it in GitHub Desktop.
Adds 'back to top' links to the page and makes a navigation bar stick to the top of the page
// Create a sticky widget
var STICKY = {}
// Start a timer on window scroll to prevent firing lots of extra functions
STICKY.timer = setTimeout(function(){})
// Only do the following if the browser isn't IE 6
if(!$('html.ie6')[0]) {
// Add 'back to top' links, except in IE 6
$('#container')
.append('<div id="back-to-top"><a href="#">Back to top</a></div>')
.append('<div id="back-to-top-2"><a href="#">Back to top</a></div>')
$('#back-to-top').hide()
.css({
'top': '0px'
})
$('#back-to-top-2').hide()
.css({
'bottom': '0px'
})
// Clone the sticky nav
$('#sticky-nav').parent().append($('#sticky-nav').clone().attr('id', 'sticky-nav-clone').hide())
$(window).scroll(function(){
clearTimeout(STICKY.timer)
STICKY.timer = setTimeout(function(){
STICKY.makeSticky("#back-to-top")
STICKY.makeSticky("#sticky-nav-clone")
STICKY.makeSticky("#back-to-top-2", true)
}, 50)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment