Your problem is that after the first execution of fadeTo your element remains with the style="opacity: 1" attribute, which remains there. So you need to delete it when you scroll up.
I also changed the way the navigator is .hide() , I suggest using .hide() , because it also uses the style attribute of the elements, so it will not be overridden. And there is also navbarVisible var, which is used to determine if the navigation is already fading, and if so, the code to fade it is not executed when it is not needed. This should be a tiny step in productivity.
Everything seems to work fine:
<script type="text/javascript"> $(document).ready(function(){ var navbarVisible = false; $(window).scroll(function(){ var ht = $('header').height()+70; if ($(this).scrollTop() >= ht) { if (!navbarVisible) { $("#navb").addClass("navbar-fixed-top") .hide() .fadeTo('slow','1'); $(".row:first").css("padding-top","50px"); navbarVisible = true; }; } else { $("#navb").removeClass("navbar-fixed-top").removeAttr('style'); $(".row:first").css("padding-top","0px"); navbarVisible = false; } }); }); </script>
You no longer need this part:
<style type="text/css"> .navOpacity{ opacity: 0; } </style>
Here's a link to a working code JSFiddle example: JSFiddle link
Janis Vepris
source share