如何利用CSS3动画实现弹跳效果

html-css09

如何利用CSS3动画实现弹跳效果,第1张

HTML代码

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>用CSS3动画给一个小球创建跳跃的动画效果</title>

<link rel="stylesheet" type="text/css" href="index.css" />

</head>

<body>

<section class="main">

<div id="ballWrapper">

<div id="ball"></div>

<div id="ballShadow"></div>

</div>

</section>

</body>

</html>

CSS代码(创建一个与html同目录的index.css文件)

#ballWrapper{position:fixedtop:35%left:50%z-index:100margin-left:-70pxwidth:140pxheight:300pxcursor:pointer-webkit-transition:all 5s linear 0s-moz-transition:all 5s linear 0stransition:all 5s linear 0s-webkit-transform:scale(1)-moz-transform:scale(1)-o-transform:scale(1)transform:scale(1)-ms-transform:scale(1)}

#ballWrapper:active{cursor:pointer-webkit-transform:scale(0)-moz-transform:scale(0)-o-transform:scale(0)transform:scale(0)-ms-transform:scale(0)}

#ball{position:absolutetop:0z-index:11width:140pxheight:140pxborder-radius:70pxbackground:#bbbbackground:url(data:image/svg+xml background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(187,187,187,1)), color-stop(99%,rgba(119,119,119,1))) background: -webkit-linear-gradient(top, rgba(187,187,187,1) 0%,rgba(119,119,119,1) 99%) background: -o-linear-gradient(top, rgba(187,187,187,1) 0%,rgba(119,119,119,1) 99%) background: -ms-linear-gradient(top, rgba(187,187,187,1) 0%,rgba(119,119,119,1) 99%) background: linear-gradient(top, rgba(187,187,187,1) 0%,rgba(119,119,119,1) 99%) box-shadow: inset 0 -5px 15px rgba(255,255,255,0.4), inset -2px -1px 40px rgba(0,0,0,0.4), 0 0 1px #000 cursor: pointer base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2JiYmJiYiIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9Ijk5JSIgc3RvcC1jb2xvcj0iIzc3Nzc3NyIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgPC9saW5lYXJHcmFkaWVudD4KICA8cmVjdCB4PSIwIiB5PSIwIiB3aWR0aD0iMSIgaGVpZ2h0PSIxIiBmaWxsPSJ1cmwoI2dyYWQtdWNnZy1nZW5lcmF0ZWQpIiAvPgo8L3N2Zz4=)background:-moz-linear-gradient(top,rgba(187,187,187,1) 0,rgba(119,119,119,1) 99%)filter:progid:DXImageTransform.Microsoft.gradient( startColorstr='#bbbbbb', endColorstr='#777777', GradientType=0 )-webkit-animation:jump 1s infinite-moz-animation:jump 1s infinite-o-animation:jump 1s infinite-ms-animation:jump 1s infiniteanimation:jump 1s infinite}

#ball::after{position:absolutetop:10pxleft:30pxz-index:10width:80pxheight:40pxborder-radius:40px/20pxbackground:url(data:image/svg+xml background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(232,232,232,1)), color-stop(1%,rgba(232,232,232,1)), color-stop(100%,rgba(255,255,255,0))) background: -webkit-linear-gradient(top, rgba(232,232,232,1) 0%,rgba(232,232,232,1) 1%,rgba(255,255,255,0) 100%) background: -o-linear-gradient(top, rgba(232,232,232,1) 0%,rgba(232,232,232,1) 1%,rgba(255,255,255,0) 100%) background: -ms-linear-gradient(top, rgba(232,232,232,1) 0%,rgba(232,232,232,1) 1%,rgba(255,255,255,0) 100%) background: linear-gradient(top, rgba(232,232,232,1) 0%,rgba(232,232,232,1) 1%,rgba(255,255,255,0) 100%) content: "" base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2U4ZThlOCIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjElIiBzdG9wLWNvbG9yPSIjZThlOGU4IiBzdG9wLW9wYWNpdHk9IjEiLz4KICAgIDxzdG9wIG9mZnNldD0iMTAwJSIgc3RvcC1jb2xvcj0iI2ZmZmZmZiIgc3RvcC1vcGFjaXR5PSIwIi8+CiAgPC9saW5lYXJHcmFkaWVudD4KICA8cmVjdCB4PSIwIiB5PSIwIiB3aWR0aD0iMSIgaGVpZ2h0PSIxIiBmaWxsPSJ1cmwoI2dyYWQtdWNnZy1nZW5lcmF0ZWQpIiAvPgo8L3N2Zz4=)background:-moz-linear-gradient(top,rgba(232,232,232,1) 0,rgba(232,232,232,1) 1%,rgba(255,255,255,0) 100%)filter:progid:DXImageTransform.Microsoft.gradient( startColorstr='#e8e8e8', endColorstr='#00ffffff', GradientType=0 )}

#ballShadow{position:absolutebottom:0left:50%z-index:10margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-webkit-transform:scaleY(.3)-moz-transform:scaleY(.3)-o-transform:scaleY(.3)transform:scaleY(.3)-ms-transform:scaleY(.3)-webkit-animation:shrink 1s infinite-moz-animation:shrink 1s infinite-o-animation:shrink 1s infinite-ms-animation:shrink 1s infiniteanimation:shrink 1s infinite}

@-webkit-keyframes jump{0%{top:0-webkit-animation-timing-function:ease-in}

50%{top:140pxheight:140px-webkit-animation-timing-function:ease-out}

55%{top:160pxheight:120pxborder-radius:70px/60px-webkit-animation-timing-function:ease-in}

65%{top:120pxheight:140pxborder-radius:70px-webkit-animation-timing-function:ease-out}

95%{top:0-webkit-animation-timing-function:ease-in}

100%{top:0-webkit-animation-timing-function:ease-in}

}

@-moz-keyframes jump{0%{top:0-moz-animation-timing-function:ease-in}

50%{top:140pxheight:140px-moz-animation-timing-function:ease-out}

55%{top:160pxheight:120pxborder-radius:70px/60px-moz-animation-timing-function:ease-in}

65%{top:120pxheight:140pxborder-radius:70px-moz-animation-timing-function:ease-out}

95%{top:0-moz-animation-timing-function:ease-in}

100%{top:0-moz-animation-timing-function:ease-in}

}

@-o-keyframes jump{0%{top:0-o-animation-timing-function:ease-in}

50%{top:140pxheight:140px-o-animation-timing-function:ease-out}

55%{top:160pxheight:120pxborder-radius:70px/60px-o-animation-timing-function:ease-in}

65%{top:120pxheight:140pxborder-radius:70px-o-animation-timing-function:ease-out}

95%{top:0-o-animation-timing-function:ease-in}

100%{top:0-o-animation-timing-function:ease-in}

}

@-ms-keyframes jump{0%{top:0-ms-animation-timing-function:ease-in}

50%{top:140pxheight:140px-ms-animation-timing-function:ease-out}

55%{top:160pxheight:120pxborder-radius:70px/60px-ms-animation-timing-function:ease-in}

65%{top:120pxheight:140pxborder-radius:70px-ms-animation-timing-function:ease-out}

95%{top:0-ms-animation-timing-function:ease-in}

100%{top:0-ms-animation-timing-function:ease-in}

}

@keyframes jump{0%{top:0animation-timing-function:ease-in}

50%{top:140pxheight:140pxanimation-timing-function:ease-out}

55%{top:160pxheight:120pxborder-radius:70px/60pxanimation-timing-function:ease-in}

65%{top:120pxheight:140pxborder-radius:70pxanimation-timing-function:ease-out}

95%{top:0animation-timing-function:ease-in}

100%{top:0animation-timing-function:ease-in}

}

@-webkit-keyframes shrink{0%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-webkit-animation-timing-function:ease-in}

50%{bottom:30pxmargin-left:-10pxwidth:20pxheight:5pxborder-radius:20pxbackground:rgba(20,20,20,.3)box-shadow:0 0 20px 35px rgba(20,20,20,.3)-webkit-animation-timing-function:ease-out}

100%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-webkit-animation-timing-function:ease-in}

}

@-moz-keyframes shrink{0%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-moz-animation-timing-function:ease-in}

50%{bottom:30pxmargin-left:-10pxwidth:20pxheight:5pxborder-radius:20pxbackground:rgba(20,20,20,.3)box-shadow:0 0 20px 35px rgba(20,20,20,.3)-moz-animation-timing-function:ease-out}

100%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-moz-animation-timing-function:ease-in}

}

@-o-keyframes shrink{0%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-o-animation-timing-function:ease-in}

50%{bottom:30pxmargin-left:-10pxwidth:20pxheight:5pxborder-radius:20pxbackground:rgba(20,20,20,.3)box-shadow:0 0 20px 35px rgba(20,20,20,.3)-o-animation-timing-function:ease-out}

100%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-o-animation-timing-function:ease-in}

}

@-ms-keyframes shrink{0%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-ms-animation-timing-function:ease-in}

50%{bottom:30pxmargin-left:-10pxwidth:20pxheight:5pxborder-radius:20pxbackground:rgba(20,20,20,.3)box-shadow:0 0 20px 35px rgba(20,20,20,.3)-ms-animation-timing-function:ease-out}

100%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-ms-animation-timing-function:ease-in}

}

@keyframes shrink{0%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)animation-timing-function:ease-in}

50%{bottom:30pxmargin-left:-10pxwidth:20pxheight:5pxborder-radius:20pxbackground:rgba(20,20,20,.3)box-shadow:0 0 20px 35px rgba(20,20,20,.3)animation-timing-function:ease-out}

100%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)animation-timing-function:ease-in}

}

用CSS可以实现

你只要改变RevealTrans滤镜的“Transition"的值,就能获得不同的网页切换效果,是不是太方便了点?但很遗憾,要把RevealTrans滤镜用于网页中的某个对象就没有这么简单了,它必须借助于Javascript来调用其方法,才能实现,这就是说要动手编程序了。但也不是太难,下面我将通过一个制作动态字幕变换的例子来说明其使用方法。

渐淡字幕变换效果

这个例子演示了利用Javascript程序控制Revealtrans滤镜来实现字幕逐渐淡出和渐进的效果,请看下面的效果图:

<!--

.mytrans { filter:revealTrans(Transition=12,Duration=2)}

-- >

/style >

2、插入一个层,我们把层的“Layer ID”改为“div1”(可直接在层的属性面板上加入即可),并设置好层的背景和调整好层的大小,并把Revealtrans滤镜加载到到层上,这时你看到的层的标记代码是这样的: div id="div1" style="position:absolutewidth:680pxheight:30pxz-index:37background: #FFFFCClayer-background-color: #FFFFCCborder: 1px none #000000" class="mytrans" >/div >

3、在网页源代码的 head >与 /head >之间插入下面这段Javascript程序:

script language="JavaScript" >

<!--

function HelpArray(len)

{

this.length=len

}

// 建立一个数组,存放转换的内容。

HelpText=new HelpArray(5)

HelpText[0]="在一个文档使用动态转换的滤镜(Revealtrans)其实是很容易的。"

HelpText[1]="首先,为对象的样式表单建立一个需要转换的“Revaltrans"滤镜,"

HelpText[2]="然后,使用“apply()"方法防止错误,"

HelpText[3]="现在,你可以改变任何你想改变的东西,"

HelpText[4]="最后,“play()"方法开始进行转换。"

ScriptText=new HelpArray(5)

var i= -1

// 显示转换效果

function playHelp()

{

if (i==4)

{ i=0 }

else

{ i++}

div1.filters[0].apply()

div1.innerText=HelpText[i]

div1.filters[0].play()

// 设置每段字幕演示的时间,以毫秒计。这里的时间要长于滤镜中的时间,以保证在转换结束后能停留一段时间。

// 以方便看清楚字幕内容。在本例中字幕演示的时间是6秒,滤镜中设置的转换时间是2秒。

mytimeout=setTimeout("playHelp()",6000)

}

-- >

/script >

4、在网页的源代码的 body >中加入这样一句代码:onload="playHelp()"。

到此,可以按F12看效果了,好象也不是太难。你若是想看看其它的转换效果,只要改变一下Revealtrans滤镜中的Transition参数值就行了,其它什么也不用改动,你瞧多方便!用它来作广告条我想效果不会差。比起动画来可瘦小多了。

当然Revealtrans滤镜同样也可以用于图片,效果也不错,但要改动一下Javascript程序。由于CSS还有一个动态滤镜是BlendTrans滤镜,用它做图片的淡入淡出效果相当不错,

可以的

左移动30像素

-webkit-transform: translateX(-30px)

-moz-transform: translateX(-30px)

-o-transform: translateX(-30px)

-ms-transform: translateX(-30px)

transform: translateX(-30px)

             

             顺时针旋转360°

       

-webkit-transform: rotateZ(360deg)

-moz-transform: rotateZ(360deg)

-o-transform: rotateZ(360deg)

-ms-transform: rotateZ(360deg)

transform: rotateZ(360deg)

跳跃的忘记了