CSS3 animation

本文通过一个简单的示例介绍CSS3动画的基本用法,包括动画的关键帧定义、动画属性如animation-name、animation-duration等的详细解释。

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

css3 animate的小示例

先看一段代码~

<!DOCTYPE>
<html>
<head>
    <title>CSS3</title>
</head>
<style type="text/css">
.div{
    position: relative;
    width: 150px;
    height: 150px;
    border: 1px solid #354789;
    overflow:hidden;
}
.ball{
    position: absolute;
    bottom: 20px;
    width: 20px;
    height: 20px;
    border-radius: 10px;
    background: #444;
}
.ball:hover{
    animation: move 1.5s linear infinite alternate;
    -moz-animation:move 1.5s linear infinite alternate;
    -o-animation:move 1.5s linear infinite alternate;
    -webkit-animation: move 1.5s linear infinite alternate;
}
@keyframes move{
    0% {background: red;top:150px;opacity: 1.0}
    25% {background: yellow;top:100px;opacity: 0.8}
    50% {background: blue;top:50px;opacity: 0.6}
    75% {background: green;top:0px;opacity: 0.4}
    100% {background: red;top:150px;opacity: 1.0}
}
@-moz-keyframes move{
    0% {background: red;top:150px;opacity: 1.0}
    25% {background: yellow;top:100px;opacity: 0.8}
    50% {background: blue;top:50px;opacity: 0.6}
    75% {background: green;top:0px;opacity: 0.4}
    100% {background: red;top:150px;opacity: 1.0}
}
@-webkit-keyframes move{
    0% {background: red;top:150px;opacity: 1.0}
    25% {background: yellow;top:100px;opacity: 0.8}
    50% {background: blue;top:50px;opacity: 0.6}
    75% {background: green;top:0px;opacity: 0.4}
    100% {background: red;top:150px;opacity: 1.0}
}
@-o-keyframes move{
    0% {background: red;top:150px;opacity: 1.0}
    25% {background: yellow;top:100px;opacity: 0.8}
    50% {background: blue;top:50px;opacity: 0.6}
    75% {background: green;top:0px;opacity: 0.4}
    100% {background: red;top:150px;opacity: 1.0}
}
</style>
<body>
<div class="div">
    <div class="ball">

    </div>
</div>
</body>
</html>

运行结果就是一个小圆匀速向上运动背景色和透明度都在变化
如下图
初始状态
中间状态
中间状态
中间状态
中间状态

现在我们来详细介绍每个属性的意思

###animation 属性是一个简写属性,用于设置六个动画属性:

animation-name //动画名称,规定需要绑定到选择器的 keyframe 名称。
animation-duration //请始终规定 animation-duration 属性,否则时长为 0,就不会播放动画了。
animation-timing-function //规定动画的速度曲线。

  • linear 动画从头到尾的速度是相同的。
  • ease 默认。动画以低速开始,然后加快,在结束前变慢。
  • ease-in 动画以低速开始。
  • ease-out 动画以低速结束。
  • ease-in-out 动画以低速开始和结束。
  • cubic-bezier(n,n,n,n) 在 cubic-bezier 函数中自己的值。可能的值是从 0 到 1 的数值。(这个我也没有尝试过:))

animation-delay //定义动画何时开始,值以秒或毫秒计。
animation-iteration-count//定义动画的播放次数。

  • n 定义动画播放次数的数值。
  • infinite 规定动画应该无限次播放。

animation-direction

  • normal 默认值。动画应该正常播放。
  • alternate 动画应该轮流反向播放。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值