HTML5技術(shù)的出現(xiàn),讓前端開發(fā)的世界變得更加炫酷!以下是一些讓人驚嘆的HTML5代碼:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>3D盒子</title> <style> .container { perspective: 800px; } .box { width: 200px; height: 200px; position: relative; transform-style: preserve-3d; transition: transform 1s; } .box:hover { transform: rotateY(45deg); } .front, .back { position: absolute; width: 100%; height: 100%; background-color: #f00; backface-visibility: hidden; } .front { transform: translateZ(100px); } .back { transform: rotateY(180deg) translateZ(100px); } </style> </head> <body> <div class="container"> <div class="box"> <div class="front"></div> <div class="back"></div> </div> </div> </body> </html>
這段代碼實(shí)現(xiàn)了一個(gè)3D盒子的效果,當(dāng)鼠標(biāo)懸停在盒子上時(shí),會(huì)發(fā)生旋轉(zhuǎn),讓人感覺非常炫酷。
另一個(gè)值得一提的 HTML5 動(dòng)畫代碼:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>自由落體動(dòng)畫</title> <style> #square { width: 50px; height: 50px; background-color: #f00; position: absolute; left: 0; top: 0; } </style> </head> <body> <div id="square"></div> <script> let velocity = 0; let position = 0; const gravity = 9.8; const interval = setInterval(function() { position += velocity / 60; velocity += gravity / 60; if (position >= window.innerHeight - 50) { velocity = -velocity / 2; } document.querySelector('#square').style.top = position + 'px'; }, 16.7); </script> </body> </html>
這段代碼實(shí)現(xiàn)了自由落體動(dòng)畫效果,通過控制小正方形的位置和速度實(shí)現(xiàn)類似物理運(yùn)動(dòng)的效果。