slideunlock.js
5.28 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/**
* jquery plugin -- jquery.slideunlock.js
* Description: a slideunlock plugin based on jQuery
* Version: 1.1
* Author: Dong Yuhao
* www.sucaijiayuan.com
* created: March 27, 2016
*/
function SliderUnlock(elm, options, success) {
var me = this;
var html = require('common/slide-unlock.hbs')();
var $c = me.checkElm(elm) ? $(elm) : $;
success = me.checkFn(success) ? success : function() {};
me.elm = $(html).replaceAll($c);
me.$progress = me.elm.find('.progress');
me.$labelTip = me.elm.find('.label-tip');
var opts = {
successLabelTip: '验证成功',
duration: 200,
swipestart: false,
min: 0,
max: me.elm.width(),
index: 0,
isOk: false,
lableIndex: 0
};
opts = $.extend(opts, options || {});
$(opts.html).appendTo($c);
// $elm
// opts
me.opts = opts;
// 是否开始滑动
me.swipestart = opts.swipestart;
// 最小值
me.min = opts.min;
// 最大值
me.max = opts.max;
// 当前滑动条所处的位置
me.index = opts.index;
// 是否滑动成功
me.isOk = opts.isOk;
// 滑块宽度
me.labelWidth = me.elm.find('.label').width();
// 滑块背景
me.sliderBg = me.elm.find('#slider_bg');
// 鼠标在滑动按钮的位置
me.lableIndex = opts.lableIndex;
// success
me.success = success;
return this;
}
SliderUnlock.prototype.init = function() {
var me = this;
me.updateView();
me.elm.find('.label').on('mousedown', function(event) {
if (me.isOk) {
return;
}
var e = event || window.event;
me.lableIndex = e.clientX - this.offsetLeft;
me.handerIn();
}).on('mousemove', function(event) {
if (me.isOk) {
return;
}
me.handerMove(event);
}).on('mouseup', function(event) {
if (me.isOk) {
return;
}
me.handerOut();
}).on('mouseout', function(event) {
if (me.isOk) {
return;
}
me.handerOut();
}).on('touchstart', function(event) {
if (me.isOk) {
return;
}
var e = event || window.event;
me.lableIndex = e.originalEvent.touches[0].pageX - this.offsetLeft;
me.handerIn();
}).on('touchmove', function(event) {
if (me.isOk) {
return;
}
me.handerMove(event, 'mobile');
}).on('touchend', function(event) {
if (me.isOk) {
return;
}
me.handerOut();
});
};
/**
* 鼠标/手指接触滑动按钮
*/
SliderUnlock.prototype.handerIn = function() {
var me = this;
me.swipestart = true;
me.min = 0;
me.max = me.elm.width();
};
/**
* 鼠标/手指移出
*/
SliderUnlock.prototype.handerOut = function() {
var me = this;
// 停止
me.swipestart = false;
// me.move();
if (me.index < me.max) {
me.reset();
}
};
/**
* 鼠标/手指移动
* @param event
* @param type
*/
SliderUnlock.prototype.handerMove = function(event, type) {
var me = this;
if (me.swipestart) {
event.preventDefault();
event = event || window.event;
if (type === 'mobile') {
me.index = event.originalEvent.touches[0].pageX - me.lableIndex;
} else {
me.index = event.clientX - me.lableIndex;
}
me.move();
}
};
/**
* 鼠标/手指移动过程
*/
SliderUnlock.prototype.move = function() {
var me = this;
if ((me.index + me.labelWidth) >= me.max) {
me.index = me.max - me.labelWidth - 2;
// 停止
me.swipestart = false;
// 解锁
me.isOk = true;
}
if (me.index < 0) {
me.index = me.min;
// 未解锁
me.isOk = false;
}
if (me.index + me.labelWidth + 2 === me.max && me.max > 0 && me.isOk) {
// 解锁默认操作
$('.label').next('.label-tip').
text(me.opts.successLabelTip).css({
color: '#fff'
});
me.success();
}
me.updateView();
};
/**
* 更新视图
*/
SliderUnlock.prototype.updateView = function() {
var me = this;
me.sliderBg.css('width', me.index);
me.elm.find('.label').css('left', me.index + 'px');
me.$progress.css('width', me.index + 'px');
me.elm.toggleClass('slide-unlock-ok', me.isOk);
};
/**
* 重置slide的起点
*/
SliderUnlock.prototype.reset = function() {
var me = this;
me.index = 0;
me.isOk = false;
me.sliderBg.animate({
width: 0
}, me.opts.duration);
me.elm.find('.label').animate({
left: me.index
}, me.opts.duration);
me.$labelTip
.text('向右滑动验证')
.animate({
opacity: 1
}, me.opts.duration);
me.updateView();
};
/**
* 检测元素是否存在
* @param elm
* @returns {boolean}
*/
SliderUnlock.prototype.checkElm = function(elm) {
if ($(elm).length > 0) {
return true;
} else {
throw 'this element does not exist.';
}
};
/**
* 检测传入参数是否是function
* @param fn
* @returns {boolean}
*/
SliderUnlock.prototype.checkFn = function(fn) {
if (typeof fn === 'function') {
return true;
} else {
throw 'the param is not a function.';
}
};
module.exports = SliderUnlock;