滚动后更改颜色导航栏

时间:2016-09-26 13:53:47

标签: html css twitter-bootstrap

我想在滚动时更改导航栏的颜色。当导航栏位于顶部时,背景是透明的。 感谢所有人的关注,请原谅我的英语不好!!

/* 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>

2 个答案:

答案 0 :(得分:5)

使用Bootstrap Affix component来观看滚动位置而不是额外的jQuery / JavaScript。只需为导航栏定义.affix-top.affix CSS。

   /* 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; 
        }
    }

并设置您希望导航栏更改样式的位置(即;从顶部400px)

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

工作演示: http://www.codeply.com/go/xp2fY6sVHP

对于Bootstrap 4,请参阅:Animate/Shrink NavBar on scroll using Bootstrap 4

答案 1 :(得分: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();
    });
}