<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>抽奖</title>
<style>
*{margin:0;padding:0;}
#title{
width:200px;height:50px;
margin:100px auto 0;
text-align:center;
font-size:24px;
font-weight:bold;
color:red;
}
#btn{
width:200px;height:20px;
margin:10px auto 0;
text-align:center;
}
#btn input{
width:50px;height:30px;
background-color:grey;
font-size:16px;
margin-right:10px;
}
</style>
<script>
var data=['爱疯','苹果','女朋友','游戏机']
window.onload=function(){
var timer;
var flag=0;
var start=document.getElementById('start');
var stop=document.getElementById('stop');
var title=document.getElementById('title');
//鼠标点击
start.onclick=function(){
start.style.background='#234';
play();
//设置flag的变化可以键盘鼠标配合使用,更加人性化
flag=1;
}
stop.onclick=function(){
start.style.background="#036";
if(title.innerHTML=='爱疯'){
clearInterval(timer);}
else{
play();}
//clearInterval(timer);
flag=0;
}
function play(){
clearInterval(timer);
timer=setInterval(function(){
var title=document.getElementById('title');
var num=Math.floor(Math.random()*data.length);
title.innerHTML=data[num];
},30)
}
//点击回车键
document.onkeyup=function(e){
e=event||window.event;
if(e.keyCode==13){
if(flag==0){
start.style.background='#234';
play();
flag=1;}
else{
start.style.background="#036";
if(title.innerHTML=='爱疯'){
clearInterval(timer);
flag=0;}
else{
play();}
//clearInterval(timer);
}
}
}
}
</script>
</head>
<body>
<div id="title">
开始抽奖
</div>
<div id="btn">
<input type="button" value="开始" id="start">
<input type="button" value="停止" id="stop">
</div>
</body>
</html>