Lâu lâu mới quay lại blog, nay share cho anh em một đoạn code viết bằng HTML và CSS tạo mô hình trái đất và mặt trăng. Trong đó trái đất sẽ quay quanh chính nó, còn mặt trăng sẽ quay quanh trái đất.
Cũng như nhiều lần share code web chỉ bằng HTML và CSS thì việc có demo ảnh và online là không có, bắt buộc bạn phải tự mình làm để chiêm ngưỡng thành quả thôi. Thế mới thú vị, phải không nào.
OK. Để làm được cái này, chúng ta cần chuẩn bị những thứ sau
Trước tiên ta thiết kế bằng HTML trước đã
1 2 3 4 5 6 7 8 9 10 11 12 13 | <div id="moon_holder"> <div id="moon_back"></div> <img src="moon2.png" id="moon"> <div id="moon_ball"></div> </div> <div id="center" align="center"> <div id="earth_holder"> <div id="earth_ball"></div> <div id="earth_glow"></div> <div id="earth"></div> </div> </div> |
Đầu tiên là style cho đoạn “xương” bằng HTML bên trên nhé
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | body { background: #fff url("https://lab.legomushroom.com/___img/sky_star2.jpg") 0px 0px; -ms-overflow-x: hidden; overflow-x: hidden; margin: 0; -webkit-animation: sky 80s linear infinite; -moz-animation: sky 80s linear infinite; -ms-animation: sky 80s linear infinite; -o-animation: sky 80s linear infinite; animation: sky 80s linear infinite; } body div#moon_holder { position: absolute; left: -30%; bottom: 60px; z-index: 6; -webkit-animation: launch_moon 240s linear infinite; -moz-animation: launch_moon 240s linear infinite; -ms-animation: launch_moon 240s linear infinite; -o-animation: launch_moon 240s linear infinite; animation: launch_moon 240s linear infinite; } body div#moon_holder img#moon { width: 90px; height: 90px; border-radius: 90px; position: absolute; z-index: 0; -webkit-animation: launch_moon_size 60s linear infinite; -moz-animation: launch_moon_size 60s linear infinite; -ms-animation: launch_moon_size 60s linear infinite; -o-animation: launch_moon_size 60s linear infinite; animation: launch_moon_size 60s linear infinite; -webkit-box-shadow: -400px -30px 20px rgba(0,0,0,0.50); box-shadow: -400px -30px 20px rgba(0,0,0,0.50); } body div#moon_holder div#moon_ball { z-index: 1; position: absolute; width: 90px; height: 90px; border-radius: 90px; margin: 1px 1px 0px 0px; -webkit-box-shadow: inset 20px -10px 37px #000, inset 0 0 27px #000; box-shadow: inset 20px -10px 37px #000, inset 0 0 27px #000; -webkit-animation: launch_moon_ball_size 240s linear infinite; -moz-animation: launch_moon_ball_size 240s linear infinite; -ms-animation: launch_moon_ball_size 240s linear infinite; -o-animation: launch_moon_ball_size 240s linear infinite; animation: launch_moon_ball_size 240s linear infinite; } body div#moon_holder div#moon_back { width: 90px; height: 90px; border-radius: 90px; position: absolute; z-index: 0; background-color: #000; -webkit-animation: launch_moon_ball_size 240s linear infinite; -moz-animation: launch_moon_ball_size 240s linear infinite; -ms-animation: launch_moon_ball_size 240s linear infinite; -o-animation: launch_moon_ball_size 240s linear infinite; animation: launch_moon_ball_size 240s linear infinite; } body div#earth_holder { position: static; z-index: 1; width: 200px; height: 200px; border-radius: 200px; margin-top: 11%; } body div#earth_holder div#earth { z-index: 2; position: absolute; width: 200px; height: 200px; border-radius: 200px; background: url("http://lab.legomushroom.com/___img/11.jpg") 0px 0px; -webkit-transform: rotate(20deg); -moz-transform: rotate(20deg); -o-transform: rotate(20deg); -ms-transform: rotate(20deg); transform: rotate(20deg); -webkit-box-shadow: inset 1px -3px 14px rgba(0,0,0,0.60), inset 0 0 67px #000, inset 0 0 20px rgba(0,0,0,0.50), -400px -30px 130px rgba(0,0,0,0.50); box-shadow: inset 1px -3px 14px rgba(0,0,0,0.60), inset 0 0 67px #000, inset 0 0 20px rgba(0,0,0,0.50), -400px -30px 130px rgba(0,0,0,0.50); -webkit-animation: rotate_earth 40s linear infinite; -moz-animation: rotate_earth 40s linear infinite; -ms-animation: rotate_earth 40s linear infinite; -o-animation: rotate_earth 40s linear infinite; animation: rotate_earth 40s linear infinite; } body div#earth_holder div#earth_glow { z-index: 4; -webkit-box-shadow: 0 0px 19px rgba(79,156,201,0.80); box-shadow: 0 0px 19px rgba(79,156,201,0.80); position: absolute; width: 200px; height: 200px; border-radius: 200px; } body div#earth_holder div#earth_ball { z-index: 3; position: absolute; width: 200px; height: 200px; border-radius: 200px; -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=74)"; filter: alpha(opacity=74); opacity: 0.74; -webkit-box-shadow: inset 40px -40px 80px #000, inset 20px -20px 100px #000; box-shadow: inset 40px -40px 80px #000, inset 20px -20px 100px #000; } |
Để cho trái đất tự quay quanh mình nó, và biến cái bản đồ rộng kia thành hình trái đất tròn, ta dùng đoạn CSS sau
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 | @keyframes "rotate_earth" { 0% { background-position: 0px -2px; } 50% { background-position: 205px -2px; } 100% { background-position: 410px -2px; } } @-moz-keyframes rotate_earth { 0% { background-position: 0px -2px; } 50% { background-position: 205px -2px; } 100% { background-position: 410px -2px; } } @-webkit-keyframes "rotate_earth" { 0% { background-position: 0px -2px; } 50% { background-position: 205px -2px; } 100% { background-position: 410px -2px; } } @-ms-keyframes "rotate_earth" { 0% { background-position: 0px -2px; } 50% { background-position: 205px -2px; } 100% { background-position: 410px -2px; } } @-o-keyframes "rotate_earth" { 0% { background-position: 0px -2px; } 50% { background-position: 205px -2px; } 100% { background-position: 410px -2px; } } |
Tiếp theo là mặt trăng quay quanh trái đất. Việc style này sẽ phải tính tới hiệu ứng xa gần đối với màn hình. Hơn nữa hiệu ứng bầu trời đầy sao cũng phải di chuyển theo để tạo cảm giác rằng trái đất chúng ta đang di chuyển
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 | @keyframes "launch_moon" { 25% { z-index: 6; } 50% { left: 140%; bottom: 600px; z-index: 0; } 55% { bottom: 680px; } 75% { z-index: 0; } 100% { left: -30%; z-index: 6; bottom: 100px; } } @-moz-keyframes launch_moon { 25% { z-index:6; } 50% { left: 140%; bottom: 600px; z-index: 0; } 55% { bottom: 680px; } 75% { z-index: 0; } 100% { left: -30%; z-index: 6; bottom: 100px; } } @-webkit-keyframes "launch_moon" { 25% { z-index: 6; } 50% { left: 140%; bottom: 600px; z-index: 0; } 55% { bottom: 680px; } 75% { z-index: 0; } 100% { left: -30%; z-index: 6; bottom: 100px; } } @-ms-keyframes "launch_moon" { 25% { z-index: 6; } 50% { left: 140%; bottom: 600px; z-index: 0; } 55% { bottom: 680px; } 75% { z-index: 0; } 100% { left: -30%; z-index: 6; bottom: 100px; } } @-o-keyframes "launch_moon" { 25% { z-index: 6; } 50% { left: 140%; bottom: 600px; z-index: 0; } 55% { bottom: 680px; } 75% { z-index: 0; } 100% { left: -30%; z-index: 6; bottom: 100px; } } @keyframes "launch_moon_size" { 0% { -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)"; filter: alpha(opacity=100); opacity: 1; } 50% { -webkit-transform: scale(1.2, 1.2); -moz-transform: scale(1.2, 1.2); -o-transform: scale(1.2, 1.2); -ms-transform: scale(1.2, 1.2); transform: scale(1.2, 1.2); -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)"; filter: alpha(opacity=100); opacity: 1; } 55% { -webkit-transform: scale(0.8, 0.8); -moz-transform: scale(0.8, 0.8); -o-transform: scale(0.8, 0.8); -ms-transform: scale(0.8, 0.8); transform: scale(0.8, 0.8); -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=70)"; filter: alpha(opacity=70); opacity: 0.7; } 75% { -webkit-transform: scale(0.7, 0.7); -moz-transform: scale(0.7, 0.7); -o-transform: scale(0.7, 0.7); -ms-transform: scale(0.7, 0.7); transform: scale(0.7, 0.7); -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=35)"; filter: alpha(opacity=35); opacity: 0.35; } 100% { -webkit-transform: scale(0.9, 0.9); -moz-transform: scale(0.9, 0.9); -o-transform: scale(0.9, 0.9); -ms-transform: scale(0.9, 0.9); transform: scale(0.9, 0.9); -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=70)"; filter: alpha(opacity=70); opacity: 0.7; } } @-moz-keyframes launch_moon_size { 0% { filter: alpha(opacity=100); opacity: 1; } 50% { -moz-transform: scale(1.2, 1.2); transform: scale(1.2, 1.2); filter: alpha(opacity=100); opacity: 1; } 55% { -moz-transform: scale(0.8, 0.8); transform: scale(0.8, 0.8); filter: alpha(opacity=70); opacity: 0.7; } 75% { -moz-transform: scale(0.7, 0.7); transform: scale(0.7, 0.7); filter: alpha(opacity=35); opacity: 0.35; } 100% { -moz-transform: scale(0.9, 0.9); transform: scale(0.9, 0.9); filter: alpha(opacity=70); opacity: 0.7; } } @-webkit-keyframes "launch_moon_size" { 0% { filter: alpha(opacity=100); opacity: 1; } 50% { -webkit-transform: scale(1.2, 1.2); transform: scale(1.2, 1.2); filter: alpha(opacity=100); opacity: 1; } 55% { -webkit-transform: scale(0.8, 0.8); transform: scale(0.8, 0.8); filter: alpha(opacity=70); opacity: 0.7; } 75% { -webkit-transform: scale(0.7, 0.7); transform: scale(0.7, 0.7); filter: alpha(opacity=35); opacity: 0.35; } 100% { -webkit-transform: scale(0.9, 0.9); transform: scale(0.9, 0.9); filter: alpha(opacity=70); opacity: 0.7; } } @-ms-keyframes "launch_moon_size" { 0% { -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)"; filter: alpha(opacity=100); opacity: 1; } 50% { -ms-transform: scale(1.2, 1.2); transform: scale(1.2, 1.2); -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)"; filter: alpha(opacity=100); opacity: 1; } 55% { -ms-transform: scale(0.8, 0.8); transform: scale(0.8, 0.8); -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=70)"; filter: alpha(opacity=70); opacity: 0.7; } 75% { -ms-transform: scale(0.7, 0.7); transform: scale(0.7, 0.7); -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=35)"; filter: alpha(opacity=35); opacity: 0.35; } 100% { -ms-transform: scale(0.9, 0.9); transform: scale(0.9, 0.9); -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=70)"; filter: alpha(opacity=70); opacity: 0.7; } } @-o-keyframes "launch_moon_size" { 0% { filter: alpha(opacity=100); opacity: 1; } 50% { -o-transform: scale(1.2, 1.2); transform: scale(1.2, 1.2); filter: alpha(opacity=100); opacity: 1; } 55% { -o-transform: scale(0.8, 0.8); transform: scale(0.8, 0.8); filter: alpha(opacity=70); opacity: 0.7; } 75% { -o-transform: scale(0.7, 0.7); transform: scale(0.7, 0.7); filter: alpha(opacity=35); opacity: 0.35; } 100% { -o-transform: scale(0.9, 0.9); transform: scale(0.9, 0.9); filter: alpha(opacity=70); opacity: 0.7; } } @keyframes "launch_moon_ball_size" { 50% { -webkit-transform: scale(1.2, 1.2); -moz-transform: scale(1.2, 1.2); -o-transform: scale(1.2, 1.2); -ms-transform: scale(1.2, 1.2); transform: scale(1.2, 1.2); } 55% { -webkit-transform: scale(0.8, 0.8); -moz-transform: scale(0.8, 0.8); -o-transform: scale(0.8, 0.8); -ms-transform: scale(0.8, 0.8); transform: scale(0.8, 0.8); } 75% { -webkit-transform: scale(0.7, 0.7); -moz-transform: scale(0.7, 0.7); -o-transform: scale(0.7, 0.7); -ms-transform: scale(0.7, 0.7); transform: scale(0.7, 0.7); } 100% { -webkit-transform: scale(0.9, 0.9); -moz-transform: scale(0.9, 0.9); -o-transform: scale(0.9, 0.9); -ms-transform: scale(0.9, 0.9); transform: scale(0.9, 0.9); } } @-moz-keyframes launch_moon_ball_size { 50% { -moz-transform: scale(1.2, 1.2); transform: scale(1.2, 1.2); } 55% { -moz-transform: scale(0.8, 0.8); transform: scale(0.8, 0.8); } 75% { -moz-transform: scale(0.7, 0.7); transform: scale(0.7, 0.7); } 100% { -moz-transform: scale(0.9, 0.9); transform: scale(0.9, 0.9); } } @-webkit-keyframes "launch_moon_ball_size" { 50% { -webkit-transform: scale(1.2, 1.2); transform: scale(1.2, 1.2); } 55% { -webkit-transform: scale(0.8, 0.8); transform: scale(0.8, 0.8); } 75% { -webkit-transform: scale(0.7, 0.7); transform: scale(0.7, 0.7); } 100% { -webkit-transform: scale(0.9, 0.9); transform: scale(0.9, 0.9); } } @-ms-keyframes "launch_moon_ball_size" { 50% { -ms-transform: scale(1.2, 1.2); transform: scale(1.2, 1.2); } 55% { -ms-transform: scale(0.8, 0.8); transform: scale(0.8, 0.8); } 75% { -ms-transform: scale(0.7, 0.7); transform: scale(0.7, 0.7); } 100% { -ms-transform: scale(0.9, 0.9); transform: scale(0.9, 0.9); } } @-o-keyframes "launch_moon_ball_size" { 50% { -o-transform: scale(1.2, 1.2); transform: scale(1.2, 1.2); } 55% { -o-transform: scale(0.8, 0.8); transform: scale(0.8, 0.8); } 75% { -o-transform: scale(0.7, 0.7); transform: scale(0.7, 0.7); } 100% { -o-transform: scale(0.9, 0.9); transform: scale(0.9, 0.9); } } @keyframes "sky" { 50% { background-position: 318px 212px; } 100% { background-position: 635px 423px; } } @-moz-keyframes sky { 50% { background-position: 318px 212px; } 100% { background-position: 635px 423px; } } @-webkit-keyframes "sky" { 50% { background-position: 318px 212px; } 100% { background-position: 635px 423px; } } @-ms-keyframes "sky" { 50% { background-position: 318px 212px; } 100% { background-position: 635px 423px; } } @-o-keyframes "sky" { 50% { background-position: 318px 212px; } 100% { background-position: 635px 423px; } } |
OKie, vậy là xong rồi đó. Thử trải nghiệm xem sao nhé
You must log in to post a comment.