我正在尝试使用此问题的解决方案:
css3 transition animation on load?
而且我有以下内容作为我的HTML和CSS:
@keyframes slideInFromRight {
0% {
transform: translateX(100%);
}
100% {
transform: translateX(0);
}
}
.announcements-container {
position: fixed;
top: 80px;
right: 20px;
z-index: 1001;
display: flex;
flex-direction: column;
width: 300px;
/* animation: 1s ease-out 0s 1 slideInFromRight; */
}
.announcments-1 {
animation: 1s ease-out 0s 1 slideInFromRight;
}
.announcements-2 {
margin-top: 15px;
}
.annoucements-header {
background-color: #1481C3;
color: #ffffff;
font-family: "Proxima Nova Bold";
padding: 7px 10px;
}
.annoucements-close {
position: absolute;
right: 5px;
width: 24px;
height: 36px;
cursor: pointer;
opacity: .85;
}
.annoucements-close:hover {
opacity: 1;
}
.annoucements-close::before,
.annoucements-close::after {
content: '';
width: 24px;
height: 2px;
background: white;
position: absolute;
top: 7px;
left: 0;
}
.annoucements-close::before {
transform: rotate(-45deg);
}
.annoucements-close::after {
transform: rotate(45deg);
}
<body>
<div class="announcements-container">
<div class="announcements-1">
<div class="annoucements-header">
<span class="annoucement-type-quantity">2 School Announcements</span>
<i class="annoucements-close"></i>
</div>
</div>
<div class="announcements-2">
<div class="annoucements-header">
<span class="annoucement-type-quantity">1 Admin Annoucement</span>
<i class="annoucements-close"></i>
</div>
</div>
</div>
</body>
如您所见,当animation: 1s ease-out 0s 1 slideInFromRight;
应用于inner-div时,它什么也不做。但是,将其应用于容器div时,它确实可以工作:
@keyframes slideInFromRight {
0% {
transform: translateX(100%);
}
100% {
transform: translateX(0);
}
}
.announcements-container {
position: fixed;
top: 80px;
right: 20px;
z-index: 1001;
display: flex;
flex-direction: column;
width: 300px;
animation: 1s ease-out 0s 1 slideInFromRight;
}
.announcments-1 {
/*animation: 1s ease-out 0s 1 slideInFromRight;*/
}
.announcements-2 {
margin-top: 15px;
}
.annoucements-header {
background-color: #1481C3;
color: #ffffff;
font-family: "Proxima Nova Bold";
padding: 7px 10px;
}
.annoucements-close {
position: absolute;
right: 5px;
width: 24px;
height: 36px;
cursor: pointer;
opacity: .85;
}
.annoucements-close:hover {
opacity: 1;
}
.annoucements-close::before,
.annoucements-close::after {
content: '';
width: 24px;
height: 2px;
background: white;
position: absolute;
top: 7px;
left: 0;
}
.annoucements-close::before {
transform: rotate(-45deg);
}
.annoucements-close::after {
transform: rotate(45deg);
}
<body>
<div class="announcements-container">
<div class="announcements-1">
<div class="annoucements-header">
<span class="annoucement-type-quantity">2 School Announcements</span>
<i class="annoucements-close"></i>
</div>
</div>
<div class="announcements-2">
<div class="annoucements-header">
<span class="annoucement-type-quantity">1 Admin Annoucement</span>
<i class="annoucements-close"></i>
</div>
</div>
</div>
</body>
但是问题在于,我希望div依次顺次滑入。如果我从最上面的div中“ X”出来,我希望第二个div向上滑动并取代它的位置,这就是为什么我认为有必要使用容器div的原因。
我还能做其他什么使动画一次应用于子div吗?关于关键帧,有什么东西可以防止在内部div上播放动画吗?使用JavaScript更好吗?
起初看起来很奇怪,但是样式表中有一个错字。 在声明announcements-1
但使用announcments-1
进行样式设置的地方。
因此,正确使用这项权利会得到您想要的东西:
@keyframes slideInFromRight {
0% {
transform: translateX(100%);
}
100% {
transform: translateX(0);
}
}
.announcements-container {
position: fixed;
top: 80px;
right: 20px;
z-index: 1001;
display: flex;
flex-direction: column;
width: 300px;
/*animation: 1s ease-out 0s 1 slideInFromRight;*/
}
.announcements-1 {
animation: 1s ease-out 0s 1 slideInFromRight;
}
.announcements-2 {
margin-top: 15px;
}
.annoucements-header {
background-color: #1481C3;
color: #ffffff;
font-family: "Proxima Nova Bold";
padding: 7px 10px;
}
.annoucements-close {
position: absolute;
right: 5px;
width: 24px;
height: 36px;
cursor: pointer;
opacity: .85;
}
.annoucements-close:hover {
opacity: 1;
}
.annoucements-close::before,
.annoucements-close::after {
content: '';
width: 24px;
height: 2px;
background: white;
position: absolute;
top: 7px;
left: 0;
}
.annoucements-close::before {
transform: rotate(-45deg);
}
.annoucements-close::after {
transform: rotate(45deg);
}
<body>
<div class="announcements-container">
<div class="announcements-1">
<div class="annoucements-header">
<span class="annoucement-type-quantity">2 School Announcements</span>
<i class="annoucements-close"></i>
</div>
</div>
<div class="announcements-2">
<div class="annoucements-header">
<span class="annoucement-type-quantity">1 Admin Annoucement</span>
<i class="annoucements-close"></i>
</div>
</div>
</div>
</body>