html+css3太阳系行星运转动画效果的实现代码

所属分类: 网页制作 / HTML/Xhtml 阅读数: 1817
收藏 0 赞 0 分享

做一个太阳系八大行星的运转动画,不包括行星的卫星,所有行星围绕太阳公转,行星采用纯色,暂时没有自转。

效果静态图:

 

动画中包括:太阳及各行星,运行轨道,行星公转动画。

先画好草图,设计好大小和位置,根据公转周期计算好动画执行的时间。

html的结构:

一个class为solarsys的div,作为太阳系容器元素,该div的position为relative。

行星轨道和行星都用div,position为absolute。

容器用relative和内部元素采用absolute的定位方式,比较简单的能实现效果,缺点就是大小是固定的。

XML/HTML Code复制内容到剪贴板
  1. <div class="solarsys">  
  2.         <!--太阳-->  
  3.         <div class='sun'></div>  
  4.   
  5.         <!--水星轨道-->  
  6.         <div class='mercuryOrbit'></div>  
  7.   
  8.         <!--水星-->  
  9.         <div class='mercury'></div>  
  10.   
  11.         <!--金星轨道-->  
  12.         <div class='venusOrbit'></div>  
  13.   
  14.         <!--金星-->  
  15.         <div class='venus'></div>  
  16.   
  17.         <!--地球轨道-->  
  18.         <div class='earthOrbit'></div>  
  19.   
  20.         <!--地球-->  
  21.         <div class='earth'></div>  
  22.   
  23.         <!--火星轨道-->  
  24.         <div class='marsOrbit'></div>  
  25.   
  26.         <!--火星-->  
  27.         <div class='mars'></div>  
  28.   
  29.         <!--木星轨道-->  
  30.         <div class='jupiterOrbit'></div>  
  31.   
  32.         <!--木星-->  
  33.         <div class='jupiter'></div>  
  34.   
  35.         <!--土星轨道-->  
  36.         <div class='saturnOrbit'></div>  
  37.   
  38.         <!--土星-->  
  39.         <div class='saturn'></div>  
  40.   
  41.         <!--天王星轨道-->  
  42.         <div class='uranusOrbit'></div>  
  43.   
  44.         <!--天王星-->  
  45.         <div class='uranus'></div>  
  46.   
  47.         <!--海王星轨道-->  
  48.         <div class='neptuneOrbit'></div>  
  49.   
  50.         <!--海王星-->  
  51.         <div class='neptune'></div>  
  52.     </div>  

太阳系容器div的css:

定宽,定高,relative定位,页面内剧中对齐。

CSS Code复制内容到剪贴板
  1. .solarsys{   
  2.             width800px;   
  3.             height800px;;   
  4.             positionrelative;   
  5.             margin: 0 auto;   
  6.             background-color#000000;   
  7.             padding: 0;   
  8.             transform: scale(1);   
  9.         }  

太阳div的css:

按照设计的大小和位置,设定宽高,left,top。

设定颜色。

通过把boder-radius生成50%,把一个正方形变成圆形。

通过box-shadow的4层颜色设置实现太阳光晕。

CSS Code复制内容到剪贴板
  1. .sun {   
  2.             left:357px;   
  3.             top:357px;   
  4.             height90px;   
  5.             width90px;   
  6.             background-colorrgb(248,107,35);   
  7.             border-radius: 50%;   
  8.             box-shadow: 5px 5px 10px rgb(248,107,35), -5px -5px 10px rgb(248,107,35), 5px -5px 10px rgb(248,107,35), -5px 5px 10px rgb(248,107,35);   
  9.             positionabsolute;   
  10.             margin: 0;   
  11.         }  

行星轨道div的css:

假设是水星轨道。

按照设计的大小和位置,设定宽高,left,top。

背景色透明。

通过把boder-radius生成50%,把一个正方形变成圆形。

boder的类型设成虚线。

boder的颜色设成灰色。

宽度设1。

CSS Code复制内容到剪贴板
  1. .mercuryOrbit {   
  2.             left:342.5px;   
  3.             top:342.5px;   
  4.             height115px;   
  5.             width115px;   
  6.             background-colortransparent;   
  7.             border-radius: 50%;   
  8.             border-styledashed;   
  9.             border-colorgray;   
  10.             positionabsolute;   
  11.             border-width1px;   
  12.             margin0px;   
  13.             padding0px;   
  14.         }  

行星div的css:

假设是水星。

按照设计的大小和位置,设定宽高,left,top。

设置颜色。

通过把boder-radius生成50%,把一个正方形变成圆形。

将transfrom-origin设定成当前div的左上角相对于整个太阳系容器的中心点的横向和纵向的偏移量。加上旋转动画后就是围绕着中心点旋转效果。 

做一个animation,引用rotate关键帧动画,线性永久执行,这里的执行时长是根据行星的公转周期计算出来。

CSS Code复制内容到剪贴板
  1. .mercury {   
  2.             left:337.5px;   
  3.             top:395px;   
  4.             height10px;   
  5.             width10px;   
  6.             background-colorrgb(166,138,56);   
  7.             border-radius: 50%;   
  8.             positionabsolute;   
  9.             transform-origin: 62.5px 5px;   
  10.             animation: rotate 1.5s infinite linear;   
  11.         }  

rotate关键帧动画:

逆时针旋转。

CSS Code复制内容到剪贴板
  1. @keyframes rotate {   
  2.             100% {   
  3.                 transform: rotate(-360deg);   
  4.             }   
  5.         }  

基本结构完成。

仅在chrome中测试过。

 

全部代码:

XML/HTML Code复制内容到剪贴板
  1. <html>  
  2. <head>  
  3.     <style>  
  4.         .solarsys{   
  5.             width: 800px;   
  6.             height: 800px;;   
  7.             position: relative;   
  8.             margin: 0 auto;   
  9.             background-color: #000000;   
  10.             padding: 0;   
  11.             transform: scale(1);   
  12.         }   
  13.   
  14.         /*太阳*/   
  15.         .sun {   
  16.             left:357px;   
  17.             top:357px;   
  18.             height: 90px;   
  19.             width: 90px;   
  20.             background-color: rgb(248,107,35);   
  21.             border-radius: 50%;   
  22.             box-shadow: 5px 5px 10px rgb(248,107,35), -5px -5px 10px rgb(248,107,35), 5px -5px 10px rgb(248,107,35), -5px 5px 10px rgb(248,107,35);   
  23.             position: absolute;   
  24.             margin: 0;   
  25.         }   
  26.   
  27.         /*水星*/   
  28.         .mercury {   
  29.             left:337.5px;   
  30.             top:395px;   
  31.             height: 10px;   
  32.             width: 10px;   
  33.             background-color: rgb(166,138,56);   
  34.             border-radius: 50%;   
  35.             position: absolute;   
  36.             transform-origin: 62.5px 5px;   
  37.             animation: rotate 1.5s infinite linear;   
  38.         }   
  39.   
  40.         /*水星轨道*/   
  41.         .mercuryOrbit {   
  42.             left:342.5px;   
  43.             top:342.5px;   
  44.             height: 115px;   
  45.             width: 115px;   
  46.             background-color: transparent;   
  47.             border-radius: 50%;   
  48.             border-style: dashed;   
  49.             border-color: gray;   
  50.             position: absolute;   
  51.             border-width: 1px;   
  52.             margin: 0px;   
  53.             padding: 0px;   
  54.         }   
  55.   
  56.         /*金星*/   
  57.         .venus {   
  58.             left:309px;   
  59.             top:389px;   
  60.             height: 22px;   
  61.             width: 22px;   
  62.             background-color: rgb(246,157,97);   
  63.             border-radius: 50%;   
  64.             position: absolute;   
  65.             transform-origin: 91px 11px;   
  66.             animation: rotate 3.84s infinite linear;   
  67.         }   
  68.   
  69.         /*金星轨道*/   
  70.         .venusOrbit {   
  71.             left:320px;   
  72.             top:320px;   
  73.             height: 160px;   
  74.             width: 160px;   
  75.             background-color: transparent;   
  76.             border-radius: 50%;   
  77.             border-style: dashed;   
  78.             border-color: gray;   
  79.             position: absolute;   
  80.             border-width: 1px;   
  81.             /*margin: 100px;*/   
  82.             /*transform-origin: -75px -75px;*/   
  83.             /*animation: rotate 4s infinite linear;*/   
  84.             margin: 0px;   
  85.             padding: 0px;   
  86.         }   
  87.   
  88.         /*地球*/   
  89.         .earth {   
  90.             left:266.5px;   
  91.             top:391px;   
  92.             height: 18px;   
  93.             width: 18px;   
  94.             background-color: rgb(115,114,174);   
  95.             border-radius: 50%;   
  96.             position: absolute;   
  97.             transform-origin: 134px 9px;   
  98.             animation: rotate 6.25s infinite linear;   
  99.         }   
  100.   
  101.         /*地球轨道*/   
  102.         .earthOrbit {   
  103.             left:275px;   
  104.             top:275px;   
  105.             height: 250px;   
  106.             width: 250px;   
  107.             background-color: transparent;   
  108.             border-radius: 50%;   
  109.             border-style: dashed;   
  110.             border-color: gray;   
  111.             position: absolute;   
  112.             border-width: 1px;   
  113.             /*margin: 100px;*/   
  114.             /*transform-origin: -75px -75px;*/   
  115.             /*animation: rotate 4s infinite linear;*/   
  116.             margin: 0px;   
  117.             padding: 0px;   
  118.         }   
  119.   
  120.         /*火星*/   
  121.         .mars {   
  122.             left:222.5px;   
  123.             top:392.5px;   
  124.             height: 15px;   
  125.             width: 15px;   
  126.             background-color: rgb(140,119,63);   
  127.             border-radius: 50%;   
  128.             position: absolute;   
  129.             transform-origin: 177.5px 7.5px;   
  130.             animation: rotate 11.75s infinite linear;   
  131.         }   
  132.   
  133.          /*火星轨道*/   
  134.         .marsOrbit {   
  135.             left:230px;   
  136.             top:230px;   
  137.             height: 340px;   
  138.             width: 340px;   
  139.             background-color: transparent;   
  140.             border-radius: 50%;   
  141.             border-style: dashed;   
  142.             border-color: gray;   
  143.             position: absolute;   
  144.             border-width: 1px;   
  145.             /*margin: 100px;*/   
  146.             /*transform-origin: -75px -75px;*/   
  147.             /*animation: rotate 4s infinite linear;*/   
  148.             margin: 0px;   
  149.             padding: 0px;   
  150.         }   
  151.   
  152.         /*木星*/   
  153.         .jupiter {   
  154.             left:134px;   
  155.             top:379px;   
  156.             height: 42px;   
  157.             width: 42px;   
  158.             background-color: rgb(156,164,143);   
  159.             border-radius: 50%;   
  160.             position: absolute;   
  161.             transform-origin: 266px 21px;   
  162.             animation: rotate 74.04s infinite linear;   
  163.         }   
  164.   
  165.         /*木星轨道*/   
  166.         .jupiterOrbit {   
  167.             left:155px;   
  168.             top:155px;   
  169.             height: 490px;   
  170.             width: 490px;   
  171.             background-color: transparent;   
  172.             border-radius: 50%;   
  173.             border-style: dashed;   
  174.             border-color: gray;   
  175.             position: absolute;   
  176.             border-width: 1px;   
  177.             /*margin: 100px;*/   
  178.             /*transform-origin: -75px -75px;*/   
  179.             /*animation: rotate 4s infinite linear;*/   
  180.             margin: 0px;   
  181.             padding: 0px;   
  182.         }   
  183.   
  184.         /*土星*/   
  185.         .saturn {   
  186.             left:92px;   
  187.             top:387px;   
  188.             height: 26px;   
  189.             width: 26px;   
  190.             background-color: rgb(215,171,68);   
  191.             border-radius: 50%;   
  192.             position: absolute;   
  193.             transform-origin: 308px 13px;   
  194.             animation: rotate 183.92s infinite linear;   
  195.         }   
  196.   
  197.         /*土星轨道*/   
  198.         .saturnOrbit {   
  199.             left:105px;   
  200.             top:105px;   
  201.             height: 590px;   
  202.             width: 590px;   
  203.             background-color: transparent;   
  204.             border-radius: 50%;   
  205.             border-style: dashed;   
  206.             border-color: gray;   
  207.             position: absolute;   
  208.             border-width: 1px;   
  209.             /*margin: 100px;*/   
  210.             /*transform-origin: -75px -75px;*/   
  211.             /*animation: rotate 4s infinite linear;*/   
  212.             margin: 0px;   
  213.             padding: 0px;   
  214.         }   
  215.   
  216.         /*天王星*/   
  217.         .uranus {   
  218.             left:41.5px;   
  219.             top:386.5px;   
  220.             height: 27px;   
  221.             width: 27px;   
  222.             background-color: rgb(164,192,206);   
  223.             border-radius: 50%;   
  224.             position: absolute;   
  225.             transform-origin: 358.5px 13.5px;   
  226.             animation: rotate 524.46s infinite linear;   
  227.         }   
  228.   
  229.         /*天王星轨道*/   
  230.         .uranusOrbit {   
  231.             left:55px;   
  232.             top:55px;   
  233.             height: 690px;   
  234.             width: 690px;   
  235.             background-color: transparent;   
  236.             border-radius: 50%;   
  237.             border-style: dashed;   
  238.             border-color: gray;   
  239.             position: absolute;   
  240.             border-width: 1px;   
  241.             /*margin: 100px;*/   
  242.             /*transform-origin: -75px -75px;*/   
  243.             /*animation: rotate 4s infinite linear;*/   
  244.             margin: 0px;   
  245.             padding: 0px;   
  246.         }   
  247.   
  248.         /*海王星*/   
  249.         .neptune {   
  250.             left:10px;   
  251.             top:390px;   
  252.             height: 20px;   
  253.             width: 20px;   
  254.             background-color: rgb(133,136,180);   
  255.             border-radius: 50%;   
  256.             position: absolute;   
  257.             transform-origin: 390px 10px;   
  258.             animation: rotate 1028.76s infinite linear;   
  259.         }   
  260.   
  261.         /*海王星轨道*/   
  262.         .neptuneOrbit {   
  263.             left:20px;   
  264.             top:20px;   
  265.             height: 760px;   
  266.             width: 760px;   
  267.             background-color: transparent;   
  268.             border-radius: 50%;   
  269.             border-style: dashed;   
  270.             border-color: gray;   
  271.             position: absolute;   
  272.             border-width: 1px;   
  273.             /*margin: 100px;*/   
  274.             /*transform-origin: -75px -75px;*/   
  275.             /*animation: rotate 4s infinite linear;*/   
  276.             margin: 0px;   
  277.             padding: 0px;   
  278.         }   
  279.   
  280.         @keyframes rotate {   
  281.             100% {   
  282.                 transform: rotate(-360deg);   
  283.             }   
  284.         }   
  285.   
  286.     </style>  
  287.   
  288. </head>  
  289. <body>  
  290.     <div class="solarsys">  
  291.         <!--太阳-->  
  292.         <div class='sun'></div>  
  293.   
  294.         <!--水星轨道-->  
  295.         <div class='mercuryOrbit'></div>  
  296.   
  297.         <!--水星-->  
  298.         <div class='mercury'></div>  
  299.   
  300.         <!--金星轨道-->  
  301.         <div class='venusOrbit'><
更多精彩内容其他人还在看

网页注释在IE中产生文字溢出

实验代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transiti
收藏 0 赞 0 分享

HTML教程:定义列表

原文:http://andymao.com/andy/post/104.html 上一节:有序列表 写完“无序列表”和“有序列表”之后已经有人和我说这两篇看得没什么意思。这两篇文章如果只以单向读取的形式阅读那么的确是没什
收藏 0 赞 0 分享

HTML教程:有序列表

原文:http://andymao.com/andy/post/103.html 上一节:无序列表 信息有时候是无序归纳的,有的却有着明确的顺序,在上一篇也提到了。那么简单的来想一下身边有哪些事物是有先后顺序的:操作步骤、排行榜、书目录……
收藏 0 赞 0 分享

HTML教程:无序列表

原文:http://andymao.com/andy/post/102.html 段落已经讲完了,那么一些基本的应用方式也讲了一些,那么是否已经应用了呢?当然应用可以更为丰富,那么这些就需要自己在实际工作中不断的摸索与思考,然后创新并总结得出新的应用形式。当然了段落不能当作
收藏 0 赞 0 分享

HTML网页制作的强大8条技巧

  虽然现在有许多网页制作工具能让您轻松地完成工作,但如果使用HTML则可以得到更大控制权,下面介绍几个小技巧。   1。使用<tt>,<i>,<br>语句来控制文字排版比用<pre>好得多。 如: <tt>实用
收藏 0 赞 0 分享

网页表格分割线去除方法

网页表格分割线去除方法。 其实上面的三个表格都有三行三列,隐藏分隔线的诀窍在于rules,察看这三个表格的源代码,我们可以看到<TABLE>标签中都有rules。它有三个参数(cols,rows,none),当rules=cols时,表格会隐藏纵向的分隔线,这
收藏 0 赞 0 分享

blockquote标记应用注意

关于语义化,不是一句两句就能说明白的,而且现在也没有一个官方的很严格的定义。关于<blockquote>没有争议的是: 1.引用一段较长的文字 2.可以使用cite标签或者属性 问题是<blockquote>引用的文字必须使用块级元素将他
收藏 0 赞 0 分享

网页表格表框制作技巧

网页表格表框制作技巧。 -------------------------------------------------------------------------------- 表格边框的显示与隐藏,是可以用frame参数来控制的。请注意它只控制表格的边框图,而不
收藏 0 赞 0 分享

HTML其实就是学习几个重要标记的应用

《这将是一场革命》一文出来以后。得到业界大伙的认同,当然与此同时也得到部分来自内部与外部的挑衅,不过的更加多的是更多人来寻问每一个点的细节。今晚回家很早就睡了,半夜在一个梦中醒来,梦里正在和小学的同学玩一个游戏——“The Next&rdquo
收藏 0 赞 0 分享

移动端专用的meta标签设置大全

不知道有没有人觉得,html的meta标签描述的头部信息特别多,下面这篇文章主要给大家分享介绍了关于移动端专用的meta设置的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
收藏 0 赞 0 分享
查看更多