是否可以在 CSS 动画上交替使用 z-index
Is it possible to alternate z-index on CSS animation
我已经尝试了多种方法,但都没有成功。 JavaScript、TweenLite 等。我需要交替使用两个 <div>
标签,slide1 和 slide2,它们位于 幻灯片。一个淡出一个淡入。一个连续的循环。我可以使用基本的 css @keyframes
动画,如下所示。它工作正常,但它们是可点击的链接,并且一个 <div>
将始终保持在顶部。
有没有办法在 @keyframes
动画中循环每个 div 的 z-index?
或者更好的方法?
CSS
.slider {
max-width: 300px;
height: 200px;
margin: 20px auto;
position: relative;
}
.slide1,.slide2 {
position: absolute;
width: 100%;
height: 100%;
}
.slide1 {
animation:fade 8s infinite;
-webkit-animation:fade 8s infinite;
}
.slide2 {
animation:fade2 8s infinite;
-webkit-animation:fade2 8s infinite;
}
@keyframes fade
{
0% {opacity:1}
33.333% { opacity: 0}
66.666% { opacity: 0}
100% { opacity: 1}
}
@keyframes fade2
{
0% {opacity:0}
33.333% { opacity: 1}
66.666% { opacity: 0 }
100% { opacity: 0}
}
HTML
<div class="slider">
<div class="slide1"></div>
<div class="slide2"></div>
</div>
他们似乎在为我工作,请查看:https://codepen.io/giovannipds/pen/LzgYaa
<style>
.slider {
line-height: 1.5;
height: 200px;
margin: 20px auto;
position: relative;
width: 300px;
}
.slide {
padding: 20px;
position: absolute;
width: 100%;
height: 100%;
}
.slide,
.slide a {
color: #fff;
}
.slide1 {
animation: fade 8s infinite;
background: red;
}
.slide2 {
animation: fade2 8s infinite;
background: blue;
}
@keyframes fade
{
0% { opacity: 1 ; z-index: 2; }
33.333% { opacity: 0; z-index: 1; }
66.666% { opacity: 0; z-index: 1; }
100% { opacity: 1; z-index: 2; }
}
@keyframes fade2
{
0% { opacity: 0; z-index: 1; }
33.333% { opacity: 1; z-index: 2; }
66.666% { opacity: 1; z-index: 2; }
100% { opacity: 0; z-index: 1; }
}
</style>
<div class="slider">
<div class="slide slide1">
<ul>
<li><a href="//www.google.com" target="_blank">Google</a></li>
<li><a href="//www.globo.com" target="_blank">Globo.com</a></li>
</ul>
</div>
<div class="slide slide2">
<ul>
<li><a href="//www.lovemondays.com.br" target="_blank">Love Mondays</a></li>
<li><a href="//www.hotmail.com" target="_blank">Hotmail</a></li>
</ul>
</div>
</div>
我已经尝试了多种方法,但都没有成功。 JavaScript、TweenLite 等。我需要交替使用两个 <div>
标签,slide1 和 slide2,它们位于 幻灯片。一个淡出一个淡入。一个连续的循环。我可以使用基本的 css @keyframes
动画,如下所示。它工作正常,但它们是可点击的链接,并且一个 <div>
将始终保持在顶部。
有没有办法在 @keyframes
动画中循环每个 div 的 z-index?
或者更好的方法?
CSS
.slider {
max-width: 300px;
height: 200px;
margin: 20px auto;
position: relative;
}
.slide1,.slide2 {
position: absolute;
width: 100%;
height: 100%;
}
.slide1 {
animation:fade 8s infinite;
-webkit-animation:fade 8s infinite;
}
.slide2 {
animation:fade2 8s infinite;
-webkit-animation:fade2 8s infinite;
}
@keyframes fade
{
0% {opacity:1}
33.333% { opacity: 0}
66.666% { opacity: 0}
100% { opacity: 1}
}
@keyframes fade2
{
0% {opacity:0}
33.333% { opacity: 1}
66.666% { opacity: 0 }
100% { opacity: 0}
}
HTML
<div class="slider">
<div class="slide1"></div>
<div class="slide2"></div>
</div>
他们似乎在为我工作,请查看:https://codepen.io/giovannipds/pen/LzgYaa
<style>
.slider {
line-height: 1.5;
height: 200px;
margin: 20px auto;
position: relative;
width: 300px;
}
.slide {
padding: 20px;
position: absolute;
width: 100%;
height: 100%;
}
.slide,
.slide a {
color: #fff;
}
.slide1 {
animation: fade 8s infinite;
background: red;
}
.slide2 {
animation: fade2 8s infinite;
background: blue;
}
@keyframes fade
{
0% { opacity: 1 ; z-index: 2; }
33.333% { opacity: 0; z-index: 1; }
66.666% { opacity: 0; z-index: 1; }
100% { opacity: 1; z-index: 2; }
}
@keyframes fade2
{
0% { opacity: 0; z-index: 1; }
33.333% { opacity: 1; z-index: 2; }
66.666% { opacity: 1; z-index: 2; }
100% { opacity: 0; z-index: 1; }
}
</style>
<div class="slider">
<div class="slide slide1">
<ul>
<li><a href="//www.google.com" target="_blank">Google</a></li>
<li><a href="//www.globo.com" target="_blank">Globo.com</a></li>
</ul>
</div>
<div class="slide slide2">
<ul>
<li><a href="//www.lovemondays.com.br" target="_blank">Love Mondays</a></li>
<li><a href="//www.hotmail.com" target="_blank">Hotmail</a></li>
</ul>
</div>
</div>