CSS使用过渡和动画

4 篇文章 0 订阅
4 篇文章 0 订阅

鼠标过渡

代码片

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        p{
            margin: 0 auto;						//居中
            width: 400px;						//p标签的静态样式
            height: 400px;						//p标签的静态样式
            background-color: #ff7d82;			//p标签的静态样式
        }
        p:hover{
            width: 800px;						//过渡后的样式
            height: 800px;						//过渡后的样式
            background-color: #e6e8ea;			//过渡后的样式
            transition-delay: 0.2s;     			/*触发延迟时间*/
            transition-duration:1000ms;    		    /*过渡持续时间*/
        }
    </style>
</head>
<body >
    <p></p>
</body>
</html>
  • transition-property:background-color;
    可以选择要进行过渡的内容,e.g. width height color等
  • transition-timing-function: ease-in-out;
    使用贝塞尔曲线控制速度
    1. ease 默认状态,匀速
    2. ease-in 减速
    3. ease-out 加速
    4. ease-in-out 先减速后加速
      过渡

Animation

代码片

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        p{
            width: 100px;
            height: 100px;
            background: skyblue;
            margin: 0 auto;
        }
        p:hover{

            -webkit-animation-duration: 2s;
            -moz-animation-duration: 800ms;
            -o-animation-duration: 800ms;
            animation-duration: 800ms;          /*duration*/
            -webkit-animation-delay: 100ms;
            -moz-animation-delay: 100ms;
            -o-animation-delay: 100ms;
            animation-delay: 100ms;             /*delay*/
            -webkit-animation-name: aini;
            -moz-animation-name: aini;
            -o-animation-name: aini;
            animation-name: aini;               /*name*/
            -webkit-animation-iteration-count: infinite;
            -moz-animation-iteration-count: infinite;
            -o-animation-iteration-count: infinite;
            animation-iteration-count: infinite;    /*次数*/
            animation-direction: alternate;         /*反方向运行*/
        }
        @keyframes aini {
            from{
                width: 600px;
                height: 600px;
                background-color: palevioletred;
            }
            to{
                width: 700px;
                height: 700px;
                background-color: lightskyblue;
            }
        }
    </style>
</head>
<body>
    <p></p>
</body>
</html>
  • animation-iteration-count: infinite;
    设置循环的次数
    1. infinite 无限次循环
    2. revert 循环一次
    3. 加数字,循环具体次数
  • animation-direction:alternate
    设置反方向运行

当@keyframes中有from属性时,动画会从from开始,至to结束,当没有from时,将会由原属性至to,大约每两个属性之间时间都是相同的
animation

区别

  • 过渡在鼠标点击之后会暂停,当动作停止时会停在结束样式上。
  • 动画会按照约定的动作一直进行下去。直到结束再停止,恢复最初的状态。
    在这里插入图片描述在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值