Skip to content

Instantly share code, notes, and snippets.

@JamieBradders
Last active May 10, 2017 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamieBradders/a4aee325c12d36dc8fd51ef3c13a8b8b to your computer and use it in GitHub Desktop.
Save JamieBradders/a4aee325c12d36dc8fd51ef3c13a8b8b to your computer and use it in GitHub Desktop.
Sticky Sidebar
/* Use .sidebar-sticky class to turn sidebar element into a sticky sidebar.
* Following code keeps the sidebar within view and uses animate() to create
* a smooth transition
*/
const $sidebar = $(".sidebar-sticky")
const $window = $(window)
const offset = $sidebar.offset()
const topPadding = 110
if ($sidebar.length > 0) {
$window.scroll(function() {
const limit = $('.banner--eligibility').offset().top - $sidebar.height() - 20
if (limit < $window.scrollTop()) {
return
}
if ($window.scrollTop() > offset.top && $window.width() > 640) {
$sidebar.stop().animate({
marginTop: $window.scrollTop() - offset.top + topPadding
})
} else {
$sidebar.stop().animate({
marginTop: 0
})
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment