码迷,mamicode.com
首页 > 其他好文 > 详细

定时器简单案例-2

时间:2018-09-23 19:11:40      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:doctype   content   inf   定时   png   display   doc   div   mil   

抽奖案例

1.页面结构,HTML文件

<!DOCTYPE htnl>
<html>
<head>
      <meat charset="utf-8">
      <title>抽奖</title>
  <link el="stylesheet" href="./style.css">
</head>
<body>
   <button id="start">开始点名</button>
   <button id="end">结束点名</button>
   <div id="name">张三</div>
   <script src="./index.js"></script>
</body>
</html>

2.css样式文件:      

 

             *{
                 padding: 0;
                 margin: 0;
                 list-style: none;
             }
             body{
                 text-align: center;
                 background-color: skyblue;
             }
             #start,#end{
                 margin-top: 50px;
             }
             #name{
                 width: 400px;
                 height: 400px;
                 border: 4px solid greenyellow;
                 box-shadow: 0 0 10px blue;
                 background-color: #000;
                 margin: 50px auto;
                 color: yellow;
                 font-size: 100px;
                 font-family: "PMingLiU";
                 display: flex;
                 justify-content: center;
                 align-items: center;
             }

 

 

 

3.js文件

             //定义变量接收花名册数组
             var nameArray=["汤浩","","赵吉周","王文照","姚凯凯","刘博","张坤","雷胜洪","马云","张娇","王晓倩","杨朝霞","","任一凡","李烨","马荣华","向秋月","王小龙","程亚浩","李连杰","洋洋","呼延觉罗.修","吴雅丽","齐鹏博","澹台改娜","梁颖","王艳"];
             var timer=null;
             $("name").innerText=nameArray[0];
             //监听鼠标点击事件
             $("start").onclick=function(){
             //清除定时器
             clearInterval(timer);
             //开启定时器
             timer=setInterval(function(){
                 //随机数
                 var s_index=parseInt(Math.random()*(nameArray.length));
                 var s_name=nameArray[s_index];
                 //console.log(s_name);
                 $("name").innerText=s_name;
                 
             },30);
             }
             end.onclick=function(){
                //清除定时器
                clearInterval(timer);
             }
             function $(id){
                 return typeof id==="string"?document.getElementById(id):null;
             }

4.页面效果:

技术分享图片

 

定时器简单案例-2

标签:doctype   content   inf   定时   png   display   doc   div   mil   

原文地址:https://www.cnblogs.com/zhang-jiao/p/9693343.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!