【问题标题】:css3 animation timing differs between firefox and other browsersFirefox和其他浏览器之间的css3动画时间不同
【发布时间】:2016-06-17 03:40:09
【问题描述】:

我有几个方形 div,我从 0 度旋转到 90 度,然后再转回 0。每个方形中有 2 个图像,绝对位于彼此之上。

图像之间的可见性切换,但您不应该看到切换。它应该发生在正方形旋转 90 度(垂直于屏幕)并且因此不可见时。

我的问题:在 firefox 中的时间是完美的,但在 chrome、safari 和 IE 中是关闭的。我不知道为什么。

我正在使用 css3 动画和关键帧来控制方形容器 div 的旋转,并通过切换“隐藏”图像的 z-index 来更改图像的可见性。

注意事项:我正在使用 PHP 获取一个随机数,然后将其作为动画延迟值的内联样式插入。我最初使用 jquery .css() 执行此操作,但我试图尽量减少前端的工作,因为这似乎是时间问题所在。

代码如下:

.pair-container {	
	-webkit-animation-name: rotate;
	-webkit-animation-duration: 8s;
	-webkit-animation-iteration-count: infinite;
	
	animation-name: rotate;
	animation-duration: 8s;
	animation-iteration-count: infinite;
					
	display:inline-block;
	height:150px;
	margin:5px; 
	position:relative;
	width:150px;
}
.pair-container a {
    left: 0;
    position: absolute;
    top: 0;
	z-index:0;
}
.pair-container a:first-child {
	-webkit-animation-name: flip;	
	-webkit-animation-delay: 2s;	
	-webkit-animation-duration: 8s;	  
	-webkit-animation-iteration-count: infinite;  
	
	animation-name: flip;	
	animation-delay: 2s;	
	animation-duration: 8s;	  
	animation-iteration-count: infinite;   
}

@keyframes rotate {
  0% {
    -webkit-transform: rotateY(0deg);
    transform: rotateY(0deg);
  }
  25% {
    -webkit-transform: rotateY(90deg);
    transform: rotateY(90deg);
  }
  50% {
    -webkit-transform: rotateY(0deg);
    transform: rotateY(0deg);
  }
  75% {
    -webkit-transform: rotateY(90deg);
    transform: rotateY(90deg);
  }  
  100% {
    -webkit-transform: rotateY(0deg);
    transform: rotateY(0deg);
  }
}
@keyframes flip {
	0% { 
 		z-index: 0;	
	}
	25% {
		z-index: 1;	
	}
	50% { 
 		z-index: 1;	
	}
	75% {
		z-index: 1;	
	}
	100% { 
 		z-index: 0;	
	}
		
}
<div style="animation-delay:2s;" class="pair-container">
  <a target="_blank" href="https://www.google.com" style="animation-delay:2s;">
    <img src="http://s24.postimg.org/5o8rk8i8x/fire_drink.jpg">
  </a>
  <a target="_blank" href="https://www.google.com">
    <img src="http://s27.postimg.org/g1bk23lyn/food.jpg">
  </a>
</div>

<div style="animation-delay:3s;" class="pair-container">
  <a target="_blank" href="https://www.google.com" style="animation-delay:3s;">
    <img src="http://s27.postimg.org/g1bk23lyn/food.jpg">
  </a>
  <a target="_blank" href="https://www.google.com">
    <img src="http://s24.postimg.org/5o8rk8i8x/fire_drink.jpg">
  </a>
</div>

【问题讨论】:

    标签: css


    【解决方案1】:

    这段代码

        0% { 
            z-index: 0; 
        }
        25% {
            z-index: 1; 
        }
    

    是问题所在。您需要 z-index 的更改恰好发生在 25% 处,但此代码不保证这一点。在 Chrome 中,这一比例为 12.5%。

    实现这一点的正确代码是:

        0%, 24.99% { 
            z-index: 0; 
        }
        25% {
            z-index: 1; 
        }
    

    修正了 sn-p

    .pair-container {	
    	-webkit-animation-name: rotate;
    	-webkit-animation-duration: 8s;
    	-webkit-animation-iteration-count: infinite;
    	
    	animation-name: rotate;
    	animation-duration: 8s;
    	animation-iteration-count: infinite;
    					
    	display:inline-block;
    	height:150px;
    	margin:5px; 
    	position:relative;
    	width:150px;
    }
    .pair-container a {
        left: 0;
        position: absolute;
        top: 0;
    	z-index:0;
    }
    .pair-container a:first-child {
    	-webkit-animation-name: flip;	
    	-webkit-animation-delay: 2s;	
    	-webkit-animation-duration: 8s;	  
    	-webkit-animation-iteration-count: infinite;  
    	
    	animation-name: flip;	
    	animation-delay: 2s;	
    	animation-duration: 8s;	  
    	animation-iteration-count: infinite;   
    }
    
    @keyframes rotate {
      0% {
        -webkit-transform: rotateY(0deg);
        transform: rotateY(0deg);
      }
      25% {
        -webkit-transform: rotateY(90deg);
        transform: rotateY(90deg);
      }
      50% {
        -webkit-transform: rotateY(0deg);
        transform: rotateY(0deg);
      }
      75% {
        -webkit-transform: rotateY(90deg);
        transform: rotateY(90deg);
      }  
      100% {
        -webkit-transform: rotateY(0deg);
        transform: rotateY(0deg);
      }
    }
    @keyframes flip {
    	0%, 24.99% { 
     		z-index: 0;	
    	}
    	25% {
    		z-index: 1;	
    	}
    	50% { 
     		z-index: 1;	
    	}
    	75% {
    		z-index: 1;	
    	}
    	75.01%, 100% { 
     		z-index: 0;	
    	}
    		
    }
    <div style="animation-delay:2s;" class="pair-container">
      <a target="_blank" href="https://www.google.com" style="animation-delay:2s;">
        <img src="http://s24.postimg.org/5o8rk8i8x/fire_drink.jpg">
      </a>
      <a target="_blank" href="https://www.google.com">
        <img src="http://s27.postimg.org/g1bk23lyn/food.jpg">
      </a>
    </div>
    
    <div style="animation-delay:3s;" class="pair-container">
      <a target="_blank" href="https://www.google.com" style="animation-delay:3s;">
        <img src="http://s27.postimg.org/g1bk23lyn/food.jpg">
      </a>
      <a target="_blank" href="https://www.google.com">
        <img src="http://s24.postimg.org/5o8rk8i8x/fire_drink.jpg">
      </a>
    </div>

    【讨论】:

    • 很高兴它有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-04
    • 1970-01-01
    • 2015-08-02
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    相关资源
    最近更新 更多