我需要在类添加/删除时播放动画。到目前为止,我的解决方案看起来很丑陋,使用了两个类的冗长方法。我很高兴能想出更优雅、更整洁的代码。
示例:http://plnkr.co/edit/wvrfP3lIbRmBeSdi99xO?p=preview
html:
<body ng-app="ngAnimate">
<input id="setbtn" type="button" value="set" ng-click="myVar=true">
<input id="clearbtn" type="button" value="clear" ng-click="myVar=false">
<br>
<span class="base-class" ng-class="{'my-class1': myVar, 'my-class2': !myVar }">Sample Text</span>
</body>css:
.base-class {
cursor: default;
display: block;
background: #dc5d63;
width: 100%;
left: 0;
right: 0;
top: 0;
position: relative;
height: 77px;
font-family: Cala-Bold, serif;
float: left;
}
.base-class.my-class1,
.base-class.my-class2 {
-moz-animation: 1s slidein ease-out;
-webkit-animation: 1s slidein ease-out;
animation: 1s slidein ease-out;
}
@-webkit-keyframes slidein {
from {
margin-top: -100%;
}
to {
margin-top: 0%;
}
}
@-moz-keyframes slidein {
from {
margin-top: -100%;
}
to {
margin-top: 0%;
}
}
@keyframes slidein {
from {
margin-top: -100%;
}
to {
margin-top: 0%;
}
}发布于 2015-01-06 23:18:08
正确的解决方案是ngClass指令的动画挂钩不明显:https://code.angularjs.org/1.2.28/docs/guide/animations (类和ngClass动画挂钩部分)
工作示例:http://plnkr.co/edit/fMaALtiR8dxR8QkAoH6E?p=preview
html:
<body ng-app="ngAnimate">
<input id="setbtn" type="button" value="set" ng-click="myVar=true">
<input id="clearbtn" type="button" value="clear" ng-click="myVar=false">
<br>
<span class="base-class" ng-class="{'my-class': myVar }">Sample Text</span>
</body>css:
.base-class {
cursor: default;
display: block;
background: #dc5d63;
width: 100%;
left: 0;
right: 0;
top: 0;
position: relative;
height: 77px;
font-family: Cala-Bold, serif;
float: left;
}
.base-class.my-class-add,
.base-class.my-class-remove {
-moz-animation: 1s slidein ease-out;
-webkit-animation: 1s slidein ease-out;
animation: 1s slidein ease-out;
}
@-webkit-keyframes slidein {
from {
margin-top: -100%;
}
to {
margin-top: 0%;
}
}
@-moz-keyframes slidein {
from {
margin-top: -100%;
}
to {
margin-top: 0%;
}
}
@keyframes slidein {
from {
margin-top: -100%;
}
to {
margin-top: 0%;
}}
https://stackoverflow.com/questions/27801617
复制相似问题