转盘抽奖插件-jquery.lottery.js

插件说明:

用于转盘随机抽奖,现在设的是转盘的指针动,指针停留的位置需要自己设置,需要后端返回的数据类型格式为  一等奖:{code : 1 ,msg :'一等奖' }  失败  {code : 5XX,msg:' '}

可以把成功和失败返回的后台数据, 利用 success 和 error回调函数进行处理。效果:



关于如何设置指针停留的角度,打开控制台,选中指针,修改其

  1. transformrotate(67deg);



插件依赖:

jquery.easing.js    jQueryRotate.js


参数说明:

duration 

转动的时间 单位ms 默认为  duration: 5000

round

转动的圈数 360 * n 默认为   round:1440 

success

中奖后的回调 默认为 success:function(){}

error

出错后的回调  默认为 error:function(){}

setAngle 

设置指针停的位置   该参数必须自己根据实际开发情景配置

setAngel:{
 0:[70,114,201,289,335],    //未中奖代表的角度
 1:158, //一等奖的角度
 2:244, //二等奖
 3: 25  //三等奖
}

url 

后台数据来源的url 默认为 url:‘’

data

向后台传的数据 默认为 data : ‘’

调用:

$('#luckyDraw').lottery(options);


源码:

/*
抽奖插件
依赖后端数据类型{code:0} code代表几等奖 {code : 5XX}代表失败
需要配置的参数:setAngel:{
 0:[70,114,201,289,335],    //未中奖代表的角度
 1:158, //一等奖的角度
 2:244, //二等奖
 3: 25  //三等奖
}
调用 $('#luckyDraw').lottery(options);
依赖的文件 jquery.easing.js  jQueryRotate.js
 */


;(function($,window,document){
    var defaults = {
        //指针转动的时间 ms
        duration : 5000,
        //圈数 360* n
        round : 1440,
        //指针初始位置
        angel : 0,
        //中奖后的回调
        success: function (rs) {

        },
        //出错后的回调
        error : function(rs){

        },
        //角度及奖项设置
        setAngle: {
            0:[70,114,201,289,335],
            1:158,
            2:244,
            3: 25
        },
        //数据来源
        url:'',
        //发送的数据
        data:''
        },
        //缓存
        _that,
        //构造函数
        Plugin = function(element,options){
            this.element = element;
            this.options = $.extend({},defaults,options);
            this.init();
        };
    Plugin.prototype.init = function(element,options){
        _that = this;
        //绑定元素click
        $(this.element).rotate({
            bind:{
                click:function(){
                    sendMsg(_that.options.url,_that.options.data)
                }
            }
        });
    };
    //插件
    jQuery.fn.lottery = function(options){
        return this.each(function(){
            if(!$.data(this,'lottery')){
                $.data(this,'lottery',new Plugin(this,options));
            }
        })
    };
    //发送请求
    var sendMsg = function(url ,data){
        $.get(url,data,function(rs){
            var rs = window._rs = JSON.parse(rs);
            //成功后的回调
            rs.code > 500 ? _that.options.error(rs) :
                //失败后的回调
                rotateFunc(rs.code,_that.options.setAngle[rs.code],_that.options.success);
        });
        },
        //指针移动
        rotateFunc = function(awards, angel, callback){
            $(_that.element).stopRotate();
            $(_that.element).rotate({
                angel : 0,
                duration : _that.options.duration,
                callback : function(){
                    callback.call(null,window._rs);
                }
            })
        };

})(jQuery,window,document,undefined);

DEMO



  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值