简体   繁体   English

仅具有角度和CSS的折叠元素上的平滑过渡

[英]Smooth transition on collapsing element with angular & css only

I am trying to add a collapsable element to my app. 我正在尝试向我的应用添加可折叠元素。 I don't want to use jQuery or any other dependency than Angular. 我不想使用jQuery或除Angular之外的任何其他依赖项。

I have built a very simple demo here: http://jsfiddle.net/eg69cfub/2/ 我在这里构建了一个非常简单的演示: http : //jsfiddle.net/eg69cfub/2/

The elements are displayed/hidden properly the only problem is the transition. 正确显示/隐藏元素的唯一问题是过渡。 I don't understand why it is so abrupt, I'd like it to be smooth. 我不明白为什么它这么突然,我希望它平滑。

How can I fix this please? 我该如何解决?

HTML: HTML:

<div ng-app>
    <div>
      <ul>
          <li ng-repeat='test in [1, 2, 3]' ng-controller='accordionCtrl'>
              <div class='header' ng-click='isVisible = !isVisible'> Hello {{ test }}</div>
              <div class='body' ng-class='{ collapsed: isVisible }'> Goodbye </div>
          </li>
      </ul>
    </div>
</div>

CSS: CSS:

li {
    list-style: none;
}
.header {
    width: 100%;
    background-color: red;
}

.body {
    width 100%;
    background-color: blue;
    display: block;
    min-height: 1px;
    transition: all ease 3s;
    overflow: hidden;
}

.collapsed {
    min-height: 0;
    height: 0;
}

JS: JS:

var myApp = angular.module('myApp',[]);


function accordionCtrl($scope) {
    $scope.isVisible = false;
}

In Angular, you can use ng-animate to perform smooth animations with ng-show directive (along with many other directives). 在Angular中,您可以使用ng-animate和ng-show指令(以及许多其他指令)执行平滑的动画。 Here is your fiddler with animations: http://jsfiddle.net/fu2jw6j1/ 这是您的带有动画的提琴手: http : //jsfiddle.net/fu2jw6j1/

Your HTML: 您的HTML:

<div ng-app>
<div>
  <ul>
      <li ng-repeat='test in [1, 2, 3]' ng-controller='accordionCtrl'>
          <div class='header' ng-click='$scope.isVisible=!$scope.isVisible'> Hello {{ test }}</div>
          <div class='box' ng-show="$scope.isVisible" ng-animate="'box'">  Goodbye </div>
      </li>
  </ul>
</div>

And here's the CSS: 这是CSS:

li {
    list-style: none;
}
.header {
    width: 100%;
    background-color: red;
}

.box {
    width 100%;
    background-color: blue;
    display: block;
    min-height: 1px;
    transition: all ease 3s;
    overflow: hidden;
    height: 20px;
}

.box-show-setup, .box-hide-setup {
  -webkit-transition:all linear 0.3s;
  -moz-transition:all linear 0.3s;
  -ms-transition:all linear 0.3s;
  -o-transition:all linear 0.3s;
  transition:all linear 0.3s;
}

.box-show-setup { height: 0; }
.box-show-setup.box-show-start { height:20px }

.box-hide-setup {height: 0; }
.box-hide-setup.box-hide-start { height: 0px; }

You can find more about ng-animate directive in Angular's documentation: https://docs.angularjs.org/api/ngAnimate 您可以在Angular的文档中找到有关ng-animate指令的更多信息: https : //docs.angularjs.org/api/ngAnimate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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