This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathzepto.turntable.js
98 lines (98 loc) · 2.79 KB
/
zepto.turntable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
;(function($,window,document,undefined) {
var Rolling = function(ele,obj){
this.$element = ele;
this.defaults = {
//好了我就是懒 没填默认参数
};
this.args = $.extend({}, this.defaults, obj);
}
Rolling.prototype = {
consturctor:Rolling,
builder:function(){
var _this=this,
$showArea = $(_this.args.showArea),
$ctrlArea = $(_this.args.ctrlArea),
$counts = $ctrlArea.children().length,
$dots = $ctrlArea.children(), //小圆点个数
_success = _this.args.success,
_ctrlArea = _this.args.ctrlArea, //缓存读取
_radius = _this.args.radius, //大圆半径
_angle = 360/$counts, //单个扇形角度
navDotIdx=1,
curAngle=0,
curDot=0,
targetDot=0,
isLock=false;
//设置大圆边长
$ctrlArea.width(_radius*2);
$ctrlArea.parent().width(_radius*2);
$ctrlArea.height(_radius*2);
$ctrlArea.parent().height(_radius*2);
//给小圆点定位
$dots.each(function(index){
$(this).css({
'width':_this.args.sideLen,
'height':_this.args.sideLen,
'marginLeft':-0.5*_this.args.sideLen,
'marginTop':-0.5*_this.args.sideLen,
'position':'absolute',
'top':'50%',
'left':'50%',
'-webkit-transform':'rotate('+index*_angle+'deg) translateY('+ (-_radius)+'px) translateZ(0);',
'transform':'rotate('+index*_angle+'deg) translateY('+ (-_radius)+'px) translateZ(0);'
})
});
//默认第一个小圆加active
$dots.eq(0).addClass('active');
//判断
function swipes(direction){
if(direction==="right"){
navDotIdx+=1;
curAngle-=_angle;
if(navDotIdx>$counts) {
navDotIdx=1;
}
} else if(direction==="left"){
navDotIdx-=1;
curAngle+=_angle;
if(navDotIdx<1){
navDotIdx=$counts;
}
}
// 每转一次
$ctrlArea.css({
"-webkit-transform":"rotate("+curAngle+"deg) translateZ(0);",
"transform":"rotate("+curAngle+"deg) translateZ(0);"
});
$dots.eq(navDotIdx-1).addClass("active").siblings().removeClass("active");
_success(navDotIdx);
}
// 转动,这个部分需要优化
$(document).on("touchstart",_ctrlArea,function(e){
curDot=e.touches[0].pageX;
},false).on("touchmove",_ctrlArea,function(e){
e.preventDefault();
e.stopPropagation();
if(isLock) return;
targetDot=e.touches[0].pageX;
if(targetDot-curDot>0){
swipes("left");
isLock=true;
} else if(targetDot-curDot<0){
swipes("right");
isLock=true;
}
}).on("webkitTransitionEnd",_ctrlArea,function(e){
e.stopPropagation();
isLock=false;
var _index=$(_ctrlArea +' .active').index();
$showArea.children().eq(_index).show().siblings().hide();
});
}
};
// 入口
window.Turntable = function(arg) {
var rolling = new Rolling(this,arg);
return rolling.builder();
}
})($,window,document);