1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>jquery案例之抽奖</title> <script type="text/javascript" src="../js/jquery-3.3.1.min.js"></script> <script language='javascript' type='text/javascript'>
/** * 分析: * 1. 给开始按钮绑定单击事件 * 1.1 定义循环定时器 * 1.2 切换小相框的src属性 * * 定义数组,存放图片资源路径 * * 生成随机数。数组索引 * 2. 给结束按钮绑定单击事件 * 2.1 停止定时器 * 2.2 给大相框设置src属性 */ var imgs = ["../img/man00.jpg)", "../img/man01.jpg)", "../img/man02.jpg)", "../img/man03.jpg)", "../img/man04.jpg)", "../img/man05.jpg)", "../img/man06.jpg)", ]; var startId; var index; $(function () { $("#startID").prop("disabled",false); $("#stopID").prop("disabled",true); $("#startID").click(function () { startId = setInterval(function () { $("#startID").prop("disabled",true); $("#stopID").prop("disabled",false); index = Math.floor(Math.random() * 7); $("#img1ID").prop("src",imgs[index]); },20); }); $("#stopID").click(function () { $("#startID").prop("disabled",false); $("#stopID").prop("disabled",true); clearInterval(startId); $("#img2ID").prop("src",imgs[index]).hide(); $("#img2ID").show(1000); }); }); </script> </head> <body> <div style="border-style:dotted;width:160px;height:100px"> <img id="img1ID" src="../img/man00.jpg)" style="width:160px;height:100px"/> </div> <div style="border-style:double;width:800px;height:500px;position:absolute;left:500px;top:10px"> <img id="img2ID" src="../img/man00.jpg)" width="800px" height="500px"/> </div> <input id="startID" type="button" value="点击开始" style="width:150px;height:150px;font-size:22px"> <input id="stopID" type="button" value="点击停止" style="width:150px;height:150px;font-size:22px"> </body> </html>
|