I am trying to create a sticky navigation on my page where the div gets position:fixed; after the user scrolls the item. Functionality works as expected, but the width of the two columns that should adhere to the top change after adding the sticky class. I tried to add width:100%; in the CSS of the sticky element, but then the element expands outside the container.
How can I make sure that the width of the columns remains where they should be when position:fixed; ?
HMTL:
<div class="container"> <div class="padding"></div> <div class="anchor"></div> <div class="row sticky"> <div class="col-sm-6"> Testing </div> <div class="col-sm-6"> Testing </div> </div> <div class="padding2"></div> </div>
CSS
.padding { height:250px; } .padding2 { height:1000px; } .sticky { background:#000; height:50px; line-height:50px; color:#fff; font-weight:bold; } .sticky.stick { position:fixed; top:0; z-index:10000; }
JS:
function stickyDiv() { var top = $(window).scrollTop(); var divTop = $('.anchor').offset().top; if (top > divTop) { $('.sticky').addClass('stick'); } else { $('.sticky').removeClass('stick'); } } $(window).scroll(function(){ stickyDiv(); }); stickyDiv();
Thanks!
source share