简体   繁体   中英

How do you make it so the navbar is transparent then changes color using bootstrap 3. (Affix)

Ive been trying to find out how to do this for a week and have been unable to do it. My html navbar looks like this.

<div class="maincontainer">
<!--Navbar-->
<div data-spy="affix" data-offset-top="60" data-offset-bottom="200">
  <div class="navbar navbar-default navbar-fixed-top" role="navigation">
    <div class="container">
        <div class="navbar-header" >
                        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-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" href="#">Pixoweb</a>
        </div>
        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav navbar-right">
                <li class="active"><a href="#">Home</a></li>
                <li><a href="#">About</a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Examples<b class="caret"></b></a>
                    <ul class="dropdown-menu">
                        <li><a href="#">Example 1</a></li>
                        <li><a href="#">Example 2</a></li>
                        <li><a href="#">Example 3</a></li>
                        <li><a href="#">Example 4</a></li>
                    </ul>
                </li>
                <li><a href="#contact" data-toggle="modal">Contact</a></li>
            </ul>
        </div>
    </div>
</div>

Js

    $('#myAffix').affix({
  offset: {
    top: 100,
    bottom: function () {
      return (this.bottom = $('.footer').outerHeight(true))
    }
  }
})

css

      .affix {
    position: fixed !important;
    top: 0 !important;
    width: 100% !important;
    background-color: #957595 !important;
}

So far this has made it so that the navbar goes behind the the context and images. I would like it if someone can help me

Navbar has not gone behind the text/images, it has been 3d transformed by bootstrap affix so that it looks like it has gone behind. I assume that you want to change the background color of the navbar when it is pinned (affixed). You can use following CSS. Note: you don't need JavaScript.

.affix .navbar-default {
    position: fixed !important;
    top: 0 !important;
    width: 100% !important;
    background-color: #957595 !important;
}

.affix {
    width: 100% !important;
}

I also struggled with my menu displaying behind my images, I fixed it by adding "z-index: 1;" to my nav class. eg:.navbar-inverse in the style sheet

.navbar-default {
  background-color: #201f1f;
  border-color: #f47d20;
  border-bottom:#f47d20 5px solid;
  padding-bottom:0px;
  z-index: 1;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM