ios多个animation动画执行使用rotateY旋转重叠问题

Posted vieber

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ios多个animation动画执行使用rotateY旋转重叠问题相关的知识,希望对你有一定的参考价值。

问题背景

ios上面,做了两个动画,都是使用animation,一个动画盖在另外一个动画上面,然后把下面的动画一直在旋转,上面的绕Y轴旋转,在ios上面就会有一半的动画卡进去,可以看下这个效果

这是代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .test-card 
            display: flex;
            flex-direction: column;
            align-items: center;
            position: absolute;
            z-index: 14;
            top: 50%;
            left: 50%;
            width: rem(560);
            transform: translateX(-50%) translateY(-50%) translateZ(1px);
            background-size: 100% 100%;
            animation-name: test-card;
            animation-delay: 1s;
            animation-duration: 1s;
            animation-timing-function: linear;
            animation-iteration-count: 0.99;
            animation-fill-mode: forwards;
        
        .test-end 
            position: absolute;
            z-index: 1;
            top: 50vh;
            left: 50vw;
            transform: translateX(-50%) translateY(-50%);
            animation-name: testEnd;
            animation-delay: 1s;
            animation-duration: 18s;
            animation-iteration-count: infinite;
            animation-fill-mode: forwards;
        
        @keyframes testEnd 
            0% 
                transform: translateX(-50%) translateY(-50%) scale(1.2) rotateZ(0);
            

            100% 
                transform: translateX(-50%) translateY(-50%) scale(1.2) rotateZ(360deg);
            
        

        @keyframes test-card 
            0% 
                transform: translateX(-50%) translateY(-50%) scale(0) rotateY(180deg) rotateZ(0deg);
            

            30% 
                transform: translateX(-50%) translateY(-50%) scale(0.2) rotateY(180deg) rotateZ(360deg);
            

            50% 
                transform: translateX(-50%) translateY(-50%) scale(0.5) rotateY(180deg) rotateZ(360deg);
            

            100% 
                transform: translateX(-50%) translateY(-50%) scale(1.3) rotateY(360deg) rotateZ(360deg);
            
        
        .test-img 
            width: 100vw;
        
    </style>
</head>
<body>
    <div class="test-end">test-endtest-endtest-endfdsafsda
        <img  class="test-img" src="https://t7.baidu.com/it/u=91673060,7145840&fm=193&f=GIF" />
    </div>
    <div class="test-card">test-cardtest-cardtest-cardfafsdafsda
        <img class="test-img" src="https://t7.baidu.com/it/u=737555197,308540855&fm=193&f=GIF" alt="">
    </div>
    <select></select>
    <script></script>
</body>
</html>

解决方案

试了z-index的值发现不管用,z-index改的再大也会也会卡出问题。因为设置了transform之后,z-index的会生效。具体原因是因为:给元素设置transform属性会创建一个新的stacking context。生成了一个新的层叠上下文。
所以两个的z-index都是相对于现在的这个层级,无论设置为多大,其实就是新的层级里面自己比较。所以没有生效。

方案1

解决方案就是给他们各包一个父元素没有transform的,这样再使用z-index,定位让他们形成不同的层级,这样他们内部在使用transform形成新的层级,也只是内部再形成了。
解决之后的效果

解决之后的代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .test-card 
            display: flex;
            flex-direction: column;
            align-items: center;
            position: fixed;
            z-index: 1400;
            top: 50%;
            left: 50%;
            width: rem(560);
            transform: translateX(-50%) translateY(-50%) translateZ(1px);
            background-size: 100% 100%;
            animation-name: test-card;
            animation-delay: 1s;
            animation-duration: 1s;
            animation-timing-function: linear;
            animation-iteration-count: 0.99;
            animation-fill-mode: forwards;
        
        .test-end 
            position: absolute;
            z-index: 1;
            top: 50vh;
            left: 50vw;
            transform: translateX(-50%) translateY(-50%);
            animation-name: testEnd;
            animation-delay: 1s;
            animation-duration: 18s;
            animation-iteration-count: infinite;
            animation-fill-mode: forwards;
        
        @keyframes testEnd 
            0% 
                transform: translateX(-50%) translateY(-50%) scale(1.2) rotateZ(0);
            

            100% 
                transform: translateX(-50%) translateY(-50%) scale(1.2) rotateZ(360deg);
            
        

        @keyframes test-card 
            0% 
                transform: translateX(-50%) translateY(-50%) scale(0) rotateY(180deg) rotateZ(0deg);
            

            30% 
                transform: translateX(-50%) translateY(-50%) scale(0.2) rotateY(180deg) rotateZ(360deg);
            

            50% 
                transform: translateX(-50%) translateY(-50%) scale(0.5) rotateY(180deg) rotateZ(360deg);
            

            100% 
                transform: translateX(-50%) translateY(-50%) scale(1.3) rotateY(360deg) rotateZ(360deg);
            
        
        .test-img 
            width: 100vw;
        
        .parent-low 
            position: relative;
            z-index: 1;
        
        .parent 
            position: relative;
            z-index: 10;
        
    </style>
</head>
<body>
    <div class="parent-low">
        <div class="test-end">test-endtest-endtest-endfdsafsda
            <img  class="test-img" src="https://t7.baidu.com/it/u=91673060,7145840&fm=193&f=GIF" />
        </div>
    </div>
    <div class="parent">
        <div class="test-card">test-cardtest-cardtest-cardfafsdafsda
            <img class="test-img" src="https://t7.baidu.com/it/u=737555197,308540855&fm=193&f=GIF" alt="">
        </div>
    </div>
    <select></select>
    <script></script>
</body>
</html>

方案二

把想要设置的上层元素的translateZ值设置大一点,也可以解决这个问题。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .test-card 
            display: flex;
            flex-direction: column;
            align-items: center;
            position: fixed;
            z-index: 1400;
            top: 50%;
            left: 50%;
            width: rem(560);
            transform: translateX(-50%) translateY(-50%) translateZ(1000px);
            background-size: 100% 100%;
            animation-name: test-card;
            animation-delay: 1s;
            animation-duration: 1s;
            animation-timing-function: linear;
            animation-iteration-count: 0.99;
            animation-fill-mode: forwards;
        
        .test-end 
            position: absolute;
            z-index: 1;
            top: 50vh;
            left: 50vw;
            transform: translateX(-50%) translateY(-50%);
            animation-name: testEnd;
            animation-delay: 1s;
            animation-duration: 18s;
            animation-iteration-count: infinite;
            animation-fill-mode: forwards;
        
        @keyframes testEnd 
            0% 
                transform: translateX(-50%) translateY(-50%) scale(1.2) rotateZ(0);
            

            100% 
                transform: translateX(-50%) translateY(-50%) scale(1.2) rotateZ(360deg);
            
        

        @keyframes test-card 
            0% 
                transform: translateX(-50%) translateY(-50%) translateZ(1000px) scale(0) rotateY(180deg);
            

            30% 
                transform: translateX(-50%) translateY(-50%) translateZ(1000px) rotateY(180deg);
            

            50% 
                transform: translateX(-50%) translateY(-50%) translateZ(1000px) rotateY(180deg);
            

            100% 
                transform: translateX(-50%) translateY(-50%) translateZ(1000px) rotateY(360deg);
            
        
        .test-img 
            width: 100vw;
        
        .parent-low 
            position: relative;
            z-index: 1;
        
        .parent 
            position: relative;
            z-index: 10;
        
    </style>
</head>
<body>
    <div class="test-end">test-endtest-endtest-endfdsafsda
        <img  class="test-img" src="https://t7.baidu.com/it/u=91673060,7145840&fm=193&f=GIF" />
    </div>
    <div class="test-card">test-cardtest-cardtest-cardfafsdafsda
        <img class="test-img" src="https://t7.baidu.com/it/u=737555197,308540855&fm=193&f=GIF" alt="">
    </div>
    <select></select>
    <script></script>
</body>
</html>

以上是关于ios多个animation动画执行使用rotateY旋转重叠问题的主要内容,如果未能解决你的问题,请参考以下文章

Tween Animation----Rotate旋转动画

Animation动画详解——layoutAnimation与gridLayoutAnimation

Animation动画详解——animateLayoutChanges与LayoutTransition

SwiftUI 与rotation3DEffect 动画冲突

如何实现Rotate旋转动画的android源代码

vue+ webpack中的animate.css实现的执行多个连续的动画