CSS动画知识点

本文介绍了CSS中的四个关键动画概念:keyframes用于创建自定义动画,animation控制动画的名称、持续时间、速度曲线等;transition实现渐变动画;transform则用于元素的移动、缩放、旋转和倾斜。通过实例演示了如何在HTML中应用这些动画效果。
摘要由CSDN通过智能技术生成


前言

对动画的知识点较为薄弱,通过以下知识点的记录来加深印象


一、@keyframes

@keyframes 规则是创建动画。

@keyframes 规则内指定一个 CSS 样式和动画将逐步从目前的样式更改为新的样式。

注意: 您必须定义动画的名称和动画的持续时间(如 animation: frame 5s;)。如果省略的持续时间,动画将无法运行,因为默认值是0。

 图片颜色由绿色逐渐为蓝色 

<div class="example"></div>
.example {
    width: 200px;
    height: 200px;
    animation: frame 5s; //绑定动画名称 动画时间
    animation-iteration-count: infinite; //无限次播放
}
/*   from-to,等同于0%-100% ; */
@keyframes frame {
    from {
        background-color: green;
    }
    to {
        background-color: blue;
    }
}

/*使用这个动画名称的话,盒子会从宽高为200px的盒子变为宽高为300px*/
@keyframes data {
    0% {
        background-color: red;
    }
    100% {
        background-color: green;
        width: 300px;
        height: 300px;
    }
}

二、animation(实现自定义动画)

简写:animation: name duration timing-function delay iteration-count direction fill-mode play-state;

animation的属性
属性说明
animation-name动画名称

animation-duration 

动画持续时间(动画需要多少秒/毫秒完成)

animation-timing-function

定义动画以怎么样的速度曲线完成一个周期
animation-delay规定动画启动之前的延迟
animation-iteration-count动画的播放次数
animation-derection动画是否应该轮流反向播放
animation-fill-mode规定元素在不播放动画时的样式(在开始前、结束后,或两者同时)。
animation-play-state规定动画是否正在运行/暂停
.example {
    width: 200px;
    height: 200px;
    animation: frame 5s linear 4s infinite reverse; /* 绑定动画名称 动画时间 动画匀速变化 动画开始前等待的时间 动画的播放次数 动画是否循环交替反向播放动画 */
    animation-name: frame; /* 动画名称 */
    animation-duration: 5s; /* 动画在多少秒/毫秒内完成 */
    animation-timing-function: linear; /*  动画速度变化 匀速 */
    animation-delay: 4s; /*  动画在4s后开始 */
    animation-iteration-count: infinite; /* 无限次播放 动画的播放次数 */
    animation-direction: reverse; /* 动画的播放方式 反向播放 */
    /*  animation-fill-mode: none; 播放完采用默认形式 */
    /*  animation-play-state:paused; 暂停动画 */
}

@keyframes frame {
    from {
        background-color: green;
    }
    to {
        background-color: blue;
    }
}

三、transition(实现渐变动画)

简写:transition: property duration timing-function delay;

描述
transition-propertyCSS属性,如width,heigtht
transition-durationtransition效果需要多少s/ms完成
transition-timing-functiontransition效果的速度曲线(匀速等)
transition-delaytransition效果开始前延误的时间

从一个宽为200px的元素在5s后变为宽为500px的元素

.example {
    width: 200px;
    height: 200px;
    background-color: #f5222d;
    transition: width 5s;
    &:hover {
        width: 500px;
    }
}

四、transform(转变动画)

描述
translate

移动

scale缩放
rotate旋转
skew倾斜

.example {
    width: 200px;
    height: 200px;
    background-color: pink;
    /* transform: translate(300px, 300px); */
    /* transform: rotate(45deg); 旋转45度 */
    transform: scale(0.5); //放大1.5
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值