本篇文章主要介绍了原生javascript实现九宫格抽奖效果代码。具有很好的参考价值。下面跟着小编一起来看下吧
效果图:

代码如下:
html>
<meta>
<meta>
<title></title>
<style>
*{margin:0;padding:0;}
#container{width:310px;height:310px;margin:30px auto;}
#ul1{width:310px;height:310px;list-style:none;}
#ul1 li,#ul1 a{width:100px;height:100px;border:1px solid #565656;float:left;text-align:center;line-height:100px;}
#ul1 a:hover{cursor:pointer;color:orange;font-size:18px;}
#ul1 .active{background:red;color:#fff;}
#pp{line-height:32px;color:#9a9a9a;text-align:center;}
</style><p>
</p>
- 一等奖
- 二等奖
- 三等奖
- 四等奖
开始
- 五等奖
- 六等奖
- 七等奖
- 八等奖
<script>
var container = document.getElementById('container'),
li = container.getElementsByTagName('li'),
aa = container.getElementsByTagName('a')[0],
pp = document.getElementById('pp'),
timer = null;
function start(){
var i = 0;
var num = Math.floor(Math.random() * li.length) + 20;
if(i<num){
timer = setInterval(function(){
for(var j=0;j<li.length;j++){
li[j].className = '';
}
li[i%li.length].className = 'active';
i++;
if(i === num){
clearInterval(timer);
if(num%li.length === 0){
pp.innerHTML += "恭喜您中了:8 等奖" + '<br/>';
}else{
pp.innerHTML += "恭喜您中了:"+ parseInt(num%li.length) + " 等奖"+ '<br/>';
}
}
},130);
}
}
aa.onclick = function(){
start();
}
</script>
Java免费学习笔记:立即使用
解锁 Java 大师之旅:从入门到精通的终极指南