Bootstrap Navbar菜单中的悬停转换

时间:2015-02-01 16:53:55

标签: css twitter-bootstrap hover css-transitions

我找到了这个codepen,其中使用css转换来实现对悬停的滑动效果。 http://codepen.io/tdevine33/pen/CnDyv 但我不能让它在我的引导程序导航栏上运行。它与Bootstrap中的菜单不兼容吗?

 <!-- Static navbar -->
<nav class="navbar navbar-default navbar-static-top">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>
    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav">
        <li><a href="index.php">Index</a></li>
        <li><a href="#">Link1</a></li>
        <li><a href="#">Link2</a></li>
        <li><a href="#">Link3</a></li>

        <li class="dropdown">
          <a href="#.php" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">LinkTest<span class="caret"></span></a>
          <ul class="dropdown-menu" role="menu">
            <li><a href="#">Linktest 2</a></li>
            <li><a href="#">Linktest 3</a></li>                
          </ul>

        <li><a href="kontakt.php">Kontakt</a></li>
          </ul>
        </li>
      </ul>
    </div><!--/.nav-collapse -->
  </div>
</nav>

1 个答案:

答案 0 :(得分:1)

原始的codepen需要稍微调整才能支持下拉菜单。我还改变了css来转换变换而不是左边的属性。

demo

<强> HTML:

<nav class="navbar navbar-default navbar-static-top">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar"> <span class="sr-only">Toggle navigation</span>
 <span class="icon-bar"></span>
 <span class="icon-bar"></span>
 <span class="icon-bar"></span>

            </button>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
            <ul class="nav navbar-nav list-hover-slide">
                <li><a href="index.php">Index</a>
                </li>
                <li><a href="#">Link1</a>
                </li>
                <li><a href="#">Link2</a>
                </li>
                <li><a href="#">Link3</a>
                </li>
                <li class="dropdown"> <a href="#.php" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">LinkTest<span class="caret"></span></a>

                    <ul class="dropdown-menu" role="menu">
                        <li><a href="#">Linktest 2</a>
                        </li>
                        <li><a href="#">Linktest 3</a>
                        </li>
                    </ul>
                    <li><a href="kontakt.php">Kontakt</a>
                    </li>
            </ul>
            </li>
            </ul>
        </div>
        <!--/.nav-collapse -->
    </div>
</nav>

<强>的CSS:

.list-hover-slide>li {
    position: relative;
    overflow: hidden;
}
.list-hover-slide>li>a {
    z-index: 1;
    transition: .35s ease color;
}
.list-hover-slide>li>a:before, .list-hover-slide>li.dropdown.open>a:before {
    content:'';
    display: block;
    z-index: -1;
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    transform: translateX(-100%);
    border-right: solid 5px tomato;
    background: gray;
    transition: .35s ease transform;
}
.list-hover-slide>li>a:hover:before, .list-hover-slide>li.dropdown.open>a:before {
    transform: translateX(0);
}
.list-hover-slide>li.dropdown.open {
    overflow: initial;
}