Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
916 views
in Technique[技术] by (71.8m points)

html - change color navbar after scroll

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...