-
Notifications
You must be signed in to change notification settings - Fork 0
/
mobileCoursel.js
139 lines (129 loc) · 4.13 KB
/
mobileCoursel.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* slider({cont:banner,imgCon:imgUl,dotCon:dotUl,time:2000});
* 传入json{cont:最大容器,imgCon:图片容器,dotCon:圆点容器,dotSty:圆点活动样式,time:图片播放间隔}
*/
function slider(json) {
var banner = json.cont;
var imgUl = json.imgCon;
var dotUl = json.dotCon;
var now = json.dotSty;
var width = banner.offsetWidth;
var current = 1;
banner.timeInterval = json.time;
clearInterval(banner.timer);
banner.timer = setInterval(function () {
current++;
slide.addTransition(imgUl);
var dis = -current * width;
slide.setTransform(imgUl, dis, 'x');
}, banner.timeInterval);
slide.addTransitionEnd(imgUl, function () {
if (current > imgUl.children.length - 2) {
current = 1;
} else if (current <= 0) {
current = imgUl.children.length - 2;
}
slide.removeTransition(imgUl);
slide.setTransform(imgUl, -current * width, 'x');
updateDot();
});
function updateDot() {
for (var i = 0; i < dotUl.children.length; i++) {
dotUl.children[i].className = '';
}
dotUl.children[current - 1].className = now;
}
imgUl.addEventListener('touchstart', function (e) {
clearInterval(banner.timer);
imgUl.startX = e.touches[0].pageX;
});
imgUl.addEventListener('touchmove', function (e) {
imgUl.isMove = true;
imgUl.endX = e.touches[0].pageX;
imgUl.distance = imgUl.endX - imgUl.startX;
slide.removeTransition(imgUl);
slide.setTransform(imgUl, -current * width + imgUl.distance, 'x');
});
imgUl.addEventListener('touchend', function () {
if (imgUl.isMove) {
if (Math.abs(imgUl.distance) > width / 3) {
if (imgUl.distance > 0) {
current--;
} else {
current++;
}
slide.addTransition(imgUl);
slide.setTransform(imgUl, -current * width, 'x');
} else {
slide.addTransition(imgUl);
slide.setTransform(imgUl, -current * width, 'x');
}
}
clearInterval(banner.timer)
banner.timer = setInterval(function () {
current++;
slide.addTransition(imgUl);
slide.setTransform(imgUl, -current * width, 'x');
}, banner.timeInterval);
});
}
var slide = {};
/**
* 给对象添加过度
* @param obj 需要添加过度的对象
*/
slide.addTransition = function (obj) {
obj.style.transition = 'all 0.5s';
obj.style.webkitTransition = 'all 0.5s';
};
/**
* 设置对象的变形行为
* @param obj 需要变形的对象
* @param dis 移动的距离
*/
slide.setTransform = function (obj, dis, dir) {
obj.style.transform = 'translate' + dir.toUpperCase() + '(' + dis + 'px)';
obj.style.webkitTransform = 'translate' + dir.toUpperCase() + '(' + dis + 'px)';
};
/**
* 移除对象的过度行为
* @param obj 需要移除过度的对象
*/
slide.removeTransition = function (obj) {
obj.style.transition = '';
obj.style.webkitTransition = '';
};
/**
* 当过度完成时,调用fn函数
* @param obj 需要监听过度完成的对象
* @param fn 调用的函数
*/
slide.addTransitionEnd = function (obj, fn) {
obj.addEventListener('transitionEnd', fn);
obj.addEventListener('webkitTransitionEnd', fn);
};
/**
* 监听触摸按钮时的点击事件
*/
slide.tap = function (obj, fn) {
var start = 0; // 保存触摸到屏幕的开始时间
var end = 0; // 离开屏幕的时间
var spend = 0; // 触摸到离开花费的时长
var isMove = false; // 记录是否有移动过手指
obj.addEventListener('touchstart', function () {
start = Date.now();
});
obj.addEventListener('touchmove', function () {
isMove = true;
});
obj.addEventListener('touchend', function () {
end = Date.now();
spend = end - start;
if (!isMove && spend < 200) {
if (fn) {
fn();
}
}
isMove = false;
});
};