css3 @keyframe 抖动/变色动画

本文介绍了使用CSS和JavaScript实现元素动画效果的方法,包括通过关键帧动画和定时器触发颜色变化,展示了两种不同技术实现动画的基本步骤和代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.纯css实现

.shake{    //抖动的元素
    width: 200px;
    height: 100px;
    margin: 50px auto;
    background: #f00;
    position: relative;
}


/**step 2**/
@-webkit-keyframes shake{
    0%{
        -webkit-transform:translateX(10px) rotate(10deg);
    }
    20%{
        -webkit-transform:translateX(-7px) rotate(-7deg);
    }
    40%{
         -webkit-transform:translateX(5px) rotate(6deg);
    }
    60%{
        -webkit-transform:translateX(-3px) rotate(-3deg);
    }
    80%{
        -webkit-transform:translateX(6px) rotate(5deg);
    }
   100%{
       -webkit-transform:translateX(-10px) rotate(-10deg);
    }
}


/**step 3**/
.shake:hover{
    -webkit-animation-name: shake;
    -webkit-animation-duration: 0.25s;
    -webkit-animation-timing-function: linear;
    -webkit-animation-iteration-count: 1;
}

 

二.js实现变色动画

html:

<div id='a' style="width:200px; height:200px; margin:0 auto;"></div>

css:

div.a {
    animation: myfirst 1s;
    -webkit-animation: myfirst 1s;
}

<style id="dynamic">
</style>

js:

var colors = ['red','yellow','blue','green']


window.setTimeout(function (){   //每隔一秒,取数组中的颜色值,作为div.a的背景动画颜色,再消除颜色动画(既消除背景色)
    var color = colors.shift();
    console.log(color);
    if (!color) return


    var style = document.getElementById("dynamic");   //给style页内标签加入keyframes动画


    style.innerHTML = '@-webkit-keyframes myfirst{50% {background: '+color+';} }\n'+ '@keyframes myfirst {50% {background: '+color+';}}'


    var a = document.getElementById('a')

    a.className = 'a'

    window.setTimeout(function(){
        a.className = ''
    },1000)


    window.setTimeout(arguments.callee,1500);

 

},1000) 

 

转载于:https://www.cnblogs.com/jlliu/p/5319202.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值