【问题标题】:How to use ng-animate to add slide effect on menu如何使用 ng-animate 在菜单上添加幻灯片效果
【发布时间】:2016-07-28 05:56:54
【问题描述】:

我正在尝试使用 ng-animate 和 CSS 样式实现幻灯片效果,但无法弄清楚我的代码有什么问题...

我可以使用min-heightmax-height 的百分比值来执行此操作吗?我不能在 px 中使用固定值。

JSFIDDLE

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
  <a href="#" class="toggle-menu" ng-click="collapsed=!collapsed">Click to toggle menu</a>
  <div class="menu" ng-show="collapsed" ng-animate="{show: 'menu-show', hide: 'menu-hide'}">
    <div class="files">
      <input type="checkbox" />
      <label>first</label>

      <input type="checkbox" />
      <label>second</label>
    </div>
    <div class="diffs">
      <input type="checkbox" />
      <label>first</label>

      <input type="checkbox" />
      <label>second</label>
    </div>
  </div>
</div>

CSS:

.menu-show-setup, .menu-hide-setup {
  transition:all 0.5s ease-out;
  overflow:hidden
}

.menu-show-setup {
  max-height:0;
}
.menu-show-setup.menu-show-start {
  max-height:25%;
}

.menu-hide-setup {
  max-height:25%;
}
.menu-hide-setup.menu-hide-start {
  max-height:0;
}

【问题讨论】:

  • 在描述您遇到的实际错误时能否更具描述性?您能否发布使用调试器时看到的任何错误?
  • 我的问题是幻灯片效果不起作用。调试器中没有错误。
  • 查看我发布的关于 ng-animate 的这个问题,它可能对您有所帮助:stackoverflow.com/a/33927043/4515720
  • 您的小提琴没有将 ng-animate 定义为依赖项,因此不使用它。见:docs.angularjs.org/api/ngAnimate
  • @S.B.是的,我认为 O.P. 使用显示应用程序依赖关系的 app.js 文件更新帖子会很有帮助。

标签: css angularjs


【解决方案1】:

首先确保您已加载 angular 和 angular-animate。 角度动画的 CDN 是 ...

<script src="//ajax.googleapis.com/ajax/libs/angularjs/X.Y.Z/angular-animate.js"></script>

那你需要引用ngAnimate给出的动画...

https://docs.angularjs.org/api/ngAnimate

例如,ng-if 当从 false 切换到 true 时,会将 .fade AND .ng-enter 类添加到 ng-if 所在的元素中。这些类将在短时间内自动删除。 ngAnimate 为你做这件事。

你要做的就是设计这些类的样式......

示例:

.fade.ng-enter {
  transition:0.5s linear all;
  opacity:0;
}

/* The finishing CSS styles for the enter animation */
.fade.ng-enter.ng-enter-active {
  opacity:1;
}

现在 ng-animate 将从 .fade.ng-enter 动画到 .fade.ng-enter.ng-enter-active

如果你不定义这些样式,ng-animate 将什么都不做。 如果您在浏览器的开发工具中检查相关元素,您将看到由 ng-animate 添加和删除的类。如果您看不到这些类被添加和删除,那么您加载的 angular 或 ng-animate 有问题。

【讨论】:

  • 尝试了各种解决方案,但都没有奏效……这让我很头疼。这段代码有什么问题? jsfiddle.net/wad7rzLa/4
猜你喜欢
  • 1970-01-01
  • 2012-08-20
  • 2014-05-22
  • 1970-01-01
  • 1970-01-01
  • 2015-04-11
  • 2012-08-20
  • 1970-01-01
  • 2019-07-28
相关资源
最近更新 更多