很多公司到了年底都會(huì)做一些抽獎(jiǎng)活動(dòng)來(lái)刺激、吸引、粘住客戶,比如抽獎(jiǎng)轉(zhuǎn)盤活動(dòng)。
前幾天用一個(gè)jqueryRotate插件實(shí)現(xiàn)了轉(zhuǎn)盤的效果。比起那些很炫麗的flash是稍遜點(diǎn),但也基本實(shí)現(xiàn)了需求
效果圖:
實(shí)現(xiàn)這個(gè)其實(shí)蠻簡(jiǎn)單的,轉(zhuǎn)動(dòng)的效果用的jqueryRotate插件,所以只要判斷每個(gè)獎(jiǎng)薦對(duì)應(yīng)的角度,然后設(shè)置指針的轉(zhuǎn)動(dòng)角度就可以了。比如關(guān)鍵的是jqueryRotate這個(gè)插件的用法。
jqueryRotate的資料:
支持Internet Explorer 6.0+ 、Firefox 2.0 、Safari 3 、Opera 9 、Google Chrome,高級(jí)瀏覽器下使用Transform,低版本ie使用VML實(shí)現(xiàn)
google code地址:http://code.google.com/p/jqueryrotate/
調(diào)用和方法:
$(el).rotate({ angle:0, //起始角度 animateTo:180, //結(jié)束的角度 duration:500, //轉(zhuǎn)動(dòng)時(shí)間 callback:function(){}, //回調(diào)函數(shù) easing: $.easing.easeInOutExpo //定義運(yùn)動(dòng)的效果,需要引用jquery.easing.min.js的文件 })
$(el).rotate(45); //直接這樣子調(diào)用的話就是變換角度
$(el).getRotateAngle(); //返回對(duì)象當(dāng)前的角度
$(el).stopRotare(); //停止旋轉(zhuǎn)動(dòng)畫
另外可以更方便的通過(guò)調(diào)用$(el).rotateRight()和$(el).rotateLeft()來(lái)分別向右旋轉(zhuǎn)90度和向左旋轉(zhuǎn)90度。
很簡(jiǎn)單吧,各種example可以看這里:http://code.google.com/p/jqueryrotate/wiki/Examples
下面是用jqueryRotate實(shí)現(xiàn)的抽獎(jiǎng)轉(zhuǎn)盤頁(yè)面:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>轉(zhuǎn)盤</title> <style> *{padding:0;margin:0} body{ text-align:center } .ly-plate{ position:relative; width:509px; height:509px; margin: 50px auto; } .rotate-bg{ width:509px; height:509px; background:url(ly-plate.png); position:absolute; top:0; left:0 } .ly-plate div.lottery-star{ width:214px; height:214px; position:absolute; top:150px; left:147px; /*text-indent:-999em; overflow:hidden; background:url(rotate-static.png); -webkit-transform:rotate(0deg);*/ outline:none } .ly-plate div.lottery-star #lotteryBtn{ cursor: pointer; position: absolute; top:0; left:0; *left:-107px } </style> </head> <body> <div class="ly-plate"> <div class="rotate-bg"></div> <div class="lottery-star"><img src="http://www.gimoo.net/t/1902/rotate-static.png" id="lotteryBtn"></div> </div> </body> <script src="http://www.gimoo.net/t/1902/jquery-1.7.2.min.js"></script> <script src="http://www.gimoo.net/t/1902/jQueryRotate.2.2.js"></script> <script> $(function(){ var timeOut = function(){ //超時(shí)函數(shù) $("#lotteryBtn").rotate({ angle:0, duration: 10000, animateTo: 2160, //這里是設(shè)置請(qǐng)求超時(shí)后返回的角度,所以應(yīng)該還是回到最原始的位置,2160是因?yàn)槲乙屗D(zhuǎn)6圈,就是360*6得來(lái)的 callback:function(){ alert('網(wǎng)絡(luò)超時(shí)') } }); }; var rotateFunc = function(awards,angle,text){ //awards:獎(jiǎng)項(xiàng),angle:獎(jiǎng)項(xiàng)對(duì)應(yīng)的角度,text:提示文字 $('#lotteryBtn').stopRotate(); $("#lotteryBtn").rotate({ angle:0, duration: 5000, animateTo: angle+1440, //angle是圖片上各獎(jiǎng)項(xiàng)對(duì)應(yīng)的角度,1440是我要讓指針旋轉(zhuǎn)4圈。所以最后的結(jié)束的角度就是這樣子^^ callback:function(){ alert(text) } }); }; $("#lotteryBtn").rotate({ bind: { click: function(){ var time = [0,1]; time = time[Math.floor(Math.random()*time.length)]; if(time==0){ timeOut(); //網(wǎng)絡(luò)超時(shí) } if(time==1){ var data = [1,2,3,0]; //返回的數(shù)組 data = data[Math.floor(Math.random()*data.length)]; if(data==1){ rotateFunc(1,157,'恭喜您抽中的一等獎(jiǎng)') } if(data==2){ rotateFunc(2,247,'恭喜您抽中的二等獎(jiǎng)') } if(data==3){ rotateFunc(3,22,'恭喜您抽中的三等獎(jiǎng)') } if(data==0){ var angle = [67,112,202,292,337]; angle = angle[Math.floor(Math.random()*angle.length)] rotateFunc(0,angle,'很遺憾,這次您未抽中獎(jiǎng)') } } } } }); }) </script> </html>
這里的time跟data是要從后臺(tái)獲取的,但這里只是靜態(tài)頁(yè)面,所以我就利用了random隨機(jī)數(shù)來(lái)盡量模擬抽獎(jiǎng)的過(guò)程了。
time參數(shù)表示從后臺(tái)請(qǐng)求是否成功,0是請(qǐng)求超時(shí),1是請(qǐng)求成功(成功后再判斷返回的值是什么樣);
data就是請(qǐng)求返回的數(shù)據(jù),1,2,3表示一二三等獎(jiǎng),0是不中獎(jiǎng),根據(jù)返回的數(shù)據(jù),再去設(shè)置指針旋轉(zhuǎn)的角度。
因?yàn)檫@個(gè)圖片上的角度無(wú)法用公式計(jì)算出來(lái),所以只能這樣子計(jì)算出來(lái)后定死。
如果比較規(guī)則的話,應(yīng)該可以用公式計(jì)算。
其實(shí)難得不在前臺(tái),別人寫好的插件,我們拿來(lái)用就好,真正難的是在后臺(tái),定一個(gè)什么樣的規(guī)則,搞一個(gè)什么樣的算法,大家去思考去吧~~呵呵~~
點(diǎn)擊下載完整DEMO
以上就是本文的全部?jī)?nèi)容,希望能給大家一個(gè)參考,也希望大家多多支持綠夏網(wǎng)。