gpt4 book ai didi

css - AngularJS CSS 动画 - 同时为两个元素设置动画

转载 作者:行者123 更新时间:2023-11-28 08:31:16 25 4
gpt4 key购买 nike

我正在学习 AngularJs,特别是 Angular 动画。我在 html 中有一个切换开关,它当前转换 div 类“my-first-animation”。如何同时对背景应用过渡?也就是说,当 div 类“my-first-animation”出现时,我希望 body 的背景颜色从白色逐渐变为黑色。提前致谢。更新:K.Torres 回答工作非常好。谢谢。但是,我尝试将以下 dark-bg 转换添加到 CSS,但它似乎不起作用。有没有人不知道为什么?谢谢。

 <!DOCTYPE html>
<html ng-app="app">
<head>
<title> Applying Animations</title>
<script src="js/angular.js"></script>
<script src="js/angular-animate.min.js"></script>
<script type="text/javascript">
var app = angular.module('app', ['ngAnimate']);
app.controller('homeCtrl', function ($scope){
$scope.on = false;
});
</script>

<style>
/*start*/
.my-first-animation.ng-enter{
transition: .5s all;
opacity: 0;
}
.my-first-animation.ng-enter-active{
opacity: 1;
}
.my-first-animation.ng-leave{
transition: .5s all;
opacity: 1;
}
.my-first-animation.ng-leave-active{
opacity: 0;
}



.dark-bg.ng-add {
transition: .5s all;
background:white;

}
.dark-bg.ng-add-active {
background:gray;

}
.dark-bg.ng-remove {
transition: .5s all;
background:gray;

}
.dark-bg.ng-remove-active {
background:white;

}
</style>
</head>
<body ng-controller="homeCtrl" ng-class="{ 'dark-bg': on }">
<button ng-click="on=!on">Toggle Content</button>
<div class="my-first-animation" ng-if="on">
This content will fade in over a half second.
</div>
</body>
</html>

最佳答案

使用ng-class<body>标记,这里是 ngClass 的文档

 <body ng-controller="MainCtrl" ng-class="{ 'dark-bg': on }" >

这将添加 css 类 dark-bg<body>标记时 on变量为真,

当你点击按钮on变量将在 true 之间切换& false , 和 ng-class取决于on也是可变的,所以如果 ontrue body会得到 dark-bg css 类,如果onfalse然后 dark-bg css 类将从 body 中删除.

然后你可以添加

.dark-bg {
background:gray;
transition: .5s all;
}

这里是 Plunker Demo

关于css - AngularJS CSS 动画 - 同时为两个元素设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28260484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com