Skip to content

Instantly share code, notes, and snippets.

@Mdimran41
Created March 23, 2016 03:54
Show Gist options
  • Save Mdimran41/192846554454751be384 to your computer and use it in GitHub Desktop.
Save Mdimran41/192846554454751be384 to your computer and use it in GitHub Desktop.
<!-- html code -->
<div class="sticky_anchor"></div> <!-- essential blank div -->
<div class="stick">
<!-- your content here -->
</div>
------------------------------------------
------------------------------------------
*/
css code
*/
.sticky{
position: fixed;
width:100%;
top:0;
}
------------------------------------------
------------------------------------------
//javaScript code
$(window).scroll(function () {
function sticky_relocate() {
var window_top = ($(window).scrollTop() >= 100);
var div_top = $('#sticky_anchor').offset().top;
if (window_top > div_top) {
$('.stick').addClass('sticky');
$('#sticky_anchor').height($('.stick').outerHeight());
} else {
$('.stick').removeClass('sticky');
$('#sticky_anchor').height(0);
}
}
$(function() {
$(window).scroll(sticky_relocate);
sticky_relocate();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment