<style>
.block{
width:100px;
height:100px;
display:inline-block;
position: absolute;
}
#b0{
top:0px;
left:100px;
background-color:#0CC;
}
#b1{
top:0px;
left:250px;
background-color:#FC0;
}
#b2{
top:0px;
left:400px;
background-color:#F66;
}
#b3{
top:0px;
left:550px;
background-color:#0CF;
}
#b4{
top:0px;
left:700px;
background-color:#6C3;
}
</style>
<script>
var play=false;
var objs=[];
var speeds=[];
var rspeeds = [];
var pos =[];
var angle = [];
var move=function()
{
if(!play) return;
for(var i=0; i<=4; i++)
{
if(pos[i]>700)
{
pos[i]=0;
objs[i].style.top="0px";
if(i!=0)
{
speeds[i]=Math.floor((Math.random()*10+1));
rspeeds[i]=Math.floor((Math.random()*10+1));
}
}
else
{
angle[i]=(angle[i]+ rspeeds[i] * 2)%360;
pos[i]=pos[i]+speeds[i];
objs[i].style.transform = "rotate("+ angle[i]+ "deg)";
objs[i].style.top=pos[i]+"px";
}
}
}
function mystart()
{
objs[0]=document.getElementById("b0");
speeds[0]=parseInt(myform.speed.value);
rspeeds[0]=parseInt(myform.rspeed.value);
pos[0]=0;
angle[0]=0;
for(var i=1; i<=4; i++)
{
var obj="b"+i;
objs[i]=document.getElementById(obj);
speeds[i]=Math.floor((Math.random()*10+1));
rspeeds[i]=Math.floor((Math.random()*10+1));
pos[i]=0;
angle[i]=0;
}
play = true;
}
function mystop()
{
play=false;
for(var i=0; i<=4; i++)
{
objs[i].style.top="0px";
}
}
setInterval(move, 10);
</script>
</head>
<body>
<div class="block" id="b0"></div>
<div class="block" id="b1"></div>
<div class="block" id="b2"></div>
<div class="block" id="b3"></div>
<div class="block" id="b4"></div>
<button onclick="mystart()"> start1 </button> <br />
<button onclick="mystop()" > stop1 </button> <br />
<form name="myform">
speed:
<input type="text" name="speed" value="5" size="5" /> <br />
rspeed:
<input type="text" name="rspeed" value="2" size="5" /> <br />
<button onclick="mystart()"> start2 </button> <br />
<button onclick="mystop()" > stop2 </button> <br />
</form>
</body>
</html>
答案 0 :(得分:0)
你的代码工作正常。我在Chrome和IE11都试过了。这是一个演示:
<style>
.block{
width:100px;
height:100px;
display:inline-block;
position: absolute;
}
#b0{
top:0px;
left:100px;
background-color:#0CC;
}
#b1{
top:0px;
left:250px;
background-color:#FC0;
}
#b2{
top:0px;
left:400px;
background-color:#F66;
}
#b3{
top:0px;
left:550px;
background-color:#0CF;
}
#b4{
top:0px;
left:700px;
background-color:#6C3;
}
</style>
<script>
var play=false;
var objs=[];
var speeds=[];
var rspeeds = [];
var pos =[];
var angle = [];
var move=function()
{
if(!play) return;
for(var i=0; i<=4; i++)
{
if(pos[i]>700)
{
pos[i]=0;
objs[i].style.top="0px";
if(i!=0)
{
speeds[i]=Math.floor((Math.random()*10+1));
rspeeds[i]=Math.floor((Math.random()*10+1));
}
}
else
{
angle[i]=(angle[i]+ rspeeds[i] * 2)%360;
pos[i]=pos[i]+speeds[i];
objs[i].style.transform = "rotate("+ angle[i]+ "deg)";
objs[i].style.top=pos[i]+"px";
}
}
}
function mystart()
{
objs[0]=document.getElementById("b0");
speeds[0]=parseInt(myform.speed.value);
rspeeds[0]=parseInt(myform.rspeed.value);
pos[0]=0;
angle[0]=0;
for(var i=1; i<=4; i++)
{
var obj="b"+i;
objs[i]=document.getElementById(obj);
speeds[i]=Math.floor((Math.random()*10+1));
rspeeds[i]=Math.floor((Math.random()*10+1));
pos[i]=0;
angle[i]=0;
}
play = true;
}
function mystop()
{
play=false;
for(var i=0; i<=4; i++)
{
objs[i].style.top="0px";
}
}
setInterval(move, 10);
</script>
</head>
<body>
<div class="block" id="b0"></div>
<div class="block" id="b1"></div>
<div class="block" id="b2"></div>
<div class="block" id="b3"></div>
<div class="block" id="b4"></div>
<button onclick="mystart()"> start1 </button> <br />
<button onclick="mystop()" > stop1 </button> <br />
<form name="myform">
speed:
<input type="text" name="speed" value="5" size="5" /> <br />
rspeed:
<input type="text" name="rspeed" value="2" size="5" /> <br />
<button onclick="mystart()"> start2 </button> <br />
<button onclick="mystop()" > stop2 </button> <br />
</form>
</body>
</html>