是否可以在CSS动画上交替使用z-index

时间:2017-10-14 18:46:12

标签: css css3 z-index keyframe

我一直在尝试这种方法但没有成功。 JavaScript,TweenLite等。我需要替换两个<div>标记, slide1 &amp; 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>

1 个答案:

答案 0 :(得分:1)

他们似乎在为我工作,请查看: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>