CSS3方法
DEMO演示
@keyframes
,animation
, transform
三样便可以搞定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> #center { width:10px; height: 10px; border-radius: 5px; background: #000; position: relative; margin: 200px auto; } #dot { width: 10px; height: 10px; background: red; position: absolute; border-radius: 5px; left: 200px; top: 0;
-o-transform: rotate(90deg); -moz-transform: rotate(90deg); -webkit-transform: rotate(90deg); -ms-transform: rotate(90deg); transform: rotate(90deg);
-o-transform-origin: -2000%; -webkit-transform-origin: -2000%; -moz-transform-origin: -2000%; -ms-transform-origin: -2000%; transform-origin: -2000%;
animation: rot 3s linear 0s infinite; -o-animation: rot 3s linear 0s infinite; -ms-animation: rot 3s linear 0s infinite; -webkit-animation: rot 3s linear 0s infinite; -moz-animation: rot 3s linear 0s infinite; }
@keyframes rot { from{ transform: rotate(0deg); } to{ transform: rotate(360deg); } } @-o-keyframes rot { from{ -o-transform: rotate(0deg); } to{ -o-transform: rotate(360deg); } } @-webkit-keyframes rot { from{ -webkit-transform: rotate(0deg); } to{ -webkit-transform: rotate(360deg); } } @-moz-keyframes rot { from{ -moz-transform: rotate(0deg); } to{ -moz-transform: rotate(360deg); } } @-ms-keyframes rot { from{ -ms-transform: rotate(0deg); } to{ -ms-transform: rotate(360deg); } } </style> </head> <body> <div id="center"> <div id="dot"></div> </div> </body> </html>
|
javascript
DEMO演示
Math.cos()
, Math.sin()
,这两个函数的参数都是弧度,所以可以用 Math.PI * 2/360
将角度转化成弧度。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> #center { width:10px; height: 10px; border-radius: 5px; background: #000; position: relative; margin: 200px auto; } #dot { width: 10px; height: 10px; background: red; position: absolute; border-radius: 5px; left: 200px; top: 0; } </style> </head> <body> <div id="center"> <div id="dot"></div> </div>
<script type="text/javascript"> var oDot = document.getElementById('dot'); var deg = 0; var timer = null; var radius = oDot.offsetLeft;
timer = setInterval(function() { var radian = (2*Math.PI/360)*deg; var theTop = Math.ceil(Math.sin(radian)*radius); var theLeft = Math.ceil(Math.cos(radian)*radius); oDot.style.top = theTop + 'px'; oDot.style.left = theLeft + 'px'; deg += 0.5; if (deg === 361) { deg = 0; } },1); </script> </body> </html>
|