如何更改元素之前和之后导航栏崩溃后?

时间:2015-05-24 02:16:51

标签: javascript html css twitter-bootstrap navbar

我正在尝试更改网站上导航栏中的一些元素。首先,当用户将鼠标悬停在链接上时,它会在链接下方添加一个边框(表示用户正在其上方悬停),如下所示:

Before, no navbar collapse 导航栏在页面顶部是透明的。

当用户向下滚动页面时,导航栏将崩溃(如果这是正确的术语?),看起来像这样:

After, collapsed navbar

问题是我不想在导航栏折叠时显示底部边框。我如何用CSS解决这个问题?

以下是我的HTML:

<nav class="navbar navbar-default navbar-fixed-top">
      <div class=container>
        <div class="navbar-header page-scroll">
          <button type=button class=navbar-toggle data-toggle=collapse data-target="#bs-example-navbar-collapse-1">
          <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"></a>
        </div>
        <div class="collapse navbar-collapse">
          <ul class="nav navbar-nav navbar-right">
            <li class=hidden>
              <a href="#page-top"></a>
            </li>
            <li class=main-links>
              <a class=page-scroll href="#about">About</a>
            </li>
            <li class=main-links>
              <a class=page-scroll href="#portfolio">Portfolio</a>
            </li>
            <li class=main-links>
              <a class=page-scroll href="#contact">Contact</a>
            </li>
            <li class=dropdown-bar>
              <a href=""> More &#9660;</a>
              <ul class=drop-nav>
                <a href=""><li>Blog</li></a>
                <a href=""><li>Resources</li></a>
              </ul>
            </li>
          </ul>
        </div>
      </div>
    </nav>

与导航栏的CSS一起使用:

.navbar-default {
    border-color: transparent;
    background-color: #222;
}

/* Name in top left corner */
.navbar-default .navbar-brand {
    font-family: Montserrat,"Helvetica Neue",Helvetica,Arial,sans-serif;
    color: white;
}

/* Hover over name in left corner */
.navbar-default .navbar-brand:hover,
.navbar-default .navbar-brand:focus,
.navbar-default .navbar-brand:active,
.navbar-default .navbar-brand.active {
    color: #888;
}

.navbar-default .navbar-collapse {
    border-color: rgba(255,255,255,.02);
}

/* Navbar button color */
.navbar-default .navbar-toggle {
    border-color: #bf0a0a;
    background-color: #bf0a0a;
}


/* Navbar icon color */
.navbar-default .navbar-toggle .icon-bar {
    background-color: #fff;
}

.navbar-default .navbar-toggle:hover,
.navbar-default .navbar-toggle:focus {
    background-color: #bf0a0a;
}

.navbar-default .nav li a {
    text-transform: uppercase;
    font-family: Montserrat,"Helvetica Neue",Helvetica,Arial,sans-serif;
    font-weight: 400;
    letter-spacing: 1px;
    color: #fff;
}

/* Hover over [About, Portfolio, Contact, More] */
.navbar-default .nav > .main-links a:hover,
.navbar-default .nav > .main-links a:focus {
    color: #fff;
    outline: 0;
    border-bottom: 4px solid #fff;
    -webkit-transition: all .4s;
    -moz-transition: all .4s;
    transition: all .4s;
}

/* Hover over [More] */
.navbar-default .nav > .dropdown-bar a:hover,
.navbar-default .nav > .dropdown-bar a:focus {
    color: #fff;
    outline: 0;
}


.navbar-default .navbar-nav>.active>a {
    border-radius: 0;
    color: #fff;
    background-color: #bf0a0a;
}

.navbar-default .navbar-nav>.active>a:hover,
.navbar-default .navbar-nav>.active>a:focus {
    color: #fff;
    background-color: #407EB0;
}

@media(min-width:768px) {
    .navbar-default {
        padding: 25px 0;
        border: 0;
        background-color: transparent;
        -webkit-transition: padding .3s;
        -moz-transition: padding .3s;
        transition: padding .3s;
    }

    .navbar-default .navbar-brand {
        font-size: 2em;
        -webkit-transition: all .3s;
        -moz-transition: all .3s;
        transition: all .3s;
    }

    .navbar-default .navbar-nav>.active>a {
        border-radius: 3px;
    }

    .navbar-default.navbar-shrink {
        padding: 10px 0;
        background-color: rgba(34, 34, 34, 0.8);
    }

    .navbar-default.navbar-shrink .dropdown-bar:hover > .drop-nav {
        display: block;
        margin-top: 10%;
        padding-right: 100%;
        background-color: rgba(34, 34, 34, 0.8);
    }

    .navbar-default.navbar-shrink .navbar-brand {
        font-size: 1.5em;
    }
}

在悬停时专门添加边框的CSS在此处:

/* Hover over [About, Portfolio, Contact, More] */
.navbar-default .nav > .main-links a:hover,
.navbar-default .nav > .main-links a:focus {
    color: #fff;
    outline: 0;
    border-bottom: 4px solid #fff;
    -webkit-transition: all .4s;
    -moz-transition: all .4s;
    transition: all .4s;
}

处理页面滚动和导航栏更改的Javascript:

var cbpAnimatedHeader = (function() {

    var docElem = document.documentElement,
        header = document.querySelector( '.navbar-default' ),
        didScroll = false,
        changeHeaderOn = 300;

    function init() {
        window.addEventListener( 'scroll', function( event ) {
            if( !didScroll ) {
                didScroll = true;
                setTimeout( scrollPage, 250 );
            }
        }, false );
    }

    function scrollPage() {
        var sy = scrollY();
        if ( sy >= changeHeaderOn ) {
            classie.add( header, 'navbar-shrink' );
        }
        else {
            classie.remove( header, 'navbar-shrink' );
        }
        didScroll = false;
    }

    function scrollY() {
        return window.pageYOffset || docElem.scrollTop;
    }

    init();

})();

1 个答案:

答案 0 :(得分:2)

当用户向下滚动页面时,navbar-shrink会添加到导航栏,从而覆盖navbar-default中的某些样式。同样,您可以通过在默认悬停代码下使用此代码来覆盖默认悬停行为。

.navbar-shrink .nav > .main-links a:hover,
.navbar-shrink .nav > .main-links a:focus {
    border-bottom: none;
}