抽奖的demo(跑马灯效果)

1,431 阅读1分钟

效果:

代码:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
		*{
			padding: 0;
			margin: 0;
		}
		#uls{
			width: 500px;
			height: 500px;
			margin: 50px auto;
		}
		li{
			width: 150px;
			height: 150px;
			background: skyblue;
			float: left;
			list-style: none;
			margin:5px;
			color: #fff;
			text-align: center;
			line-height: 150px;
			font-size: 20px;
			font-weight: bold;
		}
		#uls li button{
			width: 50px;
			height: 30px;
		}
		.light{
			width: 150px;
			height: 150px;
			background: #666;
			color: #fff;
		}
	</style>
</head>
<body>
	<ul id='uls'>
		<li>1</li>
		<li>2</li>
		<li>3</li>
		<li>8</li>
		<li><button id='btn1'>开始</button> <button id='btn2'>停止</button></li>
		<li>4</li>
		<li>7</li>
		<li>6</li>
		<li>5</li>
	</ul>
	<script>
		var btn1=document.getElementById("btn1");
		var btn2=document.getElementById("btn2");
		var arr=[0,1,2,5,8,7,6,3];//按这个下标依次旋转
		var lis=document.getElementsByTagName("li");
		var num=0;  //定义初识下标
		btn1.onclick=function(){
			btn1.disabled=true;
			time=setInterval(function(){
				lis[arr[num]].className="";
				num++;
				if(num>7){
					num=0;
				}
				lis[arr[num]].className="light";
			},50)
		}
		btn2.onclick=function(){
			btn1.disabled=false;
			clearInterval(time);
		}
	</script>
</body>
</html>