Skip to content

Instantly share code, notes, and snippets.

@blackfalcon
Last active December 18, 2015 12:39
Show Gist options
  • Save blackfalcon/5784488 to your computer and use it in GitHub Desktop.
Save blackfalcon/5784488 to your computer and use it in GitHub Desktop.
.fixed {
position: fixed;
top: 0;
}
<div data-behavior="stick-to-top">
content ....
</div>
$ ->
el = $('[data-behavior~=stick-to-top]')
return if el.length == 0
top = false
$(window).scroll ->
top ||= el.offset().top - parseInt(el.css('marginTop'))
windowTop = $(window).scrollTop()
el.toggleClass('fixed', windowTop >= top)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment