jquery轉盤抽獎功能實現_jquery
一、使用的素材
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>jQuery九宫格大转盘抽奖</title> <style> #lottery{width:570px;height:510px;margin:0px auto;border:4px solid #ba1809;} #lottery table{background-color:yellow;} #lottery table td{position:relative;width:190px;height:170px;text-align:center;color:#333;font-index:-999} #lottery table td img{display:block;width:190px;height:170px;} #lottery table td a{width:190px;height:170px;display:block;text-decoration:none;background:url(images/lottery1.jpg) no-repeat top center;} #lottery table td a:hover{background-image:url(images/lottery2.jpg);} #lottery table td.active .mask{display:block;} .mask{ width:100%; height:100%; position:absolute; left:0; top:0; background:url(images/mask.png) no-repeat; display:none; } </style> </head> <body class="keBody"> <!--效果html开始--> <p id="lottery"> <table border="0" cellpadding="0" cellspacing="0"> <tr> <td class="lottery-unit lottery-unit-0"><img src="images/gift0.jpg"><p class="mask"></p></td> <td class="lottery-unit lottery-unit-1"><img src="images/gift1.jpg"><p class="mask"></p></td> <td class="lottery-unit lottery-unit-2"><img src="images/gift2.jpg"><p class="mask"></p></td> </tr> <tr> <td class="lottery-unit lottery-unit-7"><img src="images/gift7.jpg"><p class="mask"></p></td> <td><a href="#"></a></td> <td class="lottery-unit lottery-unit-3"><img src="images/gift3.jpg"><p class="mask"></p></td> </tr> <tr> <td class="lottery-unit lottery-unit-6"><img src="images/gift6.jpg"><p class="mask"></p></td> <td class="lottery-unit lottery-unit-5"><img src="images/gift5.jpg"><p class="mask"></p></td> <td class="lottery-unit lottery-unit-4"><img src="images/gift4.jpg"><p class="mask"></p></td> </tr> </table> </p> <script src="http://code.jquery.com/jquery-latest.js?7.1.34"></script> <script type="text/javascript"> var lottery={ index:-1, //当前转动到哪个位置,起点位置 count:0, //总共有多少个位置 timer:0, //setTimeout的ID,用clearTimeout清除 speed:20, //初始转动速度 times:0, //转动次数 cycle:50, //转动基本次数:即至少需要转动多少次再进入抽奖环节 prize:-1, //中奖位置 init:function(id){ if ($("#"+id).find(".lottery-unit").length>0) { $lottery = $("#"+id); $units = $lottery.find(".lottery-unit"); this.obj = $lottery; this.count = $units.length; $lottery.find(".lottery-unit-"+this.index).addClass("active"); }; }, roll:function(){ var index = this.index; var count = this.count; var lottery = this.obj; $(lottery).find(".lottery-unit-"+index).removeClass("active"); index += 1; if (index>count-1) { index = 0; }; $(lottery).find(".lottery-unit-"+index).addClass("active"); this.index=index; return false; }, stop:function(index){ this.prize=index; return false; } }; function roll(){ lottery.times += 1; lottery.roll();//转动过程调用的是lottery的roll方法,这里是第一次调用初始化 if (lottery.times > lottery.cycle+10 && lottery.prize==lottery.index) { clearTimeout(lottery.timer); lottery.prize=-1; lottery.times=0; click=false; }else{ if (lottery.times<lottery.cycle) { lottery.speed -= 10; }else if(lottery.times==lottery.cycle) { var index = Math.random()*(lottery.count)|0; lottery.prize = index; }else{ if (lottery.times > lottery.cycle+10 && ((lottery.prize==0 && lottery.index==7) || lottery.prize==lottery.index+1)) { lottery.speed += 110; }else{ lottery.speed += 20; } } if (lottery.speed<40) { lottery.speed=40; }; //console.log(lottery.times+'^^^^^^'+lottery.speed+'^^^^^^^'+lottery.prize); lottery.timer = setTimeout(roll,lottery.speed);//循环调用 } return false; } var click=false; window.onload=function(){ lottery.init('lottery'); $("#lottery a").click(function(){ if (click) {//click控制一次抽奖过程中不能重复点击抽奖按钮,后面的点击不响应 return false; }else{ lottery.speed=100; roll(); //转圈过程不响应click事件,会将click置为false click=true; //一次抽奖完成后,设置click为true,可继续抽奖 return false; } }); }; </script> <!--效果html结束--> </body> </html>
.mask { width: 100%; height: 100%; position: absolute; left: 0; top: 0; background-color: rgba(252,211,4,0.5); display: none }
二、程式碼如下,重點在於js部分
效果如下:三、注意事項.mask開始如下,用的是rgba,但是IE8不相容,改為使用png圖片background:url(images/mask.png) no-repeat;希望大家可以從小編整理的這篇文章中有所收穫,對大家實現抽獎功能有所幫助。
熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Python和JavaScript開發者的薪資沒有絕對的高低,具體取決於技能和行業需求。 1.Python在數據科學和機器學習領域可能薪資更高。 2.JavaScript在前端和全棧開發中需求大,薪資也可觀。 3.影響因素包括經驗、地理位置、公司規模和特定技能。

JavaScript是現代Web開發的基石,它的主要功能包括事件驅動編程、動態內容生成和異步編程。 1)事件驅動編程允許網頁根據用戶操作動態變化。 2)動態內容生成使得頁面內容可以根據條件調整。 3)異步編程確保用戶界面不被阻塞。 JavaScript廣泛應用於網頁交互、單頁面應用和服務器端開發,極大地提升了用戶體驗和跨平台開發的靈活性。

如何在JavaScript中將具有相同ID的數組元素合併到一個對像中?在處理數據時,我們常常會遇到需要將具有相同ID�...

學習JavaScript不難,但有挑戰。 1)理解基礎概念如變量、數據類型、函數等。 2)掌握異步編程,通過事件循環實現。 3)使用DOM操作和Promise處理異步請求。 4)避免常見錯誤,使用調試技巧。 5)優化性能,遵循最佳實踐。

實現視差滾動和元素動畫效果的探討本文將探討如何實現類似資生堂官網(https://www.shiseido.co.jp/sb/wonderland/)中�...

探索前端中類似VSCode的面板拖拽調整功能的實現在前端開發中,如何實現類似於VSCode...

深入探討console.log輸出差異的根源本文將分析一段代碼中console.log函數輸出結果的差異,並解釋其背後的原因。 �...
