简体   繁体   English

淡出和淡入过渡不适用于ng-hide

[英]Fade-out and fade-in transition not working on ng-hide

I'm developing an application where a button is to be clicked which then hides or shows a specific element. 我正在开发一个应用程序,其中单击一个按钮即可隐藏或显示特定元素。

I'm achieving this with ng-hide from AngularJS. 我正在使用AngularJS的ng-hide实现此目的。 For some reason the transition isn't working correctly. 由于某种原因,过渡无法正常进行。 I'm quite new to transitions with CSS3 so what am I doing wrong? 我对CSS3的过渡还很陌生,所以我做错了什么?

All I wish for it to do is a smooth fade in fade out effect so the appearance doesn't seem so un-natural 我希望它能做的是平滑的淡入淡出效果,因此外观看起来不会那么不自然

CSS3 CSS3

#custom-url{
    -webkit-transition: all 2s ease;      
     transition: all 2s ease;
}
#custom-url .ng-hide{
    opacity: 0;
}

HTML HTML

<input ng-model="custom_url" id="custom-url" ng-show="customMode" type="text" placeholder="Place your custom URL text here" class="ng-hide form-control"/>
<button class="btn btn-success" ng-click="customMode = !customMode">Make my own URL <b class="glyphicon glyphicon-heart"></b></button></div>

AngularJS AngularJS

(function(angular) {
    angular.module('urlShortener', [])
        .controller('shortenerController', function($scope){
            $scope.customMode = false;
        })
})(window.angular);

Plunker Plunker

Can anyone help ? 有人可以帮忙吗?

You have couple of issues here. 您这里有几个问题。

1) When you use ng-show/ng-hide it applies class .ng-hide on the element which sets the display property to none and that is a property which cannot be animated hence your opacity rule does not get apply. 1)当您使用ng-show / ng-hide时,它将在元素上应用.ng-hide类,该元素将display属性设置为none并且该属性不能设置动画,因此您的不透明度规则将不适用。 In order to use the animation with ng-show/ng-hide you need to use ng-animate which defers setting the property by adding some intermediate classes in order for animation to complete. 为了将动画与ng-show/ng-hide一起使用,您需要使用ng-animate ,它通过添加一些中间类来延迟设置属性以使动画完成。 Here is a nice tutorial also this one . 这里是一个很好的教程也是这一个

2) ng-hide is applied on the element itself not on its descendant, hence your rule #custom-url .ng-hide will have no effect. 2) ng-hide应用于元素本身而不是其后代,因此您的规则#custom-url .ng-hide将无效。 It should actually be #custom-url.ng-hide instead. 实际上,它应该是#custom-url.ng-hide

3) If you do not wish to use angular-animate library then you would need to use ng-class instead. 3)如果您不想使用angular-animate库,则需要使用ng-class

Example with ng-animate ng-animate的示例

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

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