JQuery与CSS3合体
这是一个效果与兼容性俱佳的方式(特别对于IE9暂不支持CSS3而言)
HTML代码(可以看到与CSS3中的HTML代码并无不同):
广州网站建设,网站建设,广州网页设计,广州网站设计
- <html>
- <head>
- <meta charset="UTF-8" />
- <title>Animations in HTML5 using CSS3 animations</title>
- </head>
- <body>
- < id="container">
- < id="car">
- < id="chassis"></>
- <div id="backtire" class="tire">
- <div class="hr"></div>
- <div class="vr"></div>
- </div>
- <div id="fronttire" class="tire">
- <div class="hr"></div>
- <div class="vr"></div>
- </div>
- </div>
- <div id="grass"></div>
- </div>
- <footer></footer>
- </body>
- </html>
CSS:
- <style>
- body
- {
- padding:0;
- margin:0;
- }
- #container
- {
- position:relative;
- width:100%;
- height:600px;
- overflow:hidden; /*这个很重要*/
- }
- #car
- {
- position:absolute; /*汽车在容器中采用绝对定位*/
- width:400px;
- height:210px; /*汽车的总高度,包括轮胎和底盘*/
- z-index:1; /*让汽车在背景的上方*/
- top:300px; /*距顶端的距离(y轴)*/
- left:50px; /*距左侧的距离(x轴)*/
- }
- /*车身*/
- #chassis
- {
- position:absolute;
- width:400px;
- height:130px;
- background:#FF9900;
- border: 2px solid #FF6600;
- }
- /*轮胎*/
- .tire
- {
- z-index:1; /*同上,轮胎也应置于背景的上方*/
- position:absolute;
- bottom:0;
- border-radius:60px; /*圆半径*/
- height:120px; /* 2*radius=height */
- width:120px; /* 2*radius=width */
- background:#0099FF; /*填充色*/
- border:1px solid #3300FF;
- -o-transform:rotate(0deg); /*旋转(单位:度)*/
- -ms-transform:rotate(0deg);
- -webkit-transform:rotate(0deg);
- -moz-transform:rotate(0deg);
- }
- #fronttire
- {
- right:20px; /*设置右边的轮胎距离边缘的距离为20*/
- }
- #backtire
- {
- left:20px; /*设置左边的轮胎距离边缘的距离为20*/
- }
- #grass
- {
- position:absolute; /*背景绝对定位在容器中*/
- width:100%;
- height:130px;
- bottom:0;
- /*让背景色线性渐变,bottom,表示渐变的起始处,第一个颜色值是渐变的起始值,第二个颜色值是终止值 */
- background:linear-grdaient(bottom,#33CC00,#66FF22);
- background:-webkit-linear-gradient(bottom,#33CC00,#66FF22);
- background:-moz-linear-gradient(bottom,#33CC00,#66FF22);
- background:-ms-linear-gradient(bottom,#33CC00,#66FF22);
- }
- .hr,.vr
- {
- position:absolute;
- background:#3300FF;
- }
- .hr
- {
- height:1px;
- width:100%; /*水平线*/
- left:0;
- top:60px;
- }
- .vr
- {
- width:1px;
- height:100%; /*垂直线*/
- left:60px;
- top:0;
- }
- </style>
JS代码:
首先引入在线API:
广州网站建设,网站建设,广州网页设计,广州网站设计
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
实现动画代码(相当简洁):
- <script>
- $(function(){
- var rot=0;
- var prefix=$('.tire').css('-o-transform')?'-o-transform':($('.tire').css('-ms-transform')?'-ms-transform':($('.tire').css('-moz-transform')?'-moz-transform':($('.tire').css('-webkit-transform')?'-webkit-transform':'transform')));
- var origin={ /*设置我们的起始点*/
- left:-400
- };
- var animation={ /*该动画由jQuery执行*/
- left:1600 /*设置我们将移动到的最终位置*/
- };
- var rotate=function(){ /*该方法将被旋转的轮子调用*/
- rot+=2;
- $('.tire').css(prefix,'rotate('+rot+'deg)');
- };
- var options={ /*将要被jQuery使用的参数*/
- easing:'linear', /*指定速度,此处只是线性,即为匀速*/
- duration:10000, /*指定动画持续时间*/
- complete:function(){
- $('#car').css(origin).animate(animation,options);
- },
- step:rotate
- };
- options.complete();
- });
- </script>
简单讲解:prefix首先识别出当前是哪个定义被采用了(-o?-moz?-webkit?-ms?),然后定义了动画的起点位置和终点位置。接着,定义了设置旋转角度的函数(该函数将在在动画的每一步(step)中执行)。然后,定义了一个动画,该定义方式导致了无限自循环调用!
本文,通过一个简单的动画实例,演示了HTML5下,实现动画的几种常见方式。