HTML WEBAPP CSS -Mô phỏng hệ mặt trời -Trái đất -Mặt trăng đơn giản trên nền vũ trụ đen sao lấp lánh

Giaoan.link chí sẻ đến các bạn code của một project html nhỏ gồm sự kết hợp giữa css + js + html để mô phỏng hoạt động của mặt trời -Trái đất -Mặt trăng đơn giản. Mặt trăng quay quanh trái đất và cùng quay quanh mặt trời trên nền vũ trụ đên cùng xa số những ngôi sao lấp lánh khá thú vị. Dưới đây là mã code, video mô tả, cùng demo thực tế.

Bạn xem thêm nhiều project khác tại đây.

Các project hiển thị ngẫu nhiên:

Mã code

<!DOCTYPE html>  
<html lang="vi">  
<head>  
    <meta charset="UTF-8">  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">  
    <title>Hệ Mặt Trời Đơn Giản</title>  
    <style>  
        body {  
            margin: 0;  
            height: 100vh;  
            display: flex;  
            align-items: center;  
            justify-content: center;  
            background-color: black;  
            overflow: hidden;  
            position: relative;  
        }  
        .container {  
            font-size: 10px;  
            width: 40em;  
            height: 40em;  
            position: relative;  
            z-index: 10; /* Đặt trên nền sao */  
        }  
        .sun {  
            position: absolute;  
            top: 15em;  
            left: 15em;  
            width: 10em;  
            height: 10em;  
            background-color: yellow;  
            border-radius: 50%;  
            box-shadow: 0 0 3em white;  
        }  
        .earth,  
        .moon {  
            position: absolute;  
            border-style: solid;  
            border-color: white transparent transparent transparent;  
            border-width: 0.1em 0.1em 0 0;  
            border-radius: 50%;  
        }  
        .earth {  
            top: 5em;  
            left: 5em;  
            width: 30em;  
            height: 30em;  
            animation: orbit 36.5s linear infinite;  
        }  
        .moon {  
            top: 0;  
            right: 0;  
            width: 8em;  
            height: 8em;  
            animation: orbit 2.7s linear infinite;  
        }  
        .earth::before,  
        .moon::before {  
            content: '';  
            position: absolute;  
            border-radius: 50%;  
        }  
        .earth::before {  
            top: 2.8em;  
            right: 2.8em;  
            width: 3em;  
            height: 3em;  
            box-shadow: 0 0 1em white;
            background-color: #18adcd;  
        }  
        .moon::before {  
            top: 0.8em;  
            right: 0.2em;  
            width: 1.2em;  
            height: 1.2em;  
            background-color: silver;  
        }  
        @keyframes orbit {  
            to {  
                transform: rotate(360deg);  
            }  
        }  
        /* Nền dải ngân hà */  
        .star {  
            position: absolute;  
            border-radius: 50%;  
            background-color: white; /* Màu sắc cơ bản */  
            animation: twinkle 1.5s infinite alternate;  
        }  
        @keyframes twinkle {  
            0% {  
                opacity: 0.7;  
                transform: scale(1);  
            }  
            50% {  
                opacity: 1;  
                transform: scale(1.3); /* Nhấp nháy */  
            }  
            100% {  
                opacity: 0.7;  
                transform: scale(1);  
            }  
        }  
        /* Thêm phong cách cho tên */  
        .label {  
            position: absolute;  
            color: white;  
            font-size: 1.2em;  
            text-align: center;  
            white-space: nowrap;  
        }  
        .sun-label {  
            top: 22em;  /* 10em + 2em cho khoảng cách với mặt trời */  
            left: 17.5em; /* Đặt vào giữa mặt trời */  
        }  
        .earth-label {  
            top: 8.5em;  /* 5em + 2.5em cho khoảng cách với trái đất */  
            left: 17em; /* Đặt vào giữa trái đất */  
        }  
        .moon-label {  
            top: 1em; /* Vị trí bên trên mặt trăng */  
            left: 5em; /* Đặt vào giữa mặt trăng theo chiều ngang */  
        }  
    </style>  
</head>  
<body>  
    <div class="container">  
        <div class="sun"></div>  
        <div class="earth">  
            <div class="moon"></div>  
        </div>  
        <div class="label sun-label">Mặt Trời</div>  
        <div class="label earth-label">Trái Đất</div>  
        <div class="label moon-label">Mặt Trăng</div>  
    </div>  

    <script>  
        const starCount = 300; // Số lượng ngôi sao  
        const container = document.body;  

        for (let i = 0; i < starCount; i++) {  
            const star = document.createElement('div');  
            star.classList.add('star');  

            // Thiết lập kích thước ngôi sao ngẫu nhiên  
            const size = Math.random() * 3 + 1; // Kích thước từ 1 đến 4  
            star.style.width = `${size}px`;  
            star.style.height = `${size}px`;  

            // Thiết lập vị trí ngẫu nhiên  
            const xPos = Math.random() * 100; // Vị trí x ngẫu nhiên  
            const yPos = Math.random() * 100; // Vị trí y ngẫu nhiên  
            star.style.left = `${xPos}vw`;  
            star.style.top = `${yPos}vh`;  

            // Thiết lập màu sắc ngẫu nhiên (trắng hoặc xanh nhạt)  
            star.style.backgroundColor = Math.random() < 0.5 ? 'white' : 'lightblue';  

            // Thêm ngôi sao vào body  
            container.appendChild(star);  
        }  
    </script>  
</body>  
</html>