【CSS3】模拟太阳系八大行星运行轨迹

Tips

  1. Safari 浏览器需设置 -webkit- 前缀,Chrome 倒是都可以;
  2. -webkit- 的渐变角度进行需要换算: 90 - 原角度 = 需设角度:
    background-image: linear-gradient(135deg, red, yellow);
    background-image: -webkit-linear-gradient(-45deg, red, yellow);
  3. -webkit- 径向渐变属性为百分号,"shape size at position 不适用:
    background-image: radial-gradient(ellipse farthest-corner at 90% 90%, red, yellow);
    background-image: -webkit-radial-gradient(90% 90%, red, yellow);
  4. animation-play-state 可被 animation 属性合并简写,但在 -webkit-animation 中需单独定义:
    animation: rotate 1s infinite linear paused;
    -webkit-animation: rotate 500s infinite linear; -webkit-animation-play-state: paused;


第一种实现:【改变元素中心(公转)】

Tips:
1. "行星" 与 "轨道" 分离,不共用元素标签;
2. 利用 transform-origin 改变元素中心位置,达到 "星体" 位于最左端,最右端连接圆心,来实现 "公转";
3. transform-origin: x y; 中("z" 此处不需要),x 的值为 "星体" 外边距的值即 margin-left,y 的值为 "星体" 的高的一半,即 height / 2
【即 transform-origin: mragin-left height/2

在这里插入图片描述

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<title></title>
	<style type="text/css">
		body {
			padding: 0;
			margin: 0;
			background-color: black;
		}
		/* 拉伸 */
		.solar-system {
			position: absolute;
			min-width: 100%;
			min-height: 100%;
			/* 缩小 */
			transform: scale(0.75);
			-webkit-transform: scale(0.75);
		}
		/* 圆心 */
		.circle-center {
			position: absolute;
			width: 2px;
			height: 2px;
			border-radius: 50%;
			background-color: black;
			top: 50%;
			left: 50%;
			margin-top: -1px;
			margin-left: -1px;
			z-index: 1;
		}
		/* 太阳 */
		.sun {
			position: absolute;
			width: 80px;
			height: 80px;
			border-radius: 50%;
			background-color: rgba(223, 66, 5, 1);
			box-shadow: 0 0 10px 10px rgba(255, 104, 0, 0.9);
			top: 50%;
			left: 50%;
			margin-top: -40px;
			margin-left: -40px;
		}

		/* - - - - - - 八大行星 - - - - - - */
		/*  水星 */
		.mercury {
			position: absolute;
			width: 10px;
			height: 10px;
			border-radius: 50%;
			background-color: rgba(173, 120, 54, 1);
			top: 50%;
			left: 50%;
			margin-top: -5px;
			margin-left: -63px;
			z-index: 1;

			transform-origin: 63px 5px;
			animation: rotate 1.5s infinite linear paused;
			-webkit-transform-origin: 63px 5px;
			-webkit-animation: rotate 1.5s infinite linear;
			-webkit-animation-play-state: paused;
		}
		/* 金星 */
		.venus {
			position: absolute;
			width: 30px;
			height: 30px;
			border-radius: 50%;
			background-color: rgba(216, 189, 154, 1);
			top: 50%;
			left: 50%;
			margin-top: -15px;
			margin-left: -100px;
			z-index: 1;

			transform-origin: 100px 15px;
			animation: rotate 3s infinite linear paused;
			-webkit-transform-origin: 100px 15px;
			-webkit-animation: rotate 3s infinite linear;
			-webkit-animation-play-state: paused;
		}
		/* 地球 */
		.earth {
			position: absolute;
			width: 20px;
			height: 20px;
			border-radius: 50%;
			background-image: linear-gradient(135deg, rgb(12, 133, 0) 10%, rgb(0, 53, 255) 100%);
			background-image: -webkit-linear-gradient(-45deg, rgb(12, 133, 0) 10%, rgb(0, 53, 255) 100%);
			top: 50%;
			left: 50%;
			margin-top: -10px;
			margin-left: -139px;
			z-index: 1;

			transform-origin: 139px 10px;
			animation: rotate 6s infinite linear paused;
			-webkit-transform-origin: 139px 10px;
			-webkit-animation: rotate 6s infinite linear;
			-webkit-animation-play-state: paused;
		}
		/* 火星 */
		.mars {
			position: absolute;
			width: 14px;
			height: 14px;
			border-radius: 50%;
			background-color: rgba(152, 120, 94, 1);
			top: 50%;
			left: 50%;
			margin-top: -7px;
			margin-left: -182px;
			z-index: 1;

			transform-origin: 182px 7px;
			animation: rotate 11s infinite linear paused;
			-webkit-transform-origin: 182px 7px;
			-webkit-animation: rotate 11s infinite linear;
			-webkit-animation-play-state: paused;
		}
		/* 木星 */
		.jupiter {
			position: absolute;
			width: 50px;
			height: 50px;
			border-radius: 50%;
			background-image: repeating-linear-gradient(-25deg, rgb(162, 141, 117) 5%, rgb(247, 239, 210) 50%, rgb(162, 141, 117) 100%);
			background-image: -webkit-repeating-linear-gradient(115deg, rgb(162, 141, 117) 5%, rgb(247, 239, 210) 50%, rgb(162, 141, 117) 100%);
			top: 50%;
			left: 50%;
			margin-top: -25px;
			margin-left: -275px;
			z-index: 1;

			transform-origin: 275px 25px;
			animation: rotate 50s infinite linear paused;
			-webkit-transform-origin: 275px 25px;
			-webkit-animation: rotate 50s infinite linear;
			-webkit-animation-play-state: paused;
		}
		/* 土星 */
		.saturn {
			position: absolute;
			width: 34px;
			height: 34px;
			border-radius: 50%;
			background-image: repeating-linear-gradient(30deg, rgb(113, 72, 46) 5%, rgb(220, 180, 109) 50%, rgb(113, 72, 46) 100%);
			background-image: -webkit-repeating-linear-gradient(60deg, rgb(113, 72, 46) 5%, rgb(220, 180, 109) 50%, rgb(113, 72, 46) 100%);
			top: 50%;
			left: 50%;
			margin-top: -17px;
			margin-left: -329px;
			z-index: 1;

			transform-style: preserve-3d;
			transform-origin: 329px 18px;
			animation: rotate 180s infinite linear paused;
			-webkit-transform-style: preserve-3d;
			-webkit-transform-origin: 329px 18px;
			-webkit-animation: rotate 180s infinite linear;
			-webkit-animation-play-state: paused;
		}
		/* 土星环 */
		.saturn:after {
			position: absolute;
			content: '';
			width: 44px;
			height: 16px;
			border-radius: 50%;
			border: 8px solid rgba(136, 84, 30, 1);
			margin-top: 0px;
			margin-left: -13px;

			transform: rotateX(60deg) rotateY(30deg);
			-webkit-transform: rotateX(60deg) rotateY(30deg);
		}
		/* 天王星 */
		.uranus {
			position: absolute;
			width: 44px;
			height: 44px;
			border-radius: 50%;
			background-image: radial-gradient(ellipse farthest-corner at 90% 90%, rgb(170, 245, 246) 15%, rgb(1, 102, 121) 60%);
			background-image: -webkit-radial-gradient(90% 90%, rgb(170, 245, 246) 15%, rgb(1, 102, 121) 60%);
			top: 50%;
			left: 50%;
			margin-top: -22px;
			margin-left: -399px;
			z-index: 1;

			transform-style: preserve-3d;
			transform-origin: 399px 20px;
			animation: rotate 500s infinite linear paused;
			-webkit-transform-style: preserve-3d;
			-webkit-transform-origin: 399px 20px;
			-webkit-animation: rotate 500s infinite linear;
			-webkit-animation-play-state: paused;
		}
		/* 天王星环 */
		.uranus:after {
			position: absolute;
			content: '';
			width: 50px;
			height: 20px;
			border-radius: 50%;
			border: 10px solid rgba(255, 255, 255, 0.3);
			margin-top: 0px;
			margin-left: -13px;

			transform: rotateX(60deg) rotateY(-45deg);
			-webkit-transform: rotateX(60deg) rotateY(-45deg);
		}
		/* 海王星 */
		.neptune {
			position: absolute;
			width: 26px;
			height: 26px;
			border-radius: 50%;
			background-color: rgba(98, 134, 231, 1);
			top: 50%;
			left: 50%;
			margin-top: -13px;
			margin-left: -439px;
			z-index: 1;

			transform-origin: 439px 13px;
			animation: rotate 1000s infinite linear paused;
			-webkit-transform-origin: 439px 13px;
			-webkit-animation: rotate 1000s infinite linear;
			-webkit-animation-play-state: paused;
		}

		/* - - - - - - 行星轨道 - - - - - - */
		/* 水星轨道 */
		.mercury-orbit {
			position: absolute;
			width: 116px;
			height: 116px;
			border-radius: 50%;
			border: 1px solid rgba(255, 255, 255, 0.5);
			top: 50%;
			left: 50%;
			margin-top: -59px;
			margin-left: -59px;
		}
		/* 金星轨道 */
		.venus-orbit {
			position: absolute;
			width: 170px;
			height: 170px;
			border-radius: 50%;
			border: 1px solid rgba(255, 255, 255, 0.5);
			top: 50%;
			left: 50%;
			margin-top: -86px;
			margin-left: -86px;
		}
		/* 地球轨道 */
		.earth-orbit {
			position: absolute;
			width: 258px;
			height: 258px;
			border-radius: 50%;
			border: 1px solid rgba(255, 255, 255, 0.5);
			top: 50%;
			left: 50%;
			margin-top: -130px;
			margin-left: -130px;
		}
		/* 火星轨道 */
		.mars-orbit {
			position: absolute;
			width: 350px;
			height: 350px;
			border-radius: 50%;
			border: 1px solid rgba(255, 255, 255, 0.5);
			top: 50%;
			left: 50%;
			margin-top: -176px;
			margin-left: -176px;
		}
		/* 木星轨道 */
		.jupiter-orbit {
			position: absolute;
			width: 500px;
			height: 500px;
			border-radius: 50%;
			border: 1px solid rgba(255, 255, 255, 0.5);
			top: 50%;
			left: 50%;
			margin-top: -251px;
			margin-left: -251px;
		}
		/* 土星轨道 */
		.saturn-orbit {
			position: absolute;
			width: 624px;
			height: 624px;
			border-radius: 50%;
			border: 1px solid rgba(255, 255, 255, 0.5);
			top: 50%;
			left: 50%;
			margin-top: -313px;
			margin-left: -313px;
		}
		/* 天王星轨道 */
		.uranus-orbit {
			position: absolute;
			width: 754px;
			height: 754px;
			border-radius: 50%;
			border: 1px solid rgba(255, 255, 255, 0.5);
			top: 50%;
			left: 50%;
			margin-top: -378px;
			margin-left: -378px;
		}
		/* 海王星轨道 */
		.neptune-orbit {
			position: absolute;
			width: 852px;
			height: 852px;
			border-radius: 50%;
			border: 1px solid rgba(255, 255, 255, 0.5);
			top: 50%;
			left: 50%;
			margin-top: -427px;
			margin-left: -427px;
		}

		/* - - - - - - 控件 - - - - - - */
		.controls {
			position: absolute;
			margin-top: 50px;
			margin-left: 50px;
			z-index: 1;
		}
		.btn {
			padding: 10px 20px;
			outline: 0 none;
			border-radius: 15px;
			cursor: pointer;
		}
		/* 控制动画开始, 默认停止 */
		.on {
			animation-play-state: running !important;
			-webkit-animation-play-state: running !important;
		}

		/* - - - - - - 动画 - - - - - - */
		@keyframes rotate {
			0% {transform: rotate(0deg);}
			100% {transform: rotate(-360deg);}
		}
		@-webkit-keyframes rotate {
			0% {-webkit-transform: rotate(0deg);}
			100% {-webkit-transform: rotate(-360deg);}
		}
	</style>
	<script type="text/javascript">
		window.onload = function() {
			// 获取元素
			var toggle = document.getElementById('toggle');
				toggle.textContent = 'Strat';

			// 封闭空间与数组, 处理掉非元素节点
			var planets = [];
			;(function() {
				var nodes = document.getElementsByClassName('eight-planets')[0].childNodes;
				for (var i = 0; i < nodes.length; i++) {
					if (nodes[i].nodeType === 1) {
						// 填充数组
						planets.push(nodes[i]);
					}
				}
			})();

			// 类名变更函数
			function changeClass() {
				for (var i = 0; i < planets.length; i++) {
					var curClass = planets[i].getAttribute('class');

					var change = '';
					if (curClass.indexOf(' on') === -1) {
						change = curClass + ' on';
					} else {
						change = curClass.replace(' on', '');
					}

					planets[i].setAttribute('class', change);
				}
			}

			// 开始与暂停
			toggle.onclick = function() {
				if (this.textContent === 'Strat') {
					toggle.textContent = 'Stop';
				} else {
					toggle.textContent = 'Strat';
				}
				changeClass('on');
			}
		}
	</script>
</head>
<body>
	<div class="solar-system">
		<!-- 中心区 -->
		<div class="main-center">
			<!-- 参考圆心 -->
			<div class="circle-center"></div>
			<!-- 太阳 -->
			<div class="sun"></div>
		</div>

		<!-- 八大行星 -->
		<div class="eight-planets">
			<!-- 水星 -->
			<div class="mercury"></div>
			<!-- 金星 -->
			<div class="venus"></div>
			<!-- 地球 -->
			<div class="earth"></div>
			<!-- 火星 -->
			<div class="mars"></div>
			<!-- 木星 -->
			<div class="jupiter"></div>
			<!-- 土星 -->
			<div class="saturn"></div>
			<!-- 天王星 -->
			<div class="uranus"></div>
			<!-- 海王星 -->
			<div class="neptune"></div>
		</div>

		<!-- 行星轨道 -->
		<div class="planets-orbit">
			<!-- 水星轨道 -->
			<div class="mercury-orbit"></div>
			<!-- 金星轨道 -->
			<div class="venus-orbit"></div>
			<!-- 地球轨道 -->
			<div class="earth-orbit"></div>
			<!-- 火星轨道 -->
			<div class="mars-orbit"></div>
			<!-- 木星轨道 -->
			<div class="jupiter-orbit"></div>
			<!-- 土星轨道 -->
			<div class="saturn-orbit"></div>
			<!-- 天王星轨道 -->
			<div class="uranus-orbit"></div>
			<!-- 海王星轨道 -->
			<div class="neptune-orbit"></div>
		</div>
	</div>
	<!-- 控件 -->
	<div class="controls">
		<button class="btn" id="toggle"></button>
	</div>
</body>
</html>


第二种实现:【旋转元素自身(自转)】

Tips:
1. 将所有元素于某一点居中,"行星" 位于 "轨道" 的边部,实则是 "轨道" 自转,带动 "行星" 运动,造成 "公转" 效果;
2. 将 "星体" 设置位于 "轨道" 边部中间位置,此时 "星体" 距离 "圆心" 的距离,即为 "公转" 半径;
3. 是最简单最省事的方法,但如此的话,在旋转过程中,元素的角会撑开父元素(实际面积还是矩形);

在这里插入图片描述

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<title></title>
	<style type="text/css">
		body {
			padding: 0;
			margin: 0;
			background-color: black;
		}
		/* 拉伸 */
		.solar-system {
			position: absolute;
			min-width: 100%;
			min-height: 100%;
			/* 缩小 */
			transform: scale(0.75);
			-webkit-transform: scale(0.75);
		}
		/* 太阳 */
		.sun {
			position: absolute;
			width: 80px;
			height: 80px;
			border-radius: 50%;
			background-color: rgba(223, 66, 5, 1);
			box-shadow: 0 0 10px 10px rgba(255, 104, 0, 0.9);
			top: 50%;
			left: 50%;
			margin-top: -40px;
			margin-left: -40px;
		}

		/* - - - - - - 八大行星 - - - - - - */
		/* 水星轨道 */
		.mercury {
			position: absolute;
			width: 116px;
			height: 116px;
			border-radius: 50%;
			border: 1px solid rgba(255, 255, 255, 0.5);
			top: 50%;
			left: 50%;
			margin-top: -59px;
			margin-left: -59px;

			animation: rotate 1.5s infinite linear paused;
			-webkit-animation: rotate 1.5s infinite linear;
			-webkit-animation-play-state: paused;
		}
		/* 水星 */
		.mercury:after {
			position: absolute;
			content: '';
			width: 10px;
			height: 10px;
			border-radius: 50%;
			background-color: rgba(173, 120, 54, 1);
			margin-top: 53px;
			margin-left: -5px;
		}
		/* 金星轨道 */
		.venus {
			position: absolute;
			width: 170px;
			height: 170px;
			border-radius: 50%;
			border: 1px solid rgba(255, 255, 255, 0.5);
			top: 50%;
			left: 50%;
			margin-top: -86px;
			margin-left: -86px;

			animation: rotate 3s infinite linear paused;
			-webkit-animation: rotate 3s infinite linear;
			-webkit-animation-play-state: paused;
		}
		/* 金星 */
		.venus:after {
			position: absolute;
			content: '';
			width: 30px;
			height: 30px;
			border-radius: 50%;
			background-color: rgba(216, 189, 154, 1);
			margin-top: 70px;
			margin-left: -15px;
		}
		/* 地球轨道 */
		.earth {
			position: absolute;
			width: 258px;
			height: 258px;
			border-radius: 50%;
			border: 1px solid rgba(255, 255, 255, 0.5);
			top: 50%;
			left: 50%;
			margin-top: -130px;
			margin-left: -130px;

			animation: rotate 6s infinite linear paused;
			-webkit-animation: rotate 6s infinite linear;
			-webkit-animation-play-state: paused;
		}
		/* 地球 */
		.earth:after {
			position: absolute;
			content: '';
			width: 20px;
			height: 20px;
			border-radius: 50%;
			background-image: linear-gradient(135deg, rgb(12, 133, 0) 10%, rgb(0, 53, 255) 100%);
			background-image: -webkit-linear-gradient(-45deg, rgb(12, 133, 0) 10%, rgb(0, 53, 255) 100%);
			margin-top: 119px;
			margin-left: -10px;
		}
		/* 火星轨道 */
		.mars {
			position: absolute;
			width: 350px;
			height: 350px;
			border-radius: 50%;
			border: 1px solid rgba(255, 255, 255, 0.5);
			top: 50%;
			left: 50%;
			margin-top: -176px;
			margin-left: -176px;

			animation: rotate 11s infinite linear paused;
			-webkit-animation: rotate 11s infinite linear;
			-webkit-animation-play-state: paused;
		}
		/* 火星 */
		.mars:after {
			position: absolute;
			content: '';
			width: 14px;
			height: 14px;
			border-radius: 50%;
			background-color: rgba(152, 120, 94, 1);
			margin-top: 168px;
			margin-left: -7px;
		}
		/* 木星轨道 */
		.jupiter {
			position: absolute;
			width: 500px;
			height: 500px;
			border-radius: 50%;
			border: 1px solid rgba(255, 255, 255, 0.5);
			top: 50%;
			left: 50%;
			margin-top: -251px;
			margin-left: -251px;

			animation: rotate 50s infinite linear paused;
			-webkit-animation: rotate 50s infinite linear;
			-webkit-animation-play-state: paused;
		}
		/* 木星 */
		.jupiter:after {
			position: absolute;
			content: '';
			width: 50px;
			height: 50px;
			border-radius: 50%;
			background-image: repeating-linear-gradient(-25deg, rgb(162, 141, 117) 5%, rgb(247, 239, 210) 50%, rgb(162, 141, 117) 100%);
			background-image: -webkit-repeating-linear-gradient(115deg, rgb(162, 141, 117) 5%, rgb(247, 239, 210) 50%, rgb(162, 141, 117) 100%);
			margin-top: 225px;
			margin-left: -25px;
		}
		/* 土星轨道 */
		.saturn {
			position: absolute;
			width: 624px;
			height: 624px;
			border-radius: 50%;
			border: 1px solid rgba(255, 255, 255, 0.5);
			top: 50%;
			left: 50%;
			margin-top: -313px;
			margin-left: -313px;

			transform-style: preserve-3d;
			animation: rotate 180s infinite linear paused;
			-webkit-transform-style: preserve-3d;
			-webkit-animation: rotate 180s infinite linear;
			-webkit-animation-play-state: paused;
		}
		/* 土星 */
		.saturn:after {
			position: absolute;
			content: '';
			width: 34px;
			height: 34px;
			border-radius: 50%;
			background-image: repeating-linear-gradient(30deg, rgb(113, 72, 46) 5%, rgb(220, 180, 109) 50%, rgb(113, 72, 46) 100%);
			background-image: -webkit-repeating-linear-gradient(60deg, rgb(113, 72, 46) 5%, rgb(220, 180, 109) 50%, rgb(113, 72, 46) 100%);
			margin-top: 295px;
			margin-left: -17px;
		}
		/* 土星环 */
		.saturn:before {
			position: absolute;
			content: '';
			width: 44px;
			height: 16px;
			border-radius: 50%;
			border: 8px solid rgba(136, 84, 30, 1);
			margin-top: 295px;
			margin-left: -30px;

			transform: rotateX(60deg) rotateY(30deg);
			-webkit-transform: rotateX(60deg) rotateY(30deg);
		}
		/* 天王星轨道 */
		.uranus {
			position: absolute;
			width: 754px;
			height: 754px;
			border-radius: 50%;
			border: 1px solid rgba(255, 255, 255, 0.5);
			top: 50%;
			left: 50%;
			margin-top: -378px;
			margin-left: -378px;

			transform-style: preserve-3d;
			animation: rotate 500s infinite linear paused;
			-webkit-transform-style: preserve-3d;
			-webkit-animation: rotate 500s infinite linear;
			-webkit-animation-play-state: paused;
		}
		/* 天王星 */
		.uranus:after {
			position: absolute;
			content: '';
			width: 44px;
			height: 44px;
			border-radius: 50%;
			background-image: radial-gradient(ellipse farthest-corner at 90% 90%, rgb(170, 245, 246) 15%, rgb(1, 102, 121) 60%);
			background-image: -webkit-radial-gradient(90% 90%, rgb(170, 245, 246) 15%, rgb(1, 102, 121) 60%);
			margin-top: 355px;
			margin-left: -22px;
		}
		/* 天王星环 */
		.uranus:before {
			position: absolute;
			content: '';
			width: 50px;
			height: 20px;
			border-radius: 50%;
			border: 10px solid rgba(255, 255, 255, 0.3);
			margin-top: 355px;
			margin-left: -35px;

			transform: rotateX(60deg) rotateY(-45deg);
			-webkit-transform: rotateX(60deg) rotateY(-45deg);
		}
		/* 海王星轨道 */
		.neptune {
			position: absolute;
			width: 852px;
			height: 852px;
			border-radius: 50%;
			border: 1px solid rgba(255, 255, 255, 0.5);
			top: 50%;
			left: 50%;
			margin-top: -427px;
			margin-left: -427px;

			animation: rotate 1000s infinite linear paused;
			-webkit-animation: rotate 1000s infinite linear;
			-webkit-animation-play-state: paused;
		}
		/* 海王星 */
		.neptune:after {
			position: absolute;
			content: '';
			width: 26px;
			height: 26px;
			border-radius: 50%;
			background-color: rgba(98, 134, 231, 1);
			margin-top: 413px;
			margin-left: -13px;
		}

		/* - - - - - - 控件 - - - - - - */
		.controls {
			position: absolute;
			margin-top: 50px;
			margin-left: 50px;
			z-index: 1;
		}
		.btn {
			padding: 10px 20px;
			outline: 0 none;
			border-radius: 15px;
			cursor: pointer;
		}
		/* 控制动画开始, 默认停止 */
		.on {
			animation-play-state: running !important;
			-webkit-animation-play-state: running !important;
		}

		/* - - - - - - 动画 - - - - - - */
		@keyframes rotate {
			0% {transform: rotate(0deg);}
			100% {transform: rotate(-360deg);}
		}
		@-webkit-keyframes rotate {
			0% {-webkit-transform: rotate(0deg);}
			100% {-webkit-transform: rotate(-360deg);}
		}
	</style>
	<script type="text/javascript">
		window.onload = function() {
			// 获取元素
			var toggle = document.getElementById('toggle');
				toggle.textContent = 'Strat';

			// 封闭空间与数组, 处理掉非元素节点
			var planets = [];
			;(function() {
				var nodes = document.getElementsByClassName('eight-planets')[0].childNodes;
				for (var i = 0; i < nodes.length; i++) {
					if (nodes[i].nodeType === 1) {
						// 填充数组
						planets.push(nodes[i]);
					}
				}
			})();

			// 类名变更函数
			function changeClass() {
				for (var i = 0; i < planets.length; i++) {
					var curClass = planets[i].getAttribute('class');

					var change = '';
					if (curClass.indexOf(' on') === -1) {
						change = curClass + ' on';
					} else {
						change = curClass.replace(' on', '');
					}

					planets[i].setAttribute('class', change);
				}
			}

			// 开始与暂停
			toggle.onclick = function() {
				if (this.textContent === 'Strat') {
					toggle.textContent = 'Stop';
				} else {
					toggle.textContent = 'Strat';
				}
				changeClass('on');
			}
		}
	</script>
</head>
<body>
	<div class="solar-system">
		<!-- 中心区 -->
		<div class="main-center">
			<!-- 太阳 -->
			<div class="sun"></div>
		</div>

		<!-- 八大行星 -->
		<div class="eight-planets">
			<!-- 水星 -->
			<div class="mercury"></div>
			<!-- 金星 -->
			<div class="venus"></div>
			<!-- 地球 -->
			<div class="earth"></div>
			<!-- 火星 -->
			<div class="mars"></div>
			<!-- 木星 -->
			<div class="jupiter"></div>
			<!-- 土星 -->
			<div class="saturn"></div>
			<!-- 天王星 -->
			<div class="uranus"></div>
			<!-- 海王星 -->
			<div class="neptune"></div>
		</div>
	</div>
	<!-- 控件 -->
	<div class="controls">
		<button class="btn" id="toggle"></button>
	</div>
</body>
</html>

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用 CSS3 实现的九大行星绕太阳周转的动画效果: HTML代码: ```html <div class="sun"></div> <div class="mercury"></div> <div class="venus"></div> <div class="earth"></div> <div class="mars"></div> <div class="jupiter"></div> <div class="saturn"></div> <div class="uranus"></div> <div class="neptune"></div> ``` CSS代码: ```css .sun { width: 200px; height: 200px; border-radius: 50%; background-color: #f9d71c; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); z-index: 1; box-shadow: 0 0 50px #f9d71c, 0 0 100px #f9d71c, 0 0 150px #f9d71c, 0 0 200px #f9d71c; animation: rotate-sun 10s linear infinite; } .mercury { width: 20px; height: 20px; border-radius: 50%; background-color: #b5b5b5; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%) translateX(70px); animation: rotate-mercury 3s linear infinite; } .venus { width: 35px; height: 35px; border-radius: 50%; background-color: #f2b02e; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%) translateX(120px); animation: rotate-venus 5s linear infinite; } .earth { width: 40px; height: 40px; border-radius: 50%; background-color: #3a7cff; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%) translateX(160px); animation: rotate-earth 8s linear infinite; } .mars { width: 25px; height: 25px; border-radius: 50%; background-color: #ff4f4f; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%) translateX(200px); animation: rotate-mars 10s linear infinite; } .jupiter { width: 80px; height: 80px; border-radius: 50%; background-color: #c05c3c; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%) translateX(300px); animation: rotate-jupiter 20s linear infinite; } .saturn { width: 70px; height: 70px; border-radius: 50%; background-color: #d8c58e; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%) translateX(400px); animation: rotate-saturn 30s linear infinite; } .uranus { width: 60px; height: 60px; border-radius: 50%; background-color: #6ecfff; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%) translateX(500px); animation: rotate-uranus 40s linear infinite; } .neptune { width: 55px; height: 55px; border-radius: 50%; background-color: #3456a9; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%) translateX(600px); animation: rotate-neptune 50s linear infinite; } @keyframes rotate-sun { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } @keyframes rotate-mercury { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } @keyframes rotate-venus { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } @keyframes rotate-earth { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } @keyframes rotate-mars { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } @keyframes rotate-jupiter { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } @keyframes rotate-saturn { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } @keyframes rotate-uranus { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } @keyframes rotate-neptune { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } ``` 解释一下代码: 1. `.sun` 代表太阳,使用 `border-radius: 50%` 将其设置为圆形,使用 `box-shadow` 添加光晕效果,使用 `animation` 添加旋转动画。 2. `.mercury` 到 `.neptune` 分别代表九大行星,使用不同的颜色、大小、位置和旋转速度等属性,使用 `animation` 添加旋转动画。 3. `@keyframes` 定义了动画执行的关键帧,从 `0%` 开始,旋转角度为 `0deg`,到 `100%` 结束,旋转角度为 `360deg`。 4. 通过设置 `translateX` 属性实现行星的位置偏移,从而实现绕太阳周转的效果。 这样就可以实现大行星绕太阳周转的动画效果了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值