I have a #top bar in my website. It is always visible at the top and travels with the user as the user scrolls. It works fine. Now I want to display a shadow on the #top bar only if the scrollbar position is > 0. If the user goes to the top, it must dissapear.
#top {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 80px;
z-index: 9999;
}
#top.shadow {
-webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, 0.3);
-moz-box-shadow: 0 1px 1px rgba(0, 0, 0, 0.3);
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.3);
}
Solution (based on Godwin's answer)
$(window).scroll(function(){
if($(window).scrollTop() > 0) {
$('#top').addClass('shadow');
} else {
$('#top').removeClass('shadow');
}
});
...but I believe that is not the best way to go -- seems to have a low performance on old computers/browsers. Any ideas?