web基础学习(十)CSS3之 @keyframes 、animation

css3新增属性@keyframes(关键帧),可以帮助开发者不必依赖JavaScript,只使用css代码完成动画制作
那么如何使用@keyframes呢?
这里有两个重要知识点:

1.@keyframes 定义关键帧
  • 语法:

    @keyframes 两帧动画名称 { from {top:0px;} to {top:200px;} }
    @keyframes 多帧动画名称 { 0% { transform: translate(100px, 0); } 50% {transform: translate(200px, 200px); } 100%{ transform: translate(0, 100px); } }

动画内可以任意自定义合法的 CSS 样式属性

/*两帧*/
@keyframes mymove
{
	from {top:0px;}/*第1帧*/
	to {top:200px;}/*第2帧*/
}
/*多帧*/
@keyframes mymove {
	/*第1帧*/
    0% {
         transform: translate(100px, 0);
     }
     /*第2帧*/
     25% {
         transform: translate(200px, 0);
     }
     /*第3帧*/
     50% {
         transform: translate(200px, 200px);
     }
     /*第4帧*/
     75% {
         transform: translate(0px, 200px);
     }
     /*第5帧*/
     100% {
         transform: translate(0, 100px);
     }
 }
2. animation 使用帧动画

animation-name: 动画名称 (必须)
animation-duration: 动画运行时间(必须)
animation-timing-function: 动画运行方式速度曲线 如:linear ,ease ,ease-in.ease-out,ease-in-out
animation-delay: 动画延时时间
animation-iteration-count: 动画播放次数填写数字,如果是 infinite:一直播放
animation-direction: 动画下一周期轮流反向播放动画 ,默认是normalalternate :动画轮流反转播放
animation-play-state: 指定动画是否正在运行或已暂停, paused:停止 。running:运行
animation-fill-mode: 规定当动画不播放时(当动画完成时,或当动画有一个延迟未开始播放时),动画状态。

none:不改变默认状态。
forwards:保持最后一帧(最后一帧中定义),
backwards:在animation-delay所指定的一段时间内。动画开始之前,应用开始属性也就是起始位置在第一帧的位置(第一帧中定义)
both: forwards,backwards属性都被应用

综合方式暂时不受填写顺序影响,按照习惯应该如上的流程填写属性

.container{
	width: 200px;
    height: 200px;
    /*单独定义*/
    background-color: yellow;
    /*动画名称 (必须)*/
	animation-name: mymove;
	/*动画运行时间(必须)*/
    animation-duration: 10s;
    /*动画运行方式速度曲线*/
    animation-timing-function: linear;
    /*动画延时时间*/
    animation-delay: 2s;
    /*动画播放次数*/
    animation-iteration-count: 1;
    /*动画下一周期轮流反向播放动画*/
    animation-direction: alternate;
    /*指定动画是否正在运行或已暂停。*/
    animation-play-state: running;
    /*规定当动画不播放时(当动画完成时,或当动画有一个延迟未开始播放时),动画状态。*/
    animation-fill-mode: both;
  	/*综合定义*/
    animation:  mymove 10s infinite both ease 2s alternate;
    /*名称和时间必须定义*/
    animation:  mymove 10s ;
}
  • 实战示例:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">

        @keyframes boxmove {
            0% {
                transform: translate(100px, 0);
            }
            25% {
                transform: translate(200px, 0);
            }
            50% {
                transform: translate(200px, 200px);
            }
            75% {
                transform: translate(0px, 200px);
            }
            100% {
                transform: translate(0, 100px);
            }
        }

        .box {
            width: 200px;
            height: 200px;
            background-color: yellow;
            animation: boxmove 10s linear;
            animation-name: boxmove;
            animation-duration: 10s;
            animation-timing-function: linear;
            animation-delay: 2s;
            animation-iteration-count: 1;
            animation-direction: alternate;
            animation-play-state: running;
            animation-fill-mode: both;
            animation: boxmove 10s both ease 2s alternate infinite;
        }
    </style>

</head>
<body>
<div class="box" id="box"></div>
</body>
</html>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值