1

i want to change the color of my navbar when i scroll. When the navbar is on top the background is transparent. thank's to all for the attention and excuse me for my bad english!!

/* COLORE NAVBAR */

.navbar-default .navbar-toggle .icon-bar {
  background-color: #fff;
  box-shadow: 1px 1px #000;
}
.navbar-toggle {
  background-color: transparent;
  border: none;
}
.navbar-toggle:hover {
  background-color: transparent! important;
}
.navbar-toggle:focus {
  background-color: transparent! important;
}
.navbar-default {
  background-color: transparent;
  border-color: transparent;
}
.navbar-default .navbar-nav>li>a {
  color: #FFF;
  text-shadow: 1px 1px #000;
}
.navbar-default .navbar-nav>li>a:focus,
.navbar-default .navbar-nav>li>a:hover {
  color: red;
}
.navbar-default .navbar-nav>.active>a,
.navbar-default .navbar-nav>.active>a:focus,
.navbar-default .navbar-nav>.active>a:hover {
  background-color: yellow;
}
.navbar-default .navbar-nav>.open>a,
.navbar-default .navbar-nav>.open>a:focus,
.navbar-default .navbar-nav>.open>a:hover {
  background-color: blue;
}
.dropdown-menu>li>a {
  color: green;
}
.dropdown-menu>li>a:hover {
  background-color: brown;
  color: violet;
}
.navbar-brand>li>a:hover {
  color: white;
}
@media only screen and (max-width: 766px) {
  .collapsing,
  .in {
    background: #fff;
  }
  .collapsing ul li a,
  .in ul li a {
    color: #fff!important;
    float: left;
  }
  .collapsing ul li a:hover,
  .in ul li a:hover {
    color: #fff !important;
    background-color: #E1332D;
  }
}
<!-- Navigation -->
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
  <div class="container">
    <div class="navbar-header page-scroll">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand page-scroll" href="#page-top" style="color: white;">LUCA DELCONTE</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse navbar-ex1-collapse">
      <ul class="nav navbar-nav">
        <!-- Hidden li included to remove active class from about link when scrolled up past about section -->
        <li class="hidden">
          <a class="page-scroll" href="#page-top"></a>
        </li>
        <li>
          <a class="page-scroll" href="#about"><i class="fa fa-smile-o" aria-hidden="true"></i> 
    About</a>
        </li>
        <li>
          <a class="page-scroll" href="#services"><i class="fa fa-diamond" aria-hidden="true"></i>
     Design</a>
        </li>
        <li>
          <a class="page-scroll" href="#work"><i class="fa fa-lightbulb-o" aria-hidden="true"></i>
     Work</a>
        </li>
        <li>
          <a class="page-scroll" href="#contact"><i class="fa fa-envelope" aria-hidden="true"></i>
     Contact</a>
        </li>
      </ul>
    </div>
    <!-- /.navbar-collapse -->
  </div>
  <!-- /.container -->
</nav>
chirag
  • 1,761
  • 1
  • 17
  • 33
Luca Delconte
  • 25
  • 1
  • 9
  • Check out [this](http://stackoverflow.com/questions/29646622/set-bootstrap-navbar-transparency-on-scroll) or [this](http://stackoverflow.com/questions/24923210/transparent-navbar-to-visible-when-scrolling) or W3Schools documentation on [scrollspy](http://www.w3schools.com/bootstrap/bootstrap_scrollspy.asp) or Bootstrap's documentation on [scrollspy](http://getbootstrap.com/javascript/#scrollspy). – Steven B. Sep 26 '16 at 14:03
  • 1
    Possible duplicate of [Shrink and change background navbar on scroll](http://stackoverflow.com/questions/37084075/shrink-and-change-background-navbar-on-scroll) – Walf Sep 26 '16 at 14:16

2 Answers2

5

Use the Bootstrap Affix component to watch the scroll position instead of extra jQuery/JavaScript. Just define the .affix-top and .affix CSS for the navbar.

   /* navbar style once affixed to the page */ 
   .affix {
         background-color: black;   
    }

    @media (min-width:768px) {
        .affix-top {
          /* navbar style at top */
          background-color:transparent;
          border-color:transparent;
          padding: 15px; 
        }
    }

and set the position you want the navbar to change style (ie; 400px from the top)

<div class="navbar navbar-inverse navbar-fixed-top" data-spy="affix" data-offset-top="400">

Working Demo: http://www.codeply.com/go/xp2fY6sVHP


For Bootstrap 4, see: Animate/Shrink NavBar on scroll using Bootstrap 4

Carol Skelly
  • 351,302
  • 90
  • 710
  • 624
0

jQuery

/**
 * Listen to scroll to change header opacity class
 */
function checkScroll(){
    var startY = $('.navbar').height() * 2; //The point where the navbar changes in px

    if($(window).scrollTop() > startY){
        $('.navbar').addClass("scrolled");
    }else{
        $('.navbar').removeClass("scrolled");
    }
}

if($('.navbar').length > 0){
    $(window).on("scroll load resize", function(){
        checkScroll();
    });
}
Jay240692
  • 166
  • 1
  • 5
  • 17
  • You should add an explanation – Carol Skelly Sep 26 '16 at 21:48
  • Add a class is not solution to change a color. What if you want to change between 200 colors? 200 classes? Or to do that the scroll value have direct effect over the color? – Sam Apr 14 '17 at 20:28