jQuery Lottery(抽獎插件)是一款基于jQuery框架的抽獎工具,其具有操作簡單、適用范圍廣泛等優(yōu)點,已經(jīng)獲得了許多用戶的歡迎和認(rèn)可。
<script> $(function() { var lottery = { index: -1, //當(dāng)前轉(zhuǎn)動到哪個位置,起點位置 count: 0, //總共有多少個位置 timer: 0, //setTimeout的ID,用clearTimeout清除 speed: 20, //初始轉(zhuǎn)動速度 times: 0, //轉(zhuǎn)動次數(shù) cycle: 50, //轉(zhuǎn)動基本次數(shù):即至少需要轉(zhuǎn)動多少次再進(jìn)入抽獎環(huán)節(jié) 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.lottery-unit-" + this.index).addClass("active"); } }, roll: function() { var index = this.index; var count = this.count; var lottery = this.obj; $(lottery).find(".lottery-unit.lottery-unit-" + index).removeClass("active"); index += 1; if (index >count - 1) { index = 0; } $(lottery).find(".lottery-unit.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(); if (lottery.times >lottery.cycle + 10 && lottery.index == lottery.prize) { 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; } lottery.timer = setTimeout(roll, lottery.speed); } return false; } var click = false; lottery.init('lottery'); $(".draw-btn").click(function() { if (click) { return false; } else { lottery.speed = 100; roll(); click = true; return false; } }); }); </script>
以上為jQuery Lottery的代碼,其實現(xiàn)原理是根據(jù)每個獎品格子的編號,通過不斷地更改樣式來模擬抽獎的過程,最終確定中獎號碼,同時也可以通過修改代碼中的參數(shù)來控制抽獎的速度、轉(zhuǎn)動次數(shù)等參數(shù),從而實現(xiàn)不同的抽獎體驗,使其變得更加有趣和刺激。
上一篇dockerp詳解
下一篇織夢404的css文件放