html animation 属性,CSS3之animation属性

CSS中的animation属性可用于为许多其他CSS属性设置动画,例如颜色,背景色,高度或宽度。 每个动画都需要使用@keyframes这种at-rule语句定义,然后使用animation属性来调用它,如下所示:

.element {

animation: pulse 5s infinite;

}

@keyframes pulse {

0% {

background-color: #001F3F;

}

100% {

background-color: #FF4136;

}

}

6c566e6762433c37bb105c00cac07659.png

.element {

width: 100%;

height: 100%;

animation: pulse 5s infinite;

}

@keyframes pulse {

0% {

background-color: #001F3F;

}

100% {

background-color: #FF4136;

}

}

html,

body {

height: 100%;

}

每个@keyframes的 at-rule CSS语句规则都定义了在动画过程中的特定时刻应该发生的情况。 例如,0%是动画的开始,而100%是动画的结束。可以通过简写animation属性或它的八个子属性控制这些关键帧,以更好地控制应该如何操纵这些关键帧。

子属性

animation-name:声明要操纵的@keyframes规则的名称。

animation-duration:动画完成一个周期所需的时间。

animation-timing-function:建立预设的加速曲线,例如缓动或线性。

animation-delay:加载元素到动画序列开始之间的时间。

animation-direction:设置循环后动画的方向。 其默认值在每个周期重置。

animation-iteration-count:应该执行动画的次数。

animation-fill-mode:设置在动画之前/之后应用的值。

例如,您可以将动画的最后状态设置为保留在屏幕上,或者可以将其设置为切换回动画开始之前的状态。

animation-play-state:暂停/播放动画。

可以如下面所示使用这些子属性:

@keyframes stretch {

/* 这里声明动画动作 */

}

.element {

animation-name: stretch;

animation-duration: 1.5s;

animation-timing-function: ease-out;

animation-delay: 0s;

animation-direction: alternate;

animation-iteration-count: infinite;

animation-fill-mode: none;

animation-play-state: running;

}

/* 等同于*/

.element {

animation:

stretch

1.5s

ease-out

0s

alternate

infinite

none

running;

}

d9dde9761269c70df5a1ac8447b4622b.png

.element {

height: 250px;

width: 250px;

margin: 0 auto;

background-color: red;

animation-name: stretch;

animation-duration: 1.5s;

animation-timing-function: ease-out;

animation-delay: 0;

animation-direction: alternate;

animation-iteration-count: infinite;

animation-fill-mode: none;

animation-play-state: running;

}

@keyframes stretch {

0% {

transform: scale(.3);

background-color: red;

border-radius: 100%;

}

50% {

background-color: orange;

}

100% {

transform: scale(1.5);

background-color: yellow;

}

}

body,

html {

height: 100%;

}

body {

display: flex;

align-items: center;

justify-content: center;

}

以下是这些子属性中每个属性可以采用的值的完整列表:

animation-timing-function

ease, ease-out, ease-in, ease-in-out, linear, cubic-bezier(x1, y1, x2, y2) (e.g. cubic-bezier(0.5, 0.2, 0.3, 1.0))

animation-duration

Xs or Xms

animation-delay

Xs or Xms

animation-iteration-count

X

animation-fill-mode

forwards, backwards, both, none

animation-direction

normal, alternate

animation-play-state

paused, running, running

多个步骤

如果动画具有相同的开始和结束属性,则在@keyframes中用逗号分隔0%和100%值很有用:

@keyframes pulse {

0%, 100% {

background-color: yellow;

}

50% {

background-color: red;

}

}

多个动画

您也可以用逗号分隔值,以在选择器上声明多个动画。 在下面的示例中,我们想在@keyframe中更改圆的颜色,同时还要将其与另一边左右轻轻一碰。

.element {

animation:

pulse 3s ease infinite alternate,

nudge 5s linear infinite alternate;

}

2f8c5fedce41009808de79410eb48b12.png

.element {

height: 400px;

width: 400px;

background-color: red;

animation:

pulse 3s ease infinite alternate,

nudge 5s linear infinite alternate;

border-radius: 100%;

}

@keyframes pulse {

0%,

100% {

background-color: red;

}

50% {

background-color: orange;

}

}

@keyframes nudge {

0%,

100% {

transform: translate(0, 0);

}

50% {

transform: translate(150px, 0);

}

80% {

transform: translate(-150px, 0);

}

}

html,

body {

height: 100%;

}

body {

display: flex;

align-items: center;

justify-content: center;

}

性能

对大多数属性进行动画处理是性能方面的考虑,因此在对任何属性进行动画处理之前,我们应谨慎行事。 但是,可以安全地对某些组合进行动画处理:

transform: translate()

transform: scale()

transform: rotate()

opacity

哪些属性可以设置动画?

MDN有一个可以设置动画的CSS属性列表。 可设置动画的属性倾向于颜色和数字。 不可动画属性的一个示例是背景图像。

标签:CSS3,动画,color,100%,keyframes,animation,background,属性

来源: https://www.cnblogs.com/f6056/p/11635785.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CSS3 animation属性是用来定义一个元素的动画效果。它包括了多个子属性,如animation-name、animation-duration、animation-timing-function、animation-delay、animation-iteration-count、animation-direction、animation-fill-mode和animation-play-state。 其中,animation-name用来定义动画的名称,animation-duration用来定义动画的持续时间,animation-timing-function用来定义动画的时间函数,animation-delay用来定义动画的延迟时间,animation-iteration-count用来定义动画的播放次数,animation-direction用来定义动画的播放方向,animation-fill-mode用来定义动画结束时元素的样式,animation-play-state用来定义动画的播放状态。 更具体地说,animation-name用来绑定动画规则到选择器上,而animation-duration用来定义动画播放一次的时间长度。animation-timing-function则用来定义动画的时间函数,控制动画的速度曲线。animation-delay用来定义动画开始播放的延迟时间,而animation-iteration-count用来定义动画的重复次数。animation-direction则用来定义动画播放的方向,可以是正向、反向或循环播放。animation-fill-mode则用来定义动画在播放前和播放后所应用的样式,可以是保持初始状态或保持最后状态。最后,animation-play-state用来定义动画的播放状态,可以是暂停或播放。 综上所述,CSS3 animation属性是用来定义元素的动画效果的,它包括了多个子属性,每个属性都有自己的作用,用来控制动画的各个方面,如持续时间、播放次数、延迟时间等。通过合理使用这些属性,可以创建出各种丰富多样的动画效果。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [css3动画属性animation](https://blog.csdn.net/qq_40340943/article/details/100733638)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [css动画-animation各个属性详解](https://blog.csdn.net/weixin_45810135/article/details/108921359)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值