CSS Hover Transition

时间:2016-08-28 09:55:52

标签: html css webkit css-transitions transition

我正在尝试创建类似于this.

导航栏的css过渡

我尝试过以下代码,但是没有用。

        .navbar-default .navbar-nav li a{
  /*  color: #fff; */
    text-transform: uppercase;
    padding-left: 24px;
    padding-right: 24px;
    position: relative;
  }

  .navbar-default .navbar-nav li a:before{
    background-color: #c83a3a;
    height: 100%;
    left: 50%;
    opacity: 0;
    filter: alpha(opacity=0);
    top: 50%;
    -webkit-transform: translate(-50%,-50%);
    -ms-transform: translate(-50%,-50%);
    transform: translate(-50%,-50%);
    width: 0;
    content: "";
    position: absolute;
    -webkit-transition: all 0.3s ease 0s;
    transition: all 0.3s ease 0s;
    z-index: -1;
  }

  .navbar-default .navbar-nav li:hover a:before{
    width: 100%;
    opacity: 1;
    filter: alpha(opacity=100);
  }

我当前的网站是here 这不适用于顶级菜单栏,适用于其他所有

1 个答案:

答案 0 :(得分:1)

试试这个:

  .navbar-default .navbar-nav li a{
  /*  color: #fff; */
    text-transform: uppercase;
    padding-left: 24px;
    padding-right: 24px;
    position: relative; /* change */
  }

  /*.navbar-default .navbar-nav li a:hover
  {
    background-color: #c83a3a;
    transition: all 0.3s ease 0s;
  }*/

  .navbar-default .navbar-nav li a:before{
    background-color: #c83a3a;
    height: 100%;
    left: 50%;
    opacity: 0;
    filter: alpha(opacity=0);
    top: 50%;
    -webkit-transform: translate(-50%,-50%);
    -ms-transform: translate(-50%,-50%);
    transform: translate(-50%,-50%);
    width: 0;
    content: "";
    position: absolute;
    -webkit-transition: all 0.3s ease 0s;
    transition: all 0.3s ease 0s;
    z-index: -1;
  }

  /* change */
  .navbar-default .navbar-nav li:hover a:before{
    width: 100%;
    opacity: 1;
    filter: alpha(opacity=100);
  }

工作示例:https://jsfiddle.net/p3aqno7j/