转盘抽奖活动代码

//抽奖转盘轮播图

(function () {
    $(function () {

        //抽奖转盘
        //控制速度的函数
        var moveStyle = {
            Linear: function (g, f, i, h) {
                return i * g / h + f
            },
            Quad: {
                easeIn: function (g, f, i, h) {
                    return i * (g /= h) * g + f
                },
                easeOut: function (g, f, i, h) {
                    return -i * (g /= h) * (g - 2) + f
                },
                easeInOut: function (g, f, i, h) {
                    if ((g /= h / 2) < 1) {
                        return i / 2 * g * g + f
                    }
                    return -i / 2 * ((--g) * (g - 2) - 1) + f
                }
            },
            Cubic: {
                easeIn: function (g, f, i, h) {
                    return i * (g /= h) * g * g + f
                },
                easeOut: function (g, f, i, h) {
                    return i * ((g = g / h - 1) * g * g + 1) + f
                },
                easeInOut: function (g, f, i, h) {
                    if ((g /= h / 2) < 1) {
                        return i / 2 * g * g * g + f
                    }
                    return i / 2 * ((g -= 2) * g * g + 2) + f
                }
            },
            Quart: {
                easeIn: function (g, f, i, h) {
                    return i * (g /= h) * g * g * g + f
                },
                easeOut: function (g, f, i, h) {
                    return -i * ((g = g / h - 1) * g * g * g - 1) + f
                },
                easeInOut: function (g, f, i, h) {
                    if ((g /= h / 2) < 1) {
                        return i / 2 * g * g * g * g + f
                    }
                    return -i / 2 * ((g -= 2) * g * g * g - 2) + f
                }
            },
            Quint: {
                easeIn: function (g, f, i, h) {
                    return i * (g /= h) * g * g * g * g + f
                },
                easeOut: function (g, f, i, h) {
                    return i * ((g = g / h - 1) * g * g * g * g + 1) + f
                },
                easeInOut: function (g, f, i, h) {
                    if ((g /= h / 2) < 1) {
                        return i / 2 * g * g * g * g * g + f
                    }
                    return i / 2 * ((g -= 2) * g * g * g * g + 2) + f
                }
            },
            Sine: {
                easeIn: function (g, f, i, h) {
                    return -i * Math.cos(g / h * (Math.PI / 2)) + i + f
                },
                easeOut: function (g, f, i, h) {
                    return i * Math.sin(g / h * (Math.PI / 2)) + f
                },
                easeInOut: function (g, f, i, h) {
                    return -i / 2 * (Math.cos(Math.PI * g / h) - 1) + f
                }
            },
            Expo: {
                easeIn: function (g, f, i, h) {
                    return (g == 0) ? f : i * Math.pow(2, 10 * (g / h - 1)) + f
                }, easeOut: function (g, f, i, h) {
                    return (g == h) ? f + i : i * (-Math.pow(2, -10 * g / h) + 1) + f
                }, easeInOut: function (g, f, i, h) {
                    if (g == 0) {
                        return f
                    }
                    if (g == h) {
                        return f + i
                    }
                    if ((g /= h / 2) < 1) {
                        return i / 2 * Math.pow(2, 10 * (g - 1)) + f
                    }
                    return i / 2 * (-Math.pow(2, -10 * --g) + 2) + f
                }
            },
            Circ: {
                easeIn: function (g, f, i, h) {
                    return -i * (Math.sqrt(1 - (g /= h) * g) - 1) + f
                }, easeOut: function (g, f, i, h) {
                    return i * Math.sqrt(1 - (g = g / h - 1) * g) + f
                }, easeInOut: function (g, f, i, h) {
                    if ((g /= h / 2) < 1) {
                        return -i / 2 * (Math.sqrt(1 - g * g) - 1) + f
                    }
                    return i / 2 * (Math.sqrt(1 - (g -= 2) * g) + 1) + f
                }
            },
            Elastic: {
                easeIn: function (h, f, l, k, g, j) {
                    if (h == 0) {
                        return f
                    }
                    if ((h /= k) == 1) {
                        return f + l
                    }
                    if (!j) {
                        j = k * 0.3
                    }
                    if (!g || g < Math.abs(l)) {
                        g = l;
                        var i = j / 4
                    } else {
                        var i = j / (2 * Math.PI) * Math.asin(l / g)
                    }
                    return -(g * Math.pow(2, 10 * (h -= 1)) * Math.sin((h * k - i) * (2 * Math.PI) / j)) + f
                }, easeOut: function (h, f, l, k, g, j) {
                    if (h == 0) {
                        return f
                    }
                    if ((h /= k) == 1) {
                        return f + l
                    }
                    if (!j) {
                        j = k * 0.3
                    }
                    if (!g || g < Math.abs(l)) {
                        g = l;
                        var i = j / 4
                    } else {
                        var i = j / (2 * Math.PI) * Math.asin(l / g)
                    }
                    return (g * Math.pow(2, -10 * h) * Math.sin((h * k - i) * (2 * Math.PI) / j) + l + f)
                }, easeInOut: function (h, f, l, k, g, j) {
                    if (h == 0) {
                        return f
                    }
                    if ((h /= k / 2) == 2) {
                        return f + l
                    }
                    if (!j) {
                        j = k * (0.3 * 1.5)
                    }
                    if (!g || g < Math.abs(l)) {
                        g = l;
                        var i = j / 4
                    } else {
                        var i = j / (2 * Math.PI) * Math.asin(l / g)
                    }
                    if (h < 1) {
                        return -0.5 * (g * Math.pow(2, 10 * (h -= 1)) * Math.sin((h * k - i) * (2 * Math.PI) / j)) + f
                    }
                    return g * Math.pow(2, -10 * (h -= 1)) * Math.sin((h * k - i) * (2 * Math.PI) / j) * 0.5 + l + f
                }
            },
            Back: {
                easeIn: function (g, f, j, i, h) {
                    if (h == undefined) {
                        h = 1.70158
                    }
                    return j * (g /= i) * g * ((h + 1) * g - h) + f
                }, easeOut: function (g, f, j, i, h) {
                    if (h == undefined) {
                        h = 1.70158
                    }
                    return j * ((g = g / i - 1) * g * ((h + 1) * g + h) + 1) + f
                }, easeInOut: function (g, f, j, i, h) {
                    if (h == undefined) {
                        h = 1.70158
                    }
                    if ((g /= i / 2) < 1) {
                        return j / 2 * (g * g * (((h *= (1.525)) + 1) * g - h)) + f
                    }
                    return j / 2 * ((g -= 2) * g * (((h *= (1.525)) + 1) * g + h) + 2) + f
                }
            },
            Bounce: {
                easeIn: function (g, f, i, h) {
                    return i - e.Bounce.easeOut(h - g, 0, i, h) + f
                }, easeOut: function (g, f, i, h) {
                    if ((g /= h) < (1 / 2.75)) {
                        return i * (7.5625 * g * g) + f
                    } else {
                        if (g < (2 / 2.75)) {
                            return i * (7.5625 * (g -= (1.5 / 2.75)) * g + 0.75) + f
                        } else {
                            if (g < (2.5 / 2.75)) {
                                return i * (7.5625 * (g -= (2.25 / 2.75)) * g + 0.9375) + f
                            } else {
                                return i * (7.5625 * (g -= (2.625 / 2.75)) * g + 0.984375) + f
                            }
                        }
                    }
                }, easeInOut: function (g, f, i, h) {
                    if (g < h / 2) {
                        return e.Bounce.easeIn(g * 2, 0, i, h) * 0.5 + f
                    } else {
                        return e.Bounce.easeOut(g * 2 - h, 0, i, h) * 0.5 + i * 0.5 + f
                    }
                }
            }
        };

        function dragonBoatFestival() {
            this.init()
        }

        $.extend(dragonBoatFestival.prototype, {
            init: function () {
                var self = this;

                this.curDeg = 0;
                this.awards = {
                    "1": {
                        idx: 1,
                        level: 4,
                        name: "20元手机话费"
                    },
                    "2": {
                        idx: 3,
                        level: 2,
                        name: "10元红包"
                    },
                    "3": {
                        idx: 5,
                        level: 1,
                        name: "三元提现券"
                    },
                    "4": {
                        idx: 7,
                        level: 3,
                        name: "10元手机话费"
                    },
                    "5": {
                        idx: 9,
                        level: 5,
                        name: "充电宝"
                    },
                    "6": [
                        {
                            idx: 0,
                            level: 10,
                            name: "谢谢参与"
                        },
                        {
                            idx: 2,
                            level: 10,
                            name: "谢谢参与"
                        },
                        {
                            idx: 4,
                            level: 10,
                            name: "谢谢参与"
                        },
                        {
                            idx: 6,
                            level: 10,
                            name: "谢谢参与"
                        },
                        {
                            idx: 8,
                            level: 10,
                            name: "谢谢参与"
                        }
                    ]
                };
                
                self.use();
            },

            use: function () {
               
                            self.Move(self.getAwardSector(5), function () {
                                //可写显示获奖弹框的回调函数
                            });  //level值  5为充电宝
            },
         
            isSupportTransform: function(k) {
                var m = new Array("transform", "MozTransform", "webkitTransform", "OTransform", "msTransform");
                var h = null;
                var p = k.get(0);
                var n = p.style;
                for (var j = 0, g = m.length; j < g; j++) {
                    if (m[j] in n) {
                        h = m[j];
                        break
                    }
                }
                return h
            },
            setTransform: function(q, t) {
                var self = this;
                var $btn = self.$btn;
                var p = true;
                var i = q[0];
                var j = null;
                var w = Math.PI / 180;
                var r = t * w;
                var h = Math.sin(r), g = Math.cos(r);
                /*var s = self.$pointer[0].getBoundingClientRect();

                 var n = (s.right - s.left) / 2, l = (s.bottom - s.top) / 2;*/
                var s ;
                var n = 160, l = 160;
                var m = 0, k = 0;
                j = self.isSupportTransform(q);
                p = !j ? true : false;
                if (!p) {
                    i.style[j] = "rotate(" + t + "deg)"
                } else {
                    if ( i.filters.length > 0 && i.filters.item(0).M11 ) {
                        i.filters.item(0).M11 = g;
                        i.filters.item(0).M12 = -h;
                        i.filters.item(0).M21 = h;
                        i.filters.item(0).M22 = g;
                    } else {
                        i.style.filter += "progid:DXImageTransform.Microsoft.Matrix(M11=" + g + ", M12=" + -h + ", M21=" + h + ", M22=" + g + ', sizingMethod="auto expand")';
                    }
                    s = i.getBoundingClientRect();
                    m = (s.right - s.left) / 2;
                    k = (s.bottom - s.top) / 2;

                    i.style.marginLeft = n - m + "px";
                    i.style.marginTop = l - k + "px";
                    $btn[0].style.marginLeft = m - n + "px";
                    $btn[0].style.marginTop = k - l+ "px"

                }
            },
            Move: function (p, calBack) {
                var self = this;
                var r = 0,
                    n = deg = self.curDeg,
                    m = 0,
                    l = 635;
                var k = p,
                    q = 10;
                m = 360 * q + k - n;
                var g = "Quint";
                var u = "easeInOut";

                clearTimeout(self.timderID);

                function j() {
                    if (r < l) {
                        r++;
                        deg = Math.ceil(moveStyle[g][u](r, n, m, l));
                        self.setTransform(self.$pointerCon, deg);

                        self.timderID = setTimeout(j, 10)
                    } else {
                        deg = m + n;
                        self.setTransform(self.$pointerCon, deg);
                        self.curDeg = deg % 360;

                        if ($.isFunction(calBack)) {
                            setTimeout(function () {
                                calBack(self)
                            }, 1000)
                        }
                    }
                }

                j();
            },
            //根据请求返回的level来决定最后指针指向
            getAwardSector: function (level) {
                var self = this;
                var g = -1;
                var j = 0;
                var i = Math.round(Math.random() * 10);

                $.each(self.awards, function (i, item) {
                    if ($.isArray(item)) {
                        var ranNum = Math.floor(Math.random() * 2);
                        if (item[ranNum].level == level) {
                            g = item[ranNum].idx;
                        }
                    } else {
                        if (item.level == level) {
                            g = item.idx
                        }
                    }

                });


                if (g == -1) {
                    var k = function (m) {
                        var n = true;
                        $.each(self.awards, function (i, item) {
                            if ($.isArray(item)) {
                                var ranNum = Math.floor(Math.random() * 2);
                                if (item[ranNum].level == level) {
                                    n = false;
                                }
                            } else {
                                if (item.level == level) {
                                    n = false;
                                }
                            }
                        });
                        return n;
                    };

                    do {
                        i = Math.round(Math.random() * 10) % 10
                    } while (!k(i));

                    g = self.awards["6"][Math.floor(Math.random() * 2)].idx;
                }

                j = g * 36;

                return j;
            }
        });

        new dragonBoatFestival()

    })
})(jQuery);

  

posted @ 2016-06-08 15:50  浣花水榭  阅读(350)  评论(0编辑  收藏  举报