Skip to content

Instantly share code, notes, and snippets.

@odessy
Last active January 22, 2019 23:09
Show Gist options
  • Save odessy/81bfc35350460c273faa8ccae96cfe24 to your computer and use it in GitHub Desktop.
Save odessy/81bfc35350460c273faa8ccae96cfe24 to your computer and use it in GitHub Desktop.
make call to action sticky
jQuery(function($) {
var $window = $(window),
ctaPosition = parseInt($('#call-to-action').offset().top) - parseInt($('#call-to-action').height()) - window.innerHeight,
cta = $('#call-to-action');
// Run this on scroll events.
$window.scroll(function() {
console.log( $window.scrollTop() + ' - ' + ctaPosition );
if ( $window.scrollTop() < ctaPosition ) {
// Make the div sticky.
cta.css({'position':'fixed','bottom':'0', 'width': '100%', 'z-index': '9999'});
}
else {
// Unstick the div.
cta.css({'position':'relative'});
}
});
$window.scroll();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment