一、简化版的轮播图
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style> img{ width: 100px; height: 100px; } </style>
</head> <body> <img src="img/1.PNG" />
<script> //只要有需求: 每隔一段事件就要作什么事 就需要联想到使用定时器
var img = document.getElementsByTagName("img")[0]; //实现图片的替换 但是图片的文件名可能不是有规律. //所以可以将图片的所有文件名路径名 存到数组中. 数组是有规律的. var src_1 = ["img/1.PNG","img/2.PNG","img/3.PNG","img/4.PNG"];
var index = 1; setInterval(function(){
img.setAttribute("src",src_1[index]); index++; if(index==4){ index = 0; } },1000);
</script>
</body> </html> |
二、赛马游戏
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style> table{ width: 1200px; height: 400px; }
img{ width: 100px; }
.begin{ width: 100px; } </style> </head> <body> <div align="center"> <h3>游戏规则:2分一次。赢了+5分。 中途主持人会随机暂停。选手可以换马。如果换马额外支付1分。</h3>
<table style="border: 1px solid black;" cellpadding="0" cellspacing="0" border="1px"> <tr> <td class="begin"><img src="img/1.gif" /> </td> <td>赛道~</td> </tr> <tr> <td class="begin"><img src="img/1.gif" /> </td> <td>赛道~</td> </tr> <tr> <td class="begin"><img src="img/1.gif" /> </td> <td>赛道~</td> </tr> <tr> <td class="begin"><img src="img/1.gif" /> </td> <td>赛道~</td> </tr> </table>
<button id="btn">开始比赛</button>
</div>
<script> var dsq ; //让图片由左边往右 开始运动 var img0 = document.getElementsByTagName("img")[0]; var img1 = document.getElementsByTagName("img")[1]; var img2 = document.getElementsByTagName("img")[2]; var img3 = document.getElementsByTagName("img")[3];
var left_index0 = 0; var left_index1 = 0; var left_index2 = 0; var left_index3 = 0;
document.getElementById("btn").onclick = function(){ if(document.getElementById("btn").innerText =="开始比赛"){ document.getElementById("btn").innerText ="暂停比赛"; dsq = setInterval(saima,10); }else{ document.getElementById("btn").innerText ="开始比赛"; clearInterval(dsq); } }
function saima(){ let sjs0 = Math.floor(Math.random()*10); let sjs1 = Math.floor(Math.random()*10); let sjs2 = Math.floor(Math.random()*10); let sjs3 = Math.floor(Math.random()*10); left_index0 = left_index0 + sjs0; left_index1 = left_index1 + sjs1; left_index2 = left_index2 + sjs2; left_index3 = left_index3 + sjs3;
img0.style.marginLeft = left_index0 + "px"; img1.style.marginLeft = left_index1 + "px"; img2.style.marginLeft = left_index2 + "px"; img3.style.marginLeft = left_index3 + "px";
if (left_index0>=1000) { alert("第1匹马赢了"); clearInterval(dsq); location.reload(); } if (left_index1>=1000) { alert("第2匹马赢了"); clearInterval(dsq); location.reload(); } if (left_index2>=1000) { alert("第3匹马赢了"); clearInterval(dsq); location.reload(); } if (left_index3>=1000) { alert("第4匹马赢了"); clearInterval(dsq); location.reload(); } } </script> </body> </html> |
三、图片的移动
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style> img{ /* margin-left: 300px; */ } </style>
</head> <body> <img src="img/1.gif" />
<script> var img = document.getElementsByTagName("img")[0]; var left_index = 0;
setInterval(function(){ left_index++; img.style.marginLeft = left_index + "px"; },10);
</script> </body> </html> |