gpt4 book ai didi

css - 如何让父div的动画不在子div中播放?

转载 作者:行者123 更新时间:2023-11-27 23:01:33 24 4
gpt4 key购买 nike

我在父 div 中播放一个简单的动画,但我希望它在父 div 中播放,而不是在任何后续子 div 中播放。

我已经尝试过animation-play-state 并试图弄乱图像属性,但到目前为止没有成功。

我正在使用动画来改变父 div 中图像的亮度,这显然会改变子 div 中的亮度。

<style type="text/css">body {
background-image: url("https://www.ecosia.org/images/?q=ocean%20image#id=73D94E9737B0E809884B687B3BD30B3AE1F46394");
background-repeat: no-repeat;
background-size: cover;
background-attachment: fixed;
}

* {
box-sizing: border-box;
}

div.Main {
animation-name: Image;
animation-duration: 10s;
animation-direction: alternate;
animation-timing-function: linear;
animation-iteration-count: infinite;
opacity: 0.75;
background-image: url("https://www.ecosia.org/images/?q=ocean%20image#id=73D94E9737B0E809884B687B3BD30B3AE1F46394");
background-repeat: no-repeat;
background-size: cover;
background-attachment: fixed;
width: 99%;
height: 655px;
position: absolute;
}

@keyframes Image {
0% {
filter: brightness(100%);
}
50% {
filter: brightness(160%);
}
100% {
filter: brightness(100%);
}
}

.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
}

.dropdown {
position: relative;
display: inline-block;
background-color: Black;

}

.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}

.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}

.dropdown-content a:hover {
background-color: #096;
}

.dropdown:hover .dropdown-content {
display: block;
}

.dropdown:hover .dropbtn {
background-color: #3FF;
}

</style>
<body bgcolor="#0066CC">

<div class="Main">
<div class="dropdown">
<button class="dropbtn">Dropdown</button>
<div class="dropdown-content">
<a href="#">Menu</a>
<a href="#">Menu</a>
<a href="#">Menu</a>
</div>
</div>
</div>


</body>

最佳答案

这里的问题不是因为动画正在 div.Main 的子级上播放,而是因为 filter 属性的工作方式。滤镜作为图形效果应用在整个元素之上,包括其子元素。

您可以将背景图像设置为与其他元素处于同一级别或位于其他元素之下的单独元素,这样可以避免这种情况。

<style type="text/css">* {
box-sizing: border-box;
}

#image {
/* Here */
animation-name: image;
animation-duration: 10s;
animation-direction: alternate;
animation-timing-function: linear;
animation-iteration-count: infinite;
opacity: 0.75;
background-image: url("https://upload.wikimedia.org/wikipedia/commons/e/e0/Clouds_over_the_Atlantic_Ocean.jpg");
background-repeat: no-repeat;
background-size: cover;
background-attachment: fixed;
width: 99%;
height: 655px;
position: absolute;
}

@keyframes image {
0% {
filter: brightness(100%);
}
50% {
filter: brightness(160%);
}
100% {
filter: brightness(100%);
}
}

.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
}

.dropdown {
position: relative;
display: inline-block;
background-color: Black;
}

.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}

.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}

.dropdown-content a:hover {
background-color: #096;
}

.dropdown:hover .dropdown-content {
display: block;
}

.dropdown:hover .dropbtn {
background-color: #3FF;
}

</style>
<body bgcolor="#0066CC">

<div class="main">
<!-- Here -->
<div id="image"></div>
<div class="dropdown">
<button class="dropbtn">Dropdown</button>
<div class="dropdown-content">
<a href="#">Menu</a>
<a href="#">Menu</a>
<a href="#">Menu</a>
</div>
</div>
</div>


</body>

关于css - 如何让父div的动画不在子div中播放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58960934/

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