【问题标题】:Keyframes doesn't work when keyframe-class is added添加关键帧类时关键帧不起作用
【发布时间】:2015-05-21 20:12:24
【问题描述】:

我有一张用户卡,打开时会显示用户的照片。

(https://jsfiddle.net/xv036sny/2/)

这可行,但我想要实现的是使用 CSS 关键帧制作更复杂的动画。

.userCard__image-shown{
    transform: scale(1);
}

我正在尝试通过将关键帧添加到类 .userCard__image-shown

@keyframes test{
    0% {transform: scale(0.1);}
    50% {transform: scale(1.1);}
    100% {transform: scale(1);}
}
.userCard__image-shown{
    animation: test 0.2s;
    animation-iteration-count: 1;
}

关键是当我切换这个类时,它不起作用。 我做错了什么?

(希望你能理解我(还)糟糕的英语)

【问题讨论】:

    标签: jquery css-transitions


    【解决方案1】:

    你没有做错任何事。有时浏览器并不支持所有属性,您可能需要使用供应商前缀来获得最佳浏览器支持。这就是导致动画问题的原因。

    比如动画在 Chrome 上不能正常运行,但是你可以添加-webkit- 前缀就可以解决问题了:

    @-webkit-keyframes test{
        0% {transform: scale(0.1);}
        50% {transform: scale(1.1);}
        100% {transform: scale(1);}
    }
    
    .userCard__image-shown{
        animation: test 0.2s;
        animation-iteration-count: 1;
        transform: scale(1);
        -webkit-animation:test 0.2s;
        -webkit-animation-iteration-count:1;
    }
    

    这是一个使用您的 JSFiddle 中的代码的演示(我将动画持续时间更改为 2 秒,以便可以看到):

    $('#userName-1').on('click', function(){
        $( ".userCard" ).toggleClass('userCard-shown').delay( 300 );
        $( ".userCard__image" ).toggleClass('userCard__image-shown');
    });
    body{overflow: hidden;}img{border-radius: 50%;}
    .userCard{
        background-color: deepskyblue;
        position: absolute;
        right: -300px;
        opacity: 0;
        top: 0;
        bottom: 0;
        width: 300px;
        transition: all 0.2s;
    }
    
    .userCard-shown{
        opacity: 1;
        right: 0;
    }
    
    .userCard__image{
        padding-top: 32px;
        margin: 0 auto;
        position: relative;
        width: 90px;
        transition: all 0.4s;
        transform: scale(0.1);
    }
    
    .userCard__image-shown{
        animation: test 2s;
        animation-iteration-count: 1;
        transform: scale(1);
        -webkit-animation:test 2s;
        -webkit-animation-iteration-count:1;
    }
    
    @keyframes test{
        0% {transform: scale(0.1);}
        50% {transform: scale(1.1);}
        100% {transform: scale(1);}
    }
    
    @-webkit-keyframes test{
        0% {transform: scale(0.1);}
        50% {transform: scale(1.1);}
        100% {transform: scale(1);}
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <a href="#"><span id="userName-1">UserName</span></a>
    
    <div class="userCard">
        <div class="userCard__image">
            <img src="//yt3.ggpht.com/-LYuWGoFbuCs/AAAAAAAAAAI/AAAAAAAAAAA/LYsyfZDIHjc/s100-c-k-no/photo.jpg"/>
        </div>
    </div>

    在 JSFiddle 上也是如此:https://jsfiddle.net/xv036sny/3/

    【讨论】:

    • 所以,canIuse 是在骗我!! caniuse.com/#search=keyframes我以为我不需要前缀
    • 它只是有点“隐藏”。如果你去那个链接,你会看到一些带有绿色框的浏览器上有一个很小的黄色标记,将鼠标悬停在它们上面,你会看到它说“支持前缀:-webkit-”跨度>
    • 哦,现在我明白了。非常感谢您的宝贵时间:D
    猜你喜欢
    • 2014-12-05
    • 2023-04-03
    • 1970-01-01
    • 2020-10-17
    • 2016-12-14
    • 2014-09-20
    • 2023-03-22
    • 2015-03-04
    • 1970-01-01
    相关资源
    最近更新 更多