直接粘贴代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
<meta name="format-detection" content="telephone=no">
<title>幸运大转盘</title>
<link rel="stylesheet" type="text/css" href="css/rotate.css" />
<style rel="stylesheet" type="text/css">
html {
font-family: "Helvetica Neue", Helvetica, sans-serif;
font-size: 20px;
}
body {
max-width: 768px;
margin: 0 auto;
}
* {
padding: 0;
margin: 0;
text-decoration: none;
list-style: none;
}
/*转盘样式*/
.box {
position: relative;
border-radius: 50%;
overflow: hidden;
width: 95%;
margin: 0 auto;
background: url('http://adimg1.yokead.com/5A851DC62ABDA507D0C1A12158D77B48');
background-size: 100% 100%;
background-repeat: no-repeat;
}
.lottery {
width: 86%;
margin: 0 auto;
margin-top: 7.5%;
border-radius: 50%;
overflow: hidden;
position: relative;
background-color: #330000;
box-shadow: 0 0 15px #000;
}
.block {
box-shadow: 0 0 1px #000 inset;
position: absolute;
width: 50%;
height: 6.8rem;
transform-origin: right bottom 0;
-webkit-transform-origin: right bottom 0;
}
.block-red {
background: -webkit-linear-gradient(left top, #b0002a, #b00029, #ef0039);
background: -moz-linear-gradient(left top, #b0002a, #b00029, #ef0039);
background: -ms-linear-gradient(left top, #b0002a, #b00029, #ef0039);
background: -o-linear-gradient(left top, #b0002a, #b00029, #ef0039);
background: linear-gradient(to right bottom, #b0002a, #b00029, #ef0039);
}
.block-yellow {
background: -webkit-linear-gradient(left top, #fdf5e6, #f9d306, #fdf5e6);
background: -moz-linear-gradient(left top, #fdf5e6, #f9d306, #fdf5e6);
background: -ms-linear-gradient(left top, #fdf5e6, #f9d306, #fdf5e6);
background: -o-linear-gradient(left top, #fdf5e6, #f9d306, #fdf5e6);
background: linear-gradient(to right bottom, #fdf5e6, #f9d306, #fdf5e6);
}
.block-orange {
background: -webkit-linear-gradient(left top, #fb9b75, #f86c06, #fb9b75);
background: -moz-linear-gradient(left top, #fb9b75, #f86c06, #fb9b75);
background: -ms-linear-gradient(left top, #fb9b75, #f86c06, #fb9b75);
background: -o-linear-gradient(left top, #fb9b75, #f86c06, #fb9b75);
background: linear-gradient(to right bottom, #fb9b75, #f86c06, #fb9b75);
}
.content {
width: 100%;
height: 100%;
position: absolute;
right: 0;
bottom: 0;
text-align: center;
}
.content .none {
margin: 0 auto;
text-align: center;
font-size: 0.9rem;
padding-bottom: 0.4rem;
}
.content img {
width: 2.5rem;
height: 2.5rem;
display: inline-block;
}
.btn {
width: 30%;
height: 30%;
position: absolute;
cursor: pointer;
left: 35%;
top: 35%;
background-size: 100% 100%;
}
.alert {
position: absolute;
width: 100%;
text-align: center;
top: 30%;
left: 0;
display: none;
}
.alert-text {
display: inline-block;
padding: 0rem 1rem;
border-radius: 1rem 1rem 1rem 1rem;
background: rgba(0, 0, 0, 0.5);
}
/*alertOut*/
.alertOut {
position: fixed;
top: 50%;
width: 100%;
}
.alertOut-text {
text-align: center;
}
@media screen and (min-width:768px) and (max-width:1920px) {
html {
font-size: 30px;
}
}
@media screen and (min-width:600px) and (max-width:768px) {
html {
font-size: 26px;
}
}
@media screen and (min-width:321px) and (max-width:380px) {
html {
font-size: 16px;
}
}
@media screen and (min-width:300px) and (max-width:320px) {
html {
font-size: 12px;
}
}
</style>
</head>
<body>
<section>
<div class="rotate-box">
<div class="box">
<div class="lottery">
</div>
<img src="http://adimg1.yokead.com/844F2D9BE9B2900A17EA771179FA67DC" alt="" class="btn" />
</div>
</div>
</section>
</body>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/rotate.js"></script>
<script type="text/javascript">
// 奖品配置
var awards = [{
'index': 0,
'text': '耳机',
},
{
'index': 1,
'text': 'iPhone',
},
{
'index': 2,
'text': '相机',
'name': 'icono-camera'
},
{
'index': 3,
'text': '咖啡杯',
},
{
'index': 4,
'text': '日历',
},
{
'index': 5,
'text': '键盘',
},
{
'index': 6,
'text': '键盘',
},
{
'index': 6,
'text': '键盘',
}
]
// 写入奖品
rotateContent(awards)
// 点击转盘
$('.btn').click(function() {
// 设置结束的角度
var end = 22.5;
$(".lottery").rotate({
duration: 3000, //旋转时间
angle: 20, //开始旋转的角度
animateTo: end + 360 * 5, //最终结束时的角度
callback: function() { //回调
}
});
});
function rotateContent(awards) {
var length = awards.length
var deg = 360 / length
var str = ''
for(var i in awards) {
str += '<div class="block ' + getColor(i, length) + '" style="color:#fff;' + preTransform() + ': rotate(' + deg * i + 'deg) skewY(' + (90 - deg) + 'deg)">'
str += '<div class="content" style="' + preTransform() + ':skewY(-' + (90 - deg) + 'deg) rotate(-' + deg / 2 + 'deg) translate(' + length + '%,' + 9 * length + '%)">'
str += '<div class="none">' + awards[i].text + '</div>'
str += '</div>'
str += '</div>'
}
$('.lottery').append(str)
if(length % 2 != 0) {
if(length % 3 == 1) {
$('.lottery .block').eq(length - 1).removeClass('block-red')
$('.lottery .block').eq(length - 1).addClass('block-yellow')
}
}
}
winChange()
//设置背景颜色
function getColor(str, length) {
var color
if(length % 2 == 0) {
if(str % 2 == 0) {
color = 'block-red'
} else if(str % 2 == 1) {
color = 'block-yellow'
}
} else {
if(str % 3 == 0) {
color = 'block-red'
} else if(str % 3 == 1) {
color = 'block-yellow'
} else if(str % 3 == 2) {
color = 'block-orange'
}
}
return color
}
// 兼容
function preTransform() {
var cssPrefix,
vendors = {
'': '',
Webkit: 'webkit',
Moz: '',
O: 'o',
ms: 'ms'
},
testEle = document.createElement('p'),
cssSupport = {};
Object.keys(vendors).some(function(vendor) {
if(testEle.style[vendor + (vendor ? 'T' : 't') + 'ransform'] !== undefined) {
cssPrefix = vendor ? '-' + vendor.toLowerCase() + '-' : '';
return true;
}
});
function normalizeCss(name) {
name = name.toLowerCase();
return cssPrefix ? cssPrefix + name : name;
}
cssSupport = {
transform: normalizeCss('Transform'),
}
return cssSupport.transform;
}
// 屏幕适配
$(window).resize(function() {
winChange()
})
function winChange() {
$(".rotate-box .box").css("height", $(".rotate-box .box").width() + 'px')
$('.lottery').css("height", $(".lottery").width() + 'px')
$('.lottery .block').css("height", $(".lottery .block").width() + 'px')
$(".login-box").css({
"width": $('body').width() + "px",
"left": $('body').offset().left + "px"
})
}
</script>
</html>
rotate.js(粘贴别人的)
(function($) {
var supportedCSS, styles = document.getElementsByTagName("head")[0].style,
toCheck = "transformProperty WebkitTransform OTransform msTransform MozTransform".split(" ");
for(var a = 0; a < toCheck.length; a++)
if(styles[toCheck[a]] !== undefined) supportedCSS = toCheck[a];
// Bad eval to preven google closure to remove it from code o_O
// After compresion replace it back to var IE = 'v' == '\v'
var IE = eval('"v"=="\v"');
jQuery.fn.extend({
rotate: function(parameters) {
if(this.length === 0 || typeof parameters == "undefined") return;
if(typeof parameters == "number") parameters = {
angle: parameters
};
var returned = [];
for(var i = 0, i0 = this.length; i < i0; i++) {
var element = this.get(i);
if(!element.Wilq32 || !element.Wilq32.PhotoEffect) {
var paramClone = $.extend(true, {}, parameters);
var newRotObject = new Wilq32.PhotoEffect(element, paramClone)._rootObj;
returned.push($(newRotObject));
}
else {
element.Wilq32.PhotoEffect._handleRotation(parameters);
}
}
return returned;
},
getRotateAngle: function() {
var ret = [];
for(var i = 0, i0 = this.length; i < i0; i++) {
var element = this.get(i);
if(element.Wilq32 && element.Wilq32.PhotoEffect) {
ret[i] = element.Wilq32.PhotoEffect._angle;
}
}
return ret;
},
stopRotate: function() {
for(var i = 0, i0 = this.length; i < i0; i++) {
var element = this.get(i);
if(element.Wilq32 && element.Wilq32.PhotoEffect) {
clearTimeout(element.Wilq32.PhotoEffect._timer);
}
}
}
});
// Library agnostic interface
Wilq32 = window.Wilq32 || {};
Wilq32.PhotoEffect = (function() {
if(supportedCSS) {
return function(img, parameters) {
img.Wilq32 = {
PhotoEffect: this
};
this._img = this._rootObj = this._eventObj = img;
this._handleRotation(parameters);
}
} else {
return function(img, parameters) {
// Make sure that class and id are also copied - just in case you would like to refeer to an newly created object
this._img = img;
this._rootObj = document.createElement('span');
this._rootObj.style.display = "inline-block";
this._rootObj.Wilq32 = {
PhotoEffect: this
};
img.parentNode.insertBefore(this._rootObj, img);
if(img.complete) {
this._Loader(parameters);
} else {
var self = this;
// TODO: Remove jQuery dependency
jQuery(this._img).bind("load", function() {
self._Loader(parameters);
});
}
}
}
})();
Wilq32.PhotoEffect.prototype = {
_setupParameters: function(parameters) {
this._parameters = this._parameters || {};
if(typeof this._angle !== "number") this._angle = 0;
if(typeof parameters.angle === "number") this._angle = parameters.angle;
this._parameters.animateTo = (typeof parameters.animateTo === "number") ? (parameters.animateTo) : (this._angle);
this._parameters.step = parameters.step || this._parameters.step || null;
this._parameters.easing = parameters.easing || this._parameters.easing || function(x, t, b, c, d) {
return -c * ((t = t / d - 1) * t * t * t - 1) + b;
}
this._parameters.duration = parameters.duration || this._parameters.duration || 1000;
this._parameters.callback = parameters.callback || this._parameters.callback || function() {};
if(parameters.bind && parameters.bind != this._parameters.bind) this._BindEvents(parameters.bind);
},
_handleRotation: function(parameters) {
this._setupParameters(parameters);
if(this._angle == this._parameters.animateTo) {
this._rotate(this._angle);
}
else {
this._animateStart();
}
},
_BindEvents: function(events) {
if(events && this._eventObj) { // Unbinding previous Events
if(this._parameters.bind) {
var oldEvents = this._parameters.bind;
for(var a in oldEvents)
if(oldEvents.hasOwnProperty(a)) // TODO: Remove jQuery dependency
jQuery(this._eventObj).unbind(a, oldEvents[a]);
}
this._parameters.bind = events;
for(var a in events)
if(events.hasOwnProperty(a))
// TODO: Remove jQuery dependency
jQuery(this._eventObj).bind(a, events[a]);
}
},
_Loader: (function() {
if(IE)
return function(parameters) {
var width = this._img.width;
var height = this._img.height;
this._img.parentNode.removeChild(this._img);
this._vimage = this.createVMLNode('image');
this._vimage.src = this._img.src;
this._vimage.style.height = height + "px";
this._vimage.style.width = width + "px";
this._vimage.style.position = "absolute"; // FIXES IE PROBLEM - its only rendered if its on absolute position!
this._vimage.style.top = "0px";
this._vimage.style.left = "0px";
/* Group minifying a small 1px precision problem when rotating object */
this._container = this.createVMLNode('group');
this._container.style.width = width;
this._container.style.height = height;
this._container.style.position = "absolute";
this._container.setAttribute('coordsize', width - 1 + ',' + (height - 1)); // This -1, -1 trying to fix ugly problem with small displacement on IE
this._container.appendChild(this._vimage);
this._rootObj.appendChild(this._container);
this._rootObj.style.position = "relative"; // FIXES IE PROBLEM
this._rootObj.style.width = width + "px";
this._rootObj.style.height = height + "px";
this._rootObj.setAttribute('id', this._img.getAttribute('id'));
this._rootObj.className = this._img.className;
this._eventObj = this._rootObj;
this._handleRotation(parameters);
}
else
return function(parameters) {
this._rootObj.setAttribute('id', this._img.getAttribute('id'));
this._rootObj.className = this._img.className;
this._width = this._img.width;
this._height = this._img.height;
this._widthHalf = this._width / 2; // used for optimisation
this._heightHalf = this._height / 2; // used for optimisation
var _widthMax = Math.sqrt((this._height) * (this._height) + (this._width) * (this._width));
this._widthAdd = _widthMax - this._width;
this._heightAdd = _widthMax - this._height; // widthMax because maxWidth=maxHeight
this._widthAddHalf = this._widthAdd / 2; // used for optimisation
this._heightAddHalf = this._heightAdd / 2; // used for optimisation
this._img.parentNode.removeChild(this._img);
this._aspectW = ((parseInt(this._img.style.width, 10)) || this._width) / this._img.width;
this._aspectH = ((parseInt(this._img.style.height, 10)) || this._height) / this._img.height;
this._canvas = document.createElement('canvas');
this._canvas.setAttribute('width', this._width);
this._canvas.style.position = "relative";
this._canvas.style.left = -this._widthAddHalf + "px";
this._canvas.style.top = -this._heightAddHalf + "px";
this._canvas.Wilq32 = this._rootObj.Wilq32;
this._rootObj.appendChild(this._canvas);
this._rootObj.style.width = this._width + "px";
this._rootObj.style.height = this._height + "px";
this._eventObj = this._canvas;
this._cnv = this._canvas.getContext('2d');
this._handleRotation(parameters);
}
})(),
_animateStart: function() {
if(this._timer) {
clearTimeout(this._timer);
}
this._animateStartTime = +new Date;
this._animateStartAngle = this._angle;
this._animate();
},
_animate: function() {
var actualTime = +new Date;
var checkEnd = actualTime - this._animateStartTime > this._parameters.duration;
// TODO: Bug for animatedGif for static rotation ? (to test)
if(checkEnd && !this._parameters.animatedGif) {
clearTimeout(this._timer);
}
else {
if(this._canvas || this._vimage || this._img) {
var angle = this._parameters.easing(0, actualTime - this._animateStartTime, this._animateStartAngle, this._parameters.animateTo - this._animateStartAngle, this._parameters.duration);
this._rotate((~~(angle * 10)) / 10);
}
if(this._parameters.step) {
this._parameters.step(this._angle);
}
var self = this;
this._timer = setTimeout(function() {
self._animate.call(self);
}, 10);
}
// To fix Bug that prevents using recursive function in callback I moved this function to back
if(this._parameters.callback && checkEnd) {
this._angle = this._parameters.animateTo;
this._rotate(this._angle);
this._parameters.callback.call(this._rootObj);
}
},
_rotate: (function() {
var rad = Math.PI / 180;
if(IE)
return function(angle) {
this._angle = angle;
this._container.style.rotation = (angle % 360) + "deg";
}
else if(supportedCSS)
return function(angle) {
this._angle = angle;
this._img.style[supportedCSS] = "rotate(" + (angle % 360) + "deg)";
}
else
return function(angle) {
this._angle = angle;
angle = (angle % 360) * rad;
// clear canvas
this._canvas.width = this._width + this._widthAdd;
this._canvas.height = this._height + this._heightAdd;
// REMEMBER: all drawings are read from backwards.. so first function is translate, then rotate, then translate, translate..
this._cnv.translate(this._widthAddHalf, this._heightAddHalf); // at least center image on screen
this._cnv.translate(this._widthHalf, this._heightHalf); // we move image back to its orginal
this._cnv.rotate(angle); // rotate image
this._cnv.translate(-this._widthHalf, -this._heightHalf); // move image to its center, so we can rotate around its center
this._cnv.scale(this._aspectW, this._aspectH); // SCALE - if needed ;)
this._cnv.drawImage(this._img, 0, 0); // First - we draw image
}
})()
}
if(IE) {
Wilq32.PhotoEffect.prototype.createVMLNode = (function() {
document.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
try {
!document.namespaces.rvml && document.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
return function(tagName) {
return document.createElement('<rvml:' + tagName + ' class="rvml">');
};
} catch(e) {
return function(tagName) {
return document.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
};
}
})();
}
})(jQuery);