css3+js realizes 3D planetary movement
This time I will bring you css3+js to realize 3D planetary operation. What are the precautions for css3+js to realize 3D planetary operation? The following is a practical case, let’s take a look.
HTML part
<p class="path-Saturn"> <p id="Saturn" title="土星"> <p class="x"></p> <p class="y"></p> <p class="z"></p> <p class="space space-x"></p> <p class="space space-x1"></p> <p class="space space-x2"></p> <p class="space space-y"></p> <p class="space space-y1"></p> <p class="space space-y2"></p> <p class="space space-z"></p> <p class="space space-z1"></p> <p class="space space-z2"></p> <!-- 卫星 --> <p class="path-satellite"> <p id="satellite" title="卫星"> <p class="x"></p> <p class="y"></p> <p class="z"></p> <p class="space space-x"></p> <p class="space space-x1"></p> <p class="space space-x2"></p> <p class="space space-y"></p> <p class="space space-y1"></p> <p class="space space-y2"></p> <p class="space space-z"></p> <p class="space space-z1"></p> <p class="space space-z2"></p> </p> </p> </p> </p>
css part
.path-Saturn, .path-earth, .path-Venus, .path-Neptune, .path-Jupiter, .path-Mercury, .path-satellite, .path-moon{ position: absolute; width: 95%; height: 95%; top: 2.5%; left: 2.5%; border: 1px solid #ddd; border-radius: 50%; transform: rotateX(60deg); transform-style: preserve-3d; } #sun, #earth, #Saturn, #Venus, #Neptune, #Jupiter, #Mercury, #satellite, #moon{ width: 160px; height: 160px; position: absolute; transform-style: preserve-3d; top: 50%; left: 50%; margin: -80px 0 0 -80px; animation: rotateForward 10s linear infinite; cursor: pointer; transform: translateZ(-80px); } /*x, y, z轴*/ .x, .y, .z{ position: absolute; height: 100%; border: 1px solid #999; left: 50%; margin-left: -1px; } .y{ transform: rotateZ(90deg); } .z{ transform: rotateX(90deg); } @keyframes rotateForward { 0%{ transform: rotate3d(1, 1, 1, 0deg); } 100%{ transform: rotate3d(1, 1, 1, -360deg); } } /*Saturn*/ #Saturn{ width: 80px; height: 80px; left: 0%; margin: -40px 0 0 -40px; animation: rotateForward 4s linear infinite; transform: translateZ(-40px); } #Saturn .space{ width: 80px; height: 80px; box-shadow: 0 0 60px rgba(90, 80, 53, 1); background-color: rgba(90, 80, 53, .3); } #Saturn .space-x1, #Saturn .space-x2, #Saturn .space-y1, #Saturn .space-y2, #Saturn .space-z1, #Saturn .space-z2{ width: 87.5%; height: 87.5%; top: 6.25%; left: 6.25%; transform: rotate3d(0, 0, 0, 0deg) translateZ(20px); } #Saturn .space-x1{ transform: rotate3d(0, 0, 0, 0deg) translateZ(-20px); } #Saturn .space-y{ transform: rotate3d(0, 1, 0, 90deg) translateZ(0px); } #Saturn .space-y1{ transform: rotate3d(0, 1, 0, 90deg) translateZ(-20px); } #Saturn .space-y2{ transform: rotate3d(0, 1, 0, 90deg) translateZ(20px); } #Saturn .space-z{ transform: rotate3d(1, 0, 0, 90deg) translateZ(0px); } #Saturn .space-z1{ transform: rotate3d(1, 0, 0, 90deg) translateZ(-20px); } #Saturn .space-z2{ transform: rotate3d(1, 0, 0, 90deg) translateZ(20px); }
JS part
(function(planetObj, TimeArr, judgeDirec) { //检测参数是否规范 var timeRegexp = /^[1-9][0-9]*$/, direcRegexp = /^[01]$/; function checkArgs (arg, ele, regexp) { if(arg){ $(arg).each(function (i, item) { if(arg.length != planetObj.length || !regexp.test(item)){ throw Error('an error occured'); return; }else{ return arg; } }) }else{ arg = []; for(var i = 0; i < planetObj.length; i++){ arg.push(ele); } } return arg; } TimeArr = checkArgs(TimeArr, 50, timeRegexp); judgeDirec = checkArgs(judgeDirec, 1, direcRegexp); var PathArr = []; $(planetObj).each(function (i, item) { var n = 0; //定义一个标识,来判断当前是怎么运动的 PathArr.push({ a : $(item).parent().width() / 2, b : $(item).parent().height() / 2 }); //变化x坐标,然后根据椭圆轨迹,获得y坐标,以达到运动的效果 function getEllopsePath (x, PathObj) { x = x - PathObj.a; var m; n ? (judgeDirec[i] ? m = 1 : m = -1) : (judgeDirec[i] ? m = -1 : m = 1); //判断开根号求得的y值是否为负数,从而确定旋转方向 // if(judgeDirec[i]){ // n ? (m = judgeDirec[i]) : (m = judgeDirec[i]-2); // }else{ // n ? (m = judgeDirec[i] - 1) : (m = judgeDirec[i] + 1); // } return Math.sqrt((1 - x * x / (PathObj.a * PathObj.a)) * PathObj.b * PathObj.b) * m + PathObj.b; } function moving () { var x = parseInt($(item).css('left'), 10); if(x == 2 * PathArr[i].a){ //到达轨迹的右零界点的时候x减小 n--; }else if (x == 0) { //到达轨迹的左临界点的时候,x增加 n++; } n ? x++ : x--; $(item).css({ 'top' : getEllopsePath(x, PathArr[i]) + 'px', 'left' : x + 'px' }); } setInterval(moving, TimeArr[i]); }); })(['#Saturn', '#earth', '#Venus', '#Neptune', '#Mercury', '#Jupiter', '#satellite', '#moon'], [40, 180, 240, 20, 120, 200, 30, 10]/*option默认为50毫秒*/, [1, 0, 0, 0, 1, 0, 1, 1]/*option 判断运动方向,0为顺时针,1为逆时针,默认为逆时针*/);
Attached below is a rendering
Detailed explanation of dynamic loading of css
How to use the webkit-tap-highlight-color attribute of CSS3
The above is the detailed content of css3+js realizes 3D planetary movement. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).
