HTML5打地鼠是一款新穎好玩的游戲,不少網站都會嵌上這款游戲,給訪問者帶來不一樣的體驗。如果想要在自己的網站或博客中加入HTML5打地鼠游戲,可以通過下載代碼來實現。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>HTML5打地鼠游戲</title> <style type="text/css"> #container{ width: 640px; height: 480px; border: 1px solid #000; position: relative; } .hole{ width: 130px; height: 130px; position: absolute; top: 0; left: 0; overflow: hidden; } .mouse{ width: 130px; height: 130px; background-image: url(mouse.png); position: absolute; top: 0; left: 0; display: none; } </style> </head> <body> <div id="container"> <div class="hole"> <div class="mouse"></div> </div> <div class="hole"> <div class="mouse"></div> </div> <div class="hole"> <div class="mouse"></div> </div> </div> <script type="text/javascript"> window.onload = function(){ var holes = document.getElementsByClassName("hole"); for(var i=0;i<holes.length;i++){ holes[i].onclick = function(){ this.children[0].style.display = "block"; setTimeout(function(){ this.children[0].style.display = "none"; }.bind(this), 800); } } } </script> </body> </html>
以上是HTML5打地鼠游戲的代碼,可以將代碼復制到記事本或代碼編輯器中,然后保存為.html文件。將此文件上傳至自己的服務器或博客中,即可在網頁中添加HTML5打地鼠游戲。