如何修复CSS中的动画故障?

时间:2019-03-20 22:00:06

标签: css

我正在尝试创建一个简单的带有与宽度变化匹配的动画的侧面菜单。菜单关闭时,我只想显示图标,打开菜单时,我想同时显示-图标和文本。 动画效果很好,但是在切换时会出现一些小故障……似乎当文本出现时,它与图标的换行符之间。 菜单打开时如何延迟显示文本?

Fiddle

HTML

     <div ng-app class="container" ng-controller="MyCtrl">
        <div class="row">
        <div class="col-md-3"><button class="btn btn-success" ng-click="toggleSideMenu()">
              <i class="fa fa-bars"></i></button>
          <div class="side-menu-wrapper" ng-class="{'menu-open':!!isMenuExpanded, 'menu-closed':!isMenuExpanded}">
            <ul class="list-group">
              <li class="list-item">
                <i class="fa fa-cog">
                  </i><span>
                    First item</span></li>
              <li class="list-item">
                <i class="fa fa-book"></i>
                <span>
                    First item</span></li>
              <li class="list-item">
                <i class="fa fa-pencil"></i>
                <span>
                    First item</span></li>
            </ul>
          </div>
        </div>
        <div class="col-md-9">

        </div>
      </div>
    </div>

CSS

    .container {
      margin-top: 15px;
    }

    .side-menu-wrapper {
      background-color: rgb(41, 44, 90);
      height: 100vh;
      transition: width 0.5s ease-in;
    }

    .side-menu-wrapper span {
      margin-left: 5px;
    }

    .side-menu-wrapper.menu-closed {
      width: 15%;
    }

    .side-menu-wrapper.menu-closed span {
      /* display: none;
      opacity: 0; */
      transition: opacity 1s ease-out;
      opacity: 0;
      height: 0;
      overflow: hidden;
    }

    .side-menu-wrapper.menu-open span {
      display: inline-block;
      float: right;
      opacity: 1;
    }

    .side-menu-wrapper.menu-open {
      opacity: 1;
      transition: width 0.5s ease-out;
      width: 100%;
    }

    .list-item {
      background-color: transparent;
      color: rgb(161, 165, 224);
      position: relative;
      display: flex;
      padding: 10px 15px;
      border-bottom: 1px solid rgb(161, 165, 224);
    }

Angularjs

  function MyCtrl($scope) {
    $scope.isMenuExpanded = false;
    $scope.toggleSideMenu = function() {
      $scope.isMenuExpanded = !$scope.isMenuExpanded;
    }
  }

2 个答案:

答案 0 :(得分:0)

问题是由于在height: 0;中设置了.side-menu-wrapper.menu-closed span

引起的

更改

    .side-menu-wrapper.menu-closed span {
      /* display: none;
      opacity: 0; */
      transition: opacity 1s ease-out;
      opacity: 0;
      height: 0;
      overflow: hidden;
    }

收件人

   .side-menu-wrapper.menu-closed span {
      /* display: none;
      opacity: 0; */
      transition: opacity 1s ease-out;
      opacity: 0;
      overflow: hidden;
    }

   .side-menu-wrapper span {
      margin-left: 5px;
    }

收件人

   .side-menu-wrapper span {
      margin-left: 5px;
      opacity: 0;
      white-space: nowrap;
    }

答案 1 :(得分:0)

我将其固定在此JSFIddle

这是您必须使用的新CSS:

.container {
  margin-top: 15px;
}

.side-menu-wrapper {
  background-color: rgb(41, 44, 90);
  height: 100vh;
  transition: width 0.5s ease-in;
}

.side-menu-wrapper span {
  margin-left: 5px;
}

.side-menu-wrapper.menu-closed {
  width: 15%;
}

.side-menu-wrapper.menu-closed span {
  /* display: none;
  opacity: 0; */
  transition: opacity 1s ease-out;
  opacity: 0;
  overflow: hidden;
}

.side-menu-wrapper.menu-open span {
  display: inline-block;
  float: right;
  opacity: 1;
  
}

.side-menu-wrapper.menu-open {
  opacity: 1;
  transition: width 0.5s ease-out;

  width: 100%;
}



.list-item {
  background-color: transparent;
  color: rgb(161, 165, 224);
  position: relative;
  display: flex;
  padding: 10px 15px;
  border-bottom: 1px solid rgb(161, 165, 224);
}


有关小故障的更多详细信息

首先,我通过删除转换中的height: 0;来更新以下类,这会造成冲突:

.side-menu-wrapper.menu-open {
  opacity: 1;
  transition: width 0.5s ease-out;
  /*height: 0;  This line make a conflict and is not needed */ 
  width: 100%;
}

可能的改进

在下面的课程中,您还可以处理以下事实:菜单一旦包装,不仅不会显示图标,而且还会在右侧显示奇怪的空白

.side-menu-wrapper.menu-closed {
   width: 15%;
}

尝试类似的东西:

.side-menu-wrapper.menu-closed {
   width: 5.5%;
}