1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > php实现弹跳 css如何实现弹跳球动画效果?现弹跳球动画的实现示例

php实现弹跳 css如何实现弹跳球动画效果?现弹跳球动画的实现示例

时间:2022-08-27 04:32:34

相关推荐

php实现弹跳 css如何实现弹跳球动画效果?现弹跳球动画的实现示例

css如何实现弹跳球动画效果?本篇文章给大家通过代码示例介绍css是如何实现弹跳球动画效果的。有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所帮助。

1、定义动画关键帧

对于这个动画,我们将使用两个关键帧 - 一个用恒定速度水平平移球,另一个用于应用大致抛物线垂直弹跳运动。可以将水平和垂直平移组合成一个关键帧,但这对我们所追求的效果不起作用。

使用以下关键帧可以轻松实现水平运动:@-webkit-keyframes travel {

from { }

to { left: 640px; }

}

@keyframes travel {

from { }

to { left: 640px; }

}

稍后将使用指定的名称“travel”引用此关键帧,并使用linear(转换计时函数)来应用该关键帧,该函数随每次迭代更改方向。

对于垂直弹跳,动画,我们要利用的易用性在和渐出定时功能来模拟重力场的影响:@-webkit-keyframes bounce {

from, to {

bottom: 0;

-webkit-animation-timing-function: ease-out;

}

50% {

bottom: 220px;

-webkit-animation-timing-function: ease-in;

}

}

@keyframes bounce {

from, to {

botttom: 0;

animation-timing-function: ease-out;

}

50% {

bottom: 220px;

animation-timing-function: ease-in;

}

}

该关键帧已被命名为“bounce”以供将来参考。

组合这两个关键帧将使我们的'球'水平移动640像素,垂直移动220像素。当然,这些值需要调整以适应“舞台”的大小。

2、设置动画的舞台

与往常一样,我们首先设置一个“舞台”,在其中执行动画。在这种情况下,一个尺寸为660 x 240像素的简单DIV。让宽度为100%会很好,但是在不知道确切像素宽度的情况下放置一些元素是很困难的。#stage {

position: relative;

margin: 1em auto;

width: 660px;

height: 240px;

border: 2px solid #666;

background: #cff;

}

在这个阶段,我们将设置一个水平来回移动的DIV元素,并在其中表示上下反弹的“球”的DIV:#traveler {

position: absolute;

width: 20px;

height: 240px;

-webkit-animation-name: travel;

-webkit-animation-timing-function: linear;

-webkit-animation-iteration-count: infinite;

-webkit-animation-direction: alternate;

-webkit-animation-duration: 4.8s;

animation-name: travel;

animation-timing-function: linear;

animation-iteration-count: infinite;

animation-direction: alternate;

animation-duration: 4.8s;

}

#bouncer {

position: absolute;

width: 20px;

height: 20px;

background: red;

border-radius: 10px;

-webkit-animation-name: bounce;

-webkit-animation-iteration-count: infinite;

-webkit-animation-duration: 4.2s;

animation-name: bounce;

animation-iteration-count: infinite;

animation-duration: 4.2s;

}

所以'球'的尺寸为20 x 20像素,圆角。

3、设置球运动

我们完成了一些简单的HTML标记:

如果您的浏览器支持它,动画将自动启动并在下面的框(或#stage)中无限期地继续:

我们添加了一个额外的元素和一些样式来突出动画的x和y分量,不需要JavaScript,其他代码完全如所示。

CSS:bounce-animation.css(https://www.the-art-of-/bounce-animation.css)

大功告成!

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。