css3 动画

本文深入探讨了CSS中的过渡(transition)和动画(animation)特性,通过实例展示了如何改变元素的大小、颜色以及创建复杂的动画效果。讲解了关键帧(keyframes)的使用,包括从简化的语法到高级技巧如steps函数的应用,以及如何控制动画的暂停与继续。此外,还提到了浏览器兼容性和实际应用中的注意事项。
摘要由CSDN通过智能技术生成
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        img {
            height: 15px;
            width: 15px;
        }

        img:hover {
            height: 450px;
            width: 450px;
        }

        /* img {
            transition-property: height;
            transition-duration: 1s;
            transition-delay: 1s;
            transition-timing-function: ease;
        } */
        /* 等同于 */
        img {
            /* transition: 1s height, 1s 1s width; */
            /* transition: 1s height cubic-bezier(.83, .97, .05, 1.44); */
            transition: 1s 1s height ease;
        }

        div {
            width: 100px;
            height: 100px;
        }


        /* div:hover {
            animation-name: rainbow;
            animation-duration: 1s;
            animation-timing-function: linear;
            animation-delay: 1s;
            animation-fill-mode: forwards;
            animation-direction: normal;(alternate、reverse、alternate-reverse)
            animation-iteration-count: 3;
        } */
        /* animation-fill-mode还可以使用下列值
        (1)none:默认值,回到动画没开始时的状态。
(2)backwards:让动画回到第一帧的状态。
(3)both: 根据animation-direction(见后)轮流应用forwards和backwards规则。 */
        /* 等同于 */
        div:hover {
            animation: 1s 1s rainbow linear 3 forwards normal;
        }

        @keyframes rainbow {
            0% {
                background: #c00;
            }

            50% {
                background: orange;
            }

            100% {
                background: yellowgreen;
            }

        }
    </style>
</head>

<body>
    <div></div>
    <img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fi04.c.aliimg.com%2Fimg%2Fibank%2F2013%2F211%2F016%2F791610112_758613609.jpg&refer=http%3A%2F%2Fi04.c.aliimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1633674283&t=cf512a2dedee2709f3c890d6df52983c"
        alt="">
</body>

</html>

keyframes关键字用来定义动画的各个状态,它的写法相当自由。


@keyframes rainbow {
  0% { background: #c00 }
  50% { background: orange }
  100% { background: yellowgreen }
}

0%可以用from代表,100%可以用to代表,因此上面的代码等同于下面的形式。


@keyframes rainbow {
  from { background: #c00 }
  50% { background: orange }
  to { background: yellowgreen }
}

如果省略某个状态,浏览器会自动推算中间状态,所以下面都是合法的写法。


@keyframes rainbow {
  50% { background: orange }
  to { background: yellowgreen }
}

@keyframes rainbow {
  to { background: yellowgreen }
}

甚至,可以把多个状态写在一行。


@keyframes pound {
  from,to { transform: none; }
  50% { transform: scale(1.2); }
}

另外一点需要注意的是,浏览器从一个状态向另一个状态过渡,是平滑过渡。steps函数可以实现分步过渡。


div:hover {
  animation: 1s rainbow infinite steps(10);
}

这里有一个非常神奇的例子,可以看到steps函数的用处。

animation-play-state

有时,动画播放过程中,会突然停止。这时,默认行为是跳回到动画的开始状态。

上面动画中,如果鼠标移走,色块立刻回到动画开始状态。

如果想让动画保持突然终止时的状态,就要使用animation-play-state属性。


div {
    animation: spin 1s linear infinite;
    animation-play-state: paused;
}

div:hover {
  animation-play-state: running;
}

上面的代码指定,没有鼠标没有悬停时,动画状态是暂停;一旦悬停,动画状态改为继续播放。效果如下。

目前,IE 10和Firefox(>= 16)支持没有前缀的animation,而chrome不支持,所以必须使用webkit前缀。

也就是说,实际运用中,代码必须写成下面的样子。


div:hover {
  -webkit-animation: 1s rainbow;
  animation: 1s rainbow;  
}

@-webkit-keyframes rainbow {
  0% { background: #c00; }
  50% { background: orange; }
  100% { background: yellowgreen; }
}

@keyframes rainbow {
  0% { background: #c00; }
  50% { background: orange; }
  100% { background: yellowgreen; }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值