jquery 实现转盘抽奖--转动的效果用的jqueryRotate插件
实现这个其实蛮简单的,转动的效果用的jqueryRotate插件,所以只要判断每个奖荐对应的角度,然后设置指针的转动角度就可以了。比如关键的是jqueryRotate这个插件的用法。
jqueryRotate的资料:
支持Internet Explorer 6.0+ 、Firefox 2.0 、Safari 3 、Opera 9 、Google Chrome,高级浏览器下使用Transform,低版本ie使用VML实现
google code地址:http://code.google.com/p/jqueryrotate/
调用和方法:
$(el).rotate({ angle:0, //起始角度 animateTo:180, //结束的角度 duration:500, //转动时间 callback:function(){}, //回调函数 easing: $.easing.easeInOutExpo //定义运动的效果,需要引用jquery.easing.min.js的文件 }) $(el).rotate(45); //直接这样子调用的话就是变换角度 $(el).getRotateAngle(); //返回对象当前的角度 $(el).stopRotare(); //停止旋转动画
另外可以更方便的通过调用$(el).rotateRight()和$(el).rotateLeft()来分别向右旋转90度和向左旋转90度。
很简单吧,各种example可以看这里:http://code.google.com/p/jqueryrotate/wiki/Examples
下面是用jqueryRotate实现的抽奖转盘页面:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>转盘</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="rotate-static.png" id="lotteryBtn"></div> </div> </body> <script src="jquery-1.7.2.min.js"></script> <script src="jQueryRotate.2.2.js"></script> <script> $(function(){ var timeOut = function(){ //超时函数 $("#lotteryBtn").rotate({ angle:0, duration: 10000, animateTo: 2160, //这里是设置请求超时后返回的角度,所以应该还是回到最原始的位置,2160是因为我要让它转6圈,就是360*6得来的 callback:function(){ alert('网络超时') } }); }; var rotateFunc = function(awards,angle,text){ //awards:奖项,angle:奖项对应的角度,text:提示文字 $('#lotteryBtn').stopRotate(); $("#lotteryBtn").rotate({ angle:0, duration: 5000, animateTo: angle+1440, //angle是图片上各奖项对应的角度,1440是我要让指针旋转4圈。所以最后的结束的角度就是这样子^^ 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(); //网络超时 } if(time==1){ var data = [1,2,3,0]; //返回的数组 data = data[Math.floor(Math.random()*data.length)]; if(data==1){ rotateFunc(1,157,'恭喜您抽中的一等奖') } if(data==2){ rotateFunc(2,247,'恭喜您抽中的二等奖') } if(data==3){ rotateFunc(3,22,'恭喜您抽中的三等奖') } if(data==0){ var angle = [67,112,202,292,337]; angle = angle[Math.floor(Math.random()*angle.length)] rotateFunc(0,angle,'很遗憾,这次您未抽中奖') } } } } }); }) </script> </html>
具体实现:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Bootstrap的HTML标准模板</title> <!-- Bootstrap --> <link href="../bootstrap-3.3.5-dist/css/bootstrap.min.css" rel="stylesheet"> <!-- 以下两个插件用于在IE8以及以下版本浏览器支持HTML5元素和媒体查询,如果不需要用可以移除 --> <!--[if lt IE 9]> <script src="../bootstrap-3.3.5-dist/js/html5shiv.js"></script> <script src="../bootstrap-3.3.5-dist/js/respond.min.js"></script> <![endif]--> <style type="text/css"> .turntable-bg{ background-image: url("images/turntable-bg.jpg"); background-repeat: no-repeat; position: relative; width: 650px; height: 600px; } .turntable-bg .pointer{ width:174px; height:228px; position: absolute; left: 50%; top: 50%; margin-left: -67px; margin-top: -144px;%; z-index: 8; } .turntable-bg .rotate{ position: absolute; left: 116px; top: 60px; } </style> </head> <body> <div class="container"> <div class="turntable-bg "> <div class="pointer"><img src="images/pointer.png" alt="pointer"/></div> <div class="rotate" ><img id="rotate" src="images/turntable.png" alt="turntable"/></div> </div> </div> <!-- 如果要使用Bootstrap的js插件,必须先调入jQuery --> <script src="../bootstrap-3.3.5-dist/js/jquery-1.11.3.js"></script> <!-- 包括所有bootstrap的js插件或者可以根据需要使用的js插件调用 --> <script src="../bootstrap-3.3.5-dist/js/bootstrap.min.js"></script> <script type="text/javascript" src="js/awardRotate.js"></script> <script type="text/javascript"> $(function (){ var rotateTimeOut = function () { $('#rotate').rotate({ angle: 0, animateTo: 2160, duration: 8000, callback: function () { alert('网络超时,请检查您的网络设置!'); } }); }; var bRotate = false; var rotateFn = function (awards, angles, txt){ bRotate = !bRotate; $('#rotate').stopRotate(); $('#rotate').rotate({ angle:0, animateTo:angles+1800, duration:8000, callback:function (){ alert(txt); bRotate = !bRotate; } }) }; $('.pointer').click(function (){ if(bRotate)return; var item = rnd(0,7); switch (item) { case 0: //var angle = [26, 88, 137, 185, 235, 287, 337]; rotateFn(0, 337, '未中奖'); break; case 1: //var angle = [88, 137, 185, 235, 287]; rotateFn(1, 26, '免单4999元'); break; case 2: //var angle = [137, 185, 235, 287]; rotateFn(2, 88, '免单50元'); break; case 3: //var angle = [137, 185, 235, 287]; rotateFn(3, 137, '免单10元'); break; case 4: //var angle = [185, 235, 287]; rotateFn(4, 185, '免单5元'); break; case 5: //var angle = [185, 235, 287]; rotateFn(5, 185, '免单5元'); break; case 6: //var angle = [235, 287]; rotateFn(6, 235, '免分期服务费'); break; case 7: //var angle = [287]; rotateFn(7, 287, '提高白条额度'); break; } console.log(item); }); }); function rnd(n, m){ return Math.floor(Math.random()*(m-n+1)+n) } </script> </body> </html>
//下面是转盘代码
$(function(){
$(".draw-start").click(function(){
lottery();
});
});
function lottery(){
$.ajax({
type: 'POST',
url: 'data.php',
dataType: 'json',
cache: false,
error: function(){
alert('出错了!数据连接出错');
return false;
},
success:function(json){
$(".draw-start").unbind('click').css("cursor","default");
var a = json.angle;
var p = json.prize;
$(".draw-start").rotate({
duration:3000, //转动时间
angle: 0, //默认角度
animateTo:1800+a, //转动角度
easing: $.easing.easeOutSine,
callback: function(){
alert('恭喜你,抽中'+'"'+p+'"!');
$(".draw-start").rotate({angle:0}); $(".draw-start").click(function(){ lottery(); }).css("cursor","pointer");
}
});
}
});
}