Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter bootstrap navbar shadow on scroll

I'm look to do the following http://codepen.io/anon/pen/eIfdn for the Twitter bootstrap navbar. It simply adds a shadow to the navbar upon scrolling. Any advice would be helpful thank you.

.navbar {
*position: relative;
top: 0;
left: 0;
width: 100%;
height: 80px;
z-index: 999;
-webkit-box-shadow: 0 8px 8px rgba(0, 0, 0, 0.5);
-moz-box-shadow: 0 8px 8px rgba(0, 0, 0, 0.5);
box-shadow: 0 8px 8px rgba(0, 0, 0, 0.5);
display: none;
*z-index: 2;
margin-bottom: 20px;
overflow: visible;
}

Thats the css I changed and I added in the js from above.

Here's the JS I used

$(document).ready(function(){
 $(window).scroll(function(){
  var y = $(window).scrollTop();
  if( y > 0 ){
  $("#navbar").css({'display':'block', 'opacity':y/20});
  } else {
  $("#navbar").css({'display':'block', 'opacity':y/20});
  }
 });
})
like image 800
Morki Avatar asked Feb 11 '13 01:02

Morki


1 Answers

Here is somenthing to get you started:
http://jsfiddle.net/panchroma/pyYfG/

HTML

<div class="navbar" data-spy="affix">
<div class="navbar-inner">
.... standard navbar stuff ...
</div>
</div>
<div id="top-shadow"></div>
.... page content ...

CSS

#top-shadow {
position: fixed;
top: 0;
left: 20px;
width: 100%;
height: 42px;
z-index: 999;
-webkit-box-shadow: 0 8px 8px rgba(0, 0, 0, 0.5);
-moz-box-shadow: 0 8px 8px rgba(0, 0, 0, 0.5);
box-shadow: 0 8px 8px rgba(0, 0, 0, 0.5);
display: none;
}

.navbar.affix{ /* position fixed navbar */
top:0; 
width:100%;
}

/* UPDATE BELOW */
.navbar{    
z-index:1000; /* lift .navbar above #top-shadow */
}

The important bits are that I'm using the affix behaviour to lock the navbar inplace, and I'm applying the shadow to a new div just below the navbar. I think this will be easier to manage that trying to add a shadow directly to the navbar itself.

Good luck!

like image 168
David Taiaroa Avatar answered Oct 29 '22 15:10

David Taiaroa