一個有關抽獎的專題項目,抽獎專題項目
2015春節來了之後,就開始投入到忙碌的工作中,最近做了一個有關抽獎專題的項目,第一次整抽獎的相關前端方面的需求。
當使用者點擊抽獎的時候,我們首先需要判斷使用者是否登入,如果沒有登入,提示使用者登入,然後再進行抽獎;抽獎的時候,我們需要判斷使用者是否有抽獎的許可權,如果抽獎次數已用完或者使用者沒抽獎的許可權,需要對使用者提示;當點擊抽獎,使用者選中了虛擬獎品時候,我們需要根據彈窗提示,讓使用者進入後台看相關的獎品;如果使用者選中了實物,我們需要提示使用者輸入相關的聯絡地址,提交後台等,
抽獎功能的實現主要引用了jQuery 的rotate外掛程式,實現轉盤旋轉功能。
主要的相關的js
var $window = $(window), prizeBox =$('div.prizeBox'), startTxt = $('.startTxt',prizeBox), rotate1 = $("#rotate",prizeBox), result_list = ['Ipad', '跑步機', '馬克杯', '相機', '豆漿機', '金幣50個','小米手機', '商務記事本']; startTxt.on("click", function() { //給轉盤按鈕的文字點擊時候觸發按鈕的點擊事件 rotate1.trigger("click"); });
rotate1.on('click', function(e) { e.preventDefault(); e.stopPropagation(); var login = $('a.login'), logout = $('a.logout'), url = startTxt.data('url'), //擷取發送請假地址 _html = $('script.diaf').html(); //擷取幾個提示彈窗的html $.get(url, function(resp) { var _data = $.parseJSON(resp), t = _data.t || ''; if(t == -1) { //使用者未登入時候,登入彈窗 dialog.open(_html); $('div.diaLogin').removeClass('disnone').siblings('div').hide(); } else if(t == -2) { //使用者抽獎次數用完或者沒抽獎許可權提示彈窗 dialog.open(_html); $('div.noPrize').removeClass('disnone').siblings('div').hide(); } else if(_data.p) { //轉盤旋轉 var _rotate = 45*_data.p - 10 - Math.round(Math.random()*25); rotate1.rotate({ duration :5000, angle : 0, animateTo : 3600 + _rotate,//Math.round(Math.random()*360)+360, callback : function() { dialog.open(_html); if(t == 2){ //獲得虛擬獎品時候 $('#hasPrize1').removeClass('disnone').siblings('div').hide(); $('#prizeTxt1').text(result_list[_data.p-1]); } else if(t==1){ //獲得實物獎品 var hasPrize2 = $('#hasPrize2') hasPrize2.removeClass('disnone').siblings('div').hide(); $('#prizeTxt2').text(result_list[_data.p-1]); //擷取的獎品名字 var btn = hasPrize2.find('.diaLoginBtn3'); btn.on('click',function(e){ //添加相應的收穫地址資訊彈窗 e.preventDefault(); var diaFormBox = $('.diaFormBox '); diaFormBox.removeClass('disnone').show().siblings('div').hide(); $('#orderid').val(_data.id); var area3 = new Area({ target: '.area3', itemName: ['請選擇省份', '請選擇城市', '請選擇縣/區'], items: ['province', 'city', 'area'], callback: function(field, allFields) { field.trigger('validate'); } }); try{ area3.fields[0].name = 'provinceid'; area3.fields[1].name = 'cityid'; area3.fields[2].name = 'areaid'; } catch (e){ } $("#form").validator({ stopOnError: true, focusCleanup: true, timely: 1, showOk:false, // 密碼驗證 fields: { 'tel': { rule: 'required', tip: '', ok: '', msg: { required: '聯絡電話不可為空' } }, 'address':{ rule: 'required;length[2~30]', msg: { required: '地址不可為空', length:'請填寫2-30漢字或者字元' } }, 'contact':{ rule: 'required;length[2~20]', msg: { required: '連絡人不可為空', lenght:'請填寫2-20漢字或者字元' } }, 'areaid':{ rule: 'required', ok:'', msg: { required: '請選擇地區 ' } } }, // 驗證完畢 valid: function(form) { var _form = $(form); var _data = _form.serializeArray(); var _url = _form.attr('action') $.post(_url, _data, function(res) { var _res = $.parseJSON(res); if(_res.t == 1) { $('#hasPrize3').removeClass('disnone').show().siblings('div').hide(); } }) } }); }) } else{ alert('網路異常,請稍後再試!'); } } }); } });});
/*
*下面功能主要是動態顯示已獲得獎品的名單
*/
var prizeNCon = (‘div.prizeNCon′,prizeBox),ownlistul=(‘ul’, prizeNCon);
prizeNCon.on(‘mouseleave’, function() {
var timer = setInterval(function() {
ownlist_ul.animate({
marginTop: “-30px”
}, 500, function() {
ownlist_ul.append(ownlist_ul.find(‘li:first’));
ownlist_ul.css(‘marginTop’,0);
});
}, 2500);
ownlist_ul.data(‘timer’, timer);
}).mouseleave();
ownlist_ul.on(‘mouseenter’, function() {
var timer = ownlist_ul.data(‘timer’);
timer && clearInterval(timer);
});
當壓縮發布代碼的時候,在ie中測試的發現了一些相容性問題,主要是判斷ie版本js,跟rotate不相容,解決辦法是用
var IE = (function(){
var match = /(msie) ([\w.]+)/i.exec(navigator.userAgent);
return match&&match[1] ? parseInt(match[2]):0;
})()
把原來rotate外掛程式裡面的內容換成了這段代碼,ie就相容可用了