-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviRotator.js
536 lines (459 loc) · 20.9 KB
/
viRotator.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/// <reference path="jquery-1.6.4.min.js" />
; (function ($) {
/***********************************ViScrollBar************************************/
//create the ScrollBar class
var ViScrollBar = function (element, options) {
var defaults = {
maxDistance: 100,
limitDistance: 100,
animSpeed: 300,
direction: 'horizontal',
onChanging: $.noop(),
onChanged: $.noop()
};
//default options
this.setting = $.extend({}, defaults, options);
this.vars = {
direction: {
horizontal: "horizontal",
vertical: "vertical"
}
};
//initialize dom
this.scrollBar = $(element);
this.initialize();
};
ViScrollBar.prototype = {
initialize: function () {
var _this = this,
setting = _this.setting,
vars = _this.vars,
scrollBar = _this.scrollBar;
scrollBar.append("<div class=\"vi-scroll-bg\"></div><div class=\"vi-scroll-handler\"></div>");
var scrollBarWidth = scrollBar.outerWidth(true),
scrollBarHeight = scrollBar.outerHeight(true);
_this.handlerSize = _this.getHandlerSize();
scrollBar.addClass(setting.direction);
if (setting.direction == vars.direction.horizontal) {
_this.limitDistance = Math.round(scrollBarWidth - _this.handlerSize);
_this.scrollHandler = scrollBar.find(".vi-scroll-handler").width(_this.handlerSize).height(scrollBar.outerHeight(true));
} else {
_this.limitDistance = Math.round(scrollBarHeight - _this.handlerSize);
_this.scrollHandler = scrollBar.find(".vi-scroll-handler").height(_this.handlerSize).width(scrollBar.outerWidth(true));
}
scrollBar.disableSelection();
_this._registeDragEvent();
},
_registeDragEvent: function () {
var _this = this,
setting = _this.setting,
vars = _this.vars,
scrollBar = _this.scrollBar,
scrollHandler = _this.scrollHandler,
limited = {
min: 0,
max: _this.limitDistance
};
//bind mousedown event
scrollHandler.bind("mousedown", function (e) {
scrollBar.disableSelection();
var dx = null;
if (setting.direction == vars.direction.horizontal) {
dx = {
left: $(e.target).position().left,
pageX: e.pageX
};
} else {
dx = {
top: $(e.target).position().top,
pageY: e.pageY
};
}
scrollHandler.css('opacity', 0.8);
$(document).bind("mousemove", dx, onMoveStart).bind('mouseup', dx, onMoveEnd);
});
function onMoveStart(e) {
scrollBar.disableSelection();
var dx = e.data;
var left = Math.min(Math.max(e.pageX - dx.pageX + dx.left, limited.min), limited.max),
top = Math.min(Math.max(e.pageY - dx.pageY + dx.top, limited.min), limited.max);
if (setting.direction == vars.direction.horizontal) {
scrollHandler.css({ left: left });
} else {
scrollHandler.css({ top: top });
}
if (setting.onChanging && $.isFunction(setting.onChanging)) {
if (setting.direction == vars.direction.horizontal) {
setting.onChanging(left);
} else {
setting.onChanging(top);
}
}
}
function onMoveEnd(e) {
scrollBar.disableSelection();
scrollHandler.css('opacity', 1);
$(document).unbind("mousemove", onMoveStart).unbind("mouseup", onMoveEnd);
if (setting.onChanged && $.isFunction(setting.onChanged)) {
setting.onChanged();
}
}
},
getHandlerSize: function () {
var _this = this,
setting = _this.setting,
vars = _this.vars,
scrollBar = _this.scrollBar,
maxDistance = _this.setting.maxDistance;
if (setting.direction == vars.direction.horizontal) {
var scrollBarWidth = scrollBar.outerWidth(true);
var handlerWidth = scrollBarWidth / maxDistance * scrollBarWidth;
handlerWidth = handlerWidth > 20 ? handlerWidth : 20;
return handlerWidth;
} else {
var scrollBarHeight = scrollBar.outerHeight(true);
var handlerHeight = scrollBarHeight / maxDistance * scrollBarHeight;
handlerHeight = handlerHeight > 20 ? handlerHeight : 20;
return handlerHeight;
}
},
scrollTo: function (val, callBack) {
var _this = this,
setting = _this.setting,
vars = _this.vars,
handler = _this.scrollHandler,
limited = _this.limitDistance;
try {
var moveExpress = null,
animateOption = null;
var moveInstance = setting.direction == vars.direction.horizontal ? handler.position().left : handler.position().top;
moveExpress = val > 0 ? (limited - moveInstance >= val ? val : limited - moveInstance) : (moveInstance > Math.abs(val) ? val : -moveInstance);
if (setting.direction == vars.direction.horizontal) {
animateOption = { left: "+=" + moveExpress };
} else {
animateOption = { top: "+=" + moveExpress };
}
handler.animate(animateOption, {
queue: false,
duration: setting.animSpeed,
easing: 'linear',
complete: function () {
if (callBack && $.isFunction(callBack)) {
callBack();
}
}
});
}
catch (e) { }
}
};
$.fn.viScrollBar = function (options) {
return this.each(function (key, value) {
var element = $(this);
if (element.data('viScrollBar')) return element.data('viScrollBar');
// Pass options to plugin constructor
var viScrollBar = new ViScrollBar(this, options);
// Store plugin object in this element's data
element.data('viScrollBar', viScrollBar);
});
};
/**********************************ViRotator*************************************/
//create the ViRotator class
var ViRotator = function (element, options) {
var setting = this.setting = $.extend(this, $.fn.viRotator.defaults, options);
this.vars = {
prev: "prev",
next: "next",
direction: {
horizontal: "horizontal",
vertical: "vertical"
}
};
this.rotator = element;
this.isHorizontal = setting.direction == this.vars.direction.horizontal;
this.moveDirection = this.isHorizontal ? "margin-left" : "margin-top";
this.initialize();
};
ViRotator.prototype = {
initialize: function () {
var _this = this,
rotator = _this.rotator,
setting = _this.setting,
vars = _this.vars;
//由于牵涉到拖动的操作,设置元素的不可选操作有助于用户体验,
//要不会出现选中更多个item的情况,ui变得丑陋
rotator.disableSelection();
rotator.addClass("vi-rotator")
.children("ul:first")
.wrap("<div class=\"vi-rotator-container\">" +
"<div class=\"vi-rotator-outer\">" +
"<div class=\"vi-rotator-items\"></div>" +
"</div>" +
"</div>");
//设置rotator的相关样式,尺寸
var rotatorItems = _this.rotatorItems = $(".vi-rotator-items", rotator);
var rotatorOuter = _this.rotatorOuter = $(".vi-rotator-outer", rotator);
var rotatorItem = _this.rotatorItem = _this._getItem();
rotatorItem.addClass("vi-rotator-item").css({
"float": "left"
});
if (!_this.isHorizontal) {
//这里为了解决ul > li的margin-top, margin-bottom样式重叠的bug.
rotatorItem.css({
"clear": "both"
});
}
var itemsWidth = _this._getItemsWidth(),
itemsHeight = _this._getItemsHeight(),
containerWidth = _this._getContainerWidth(),
containerHeight = _this._getContainerHeight();
//检查是否需要显示操作栏,例如:滚动轴,导航按钮
var isShowBar = _this.isHorizontal ? itemsWidth > containerWidth : itemsHeight > containerHeight;
rotatorOuter.width(containerWidth);
if (_this.isHorizontal) {
rotatorItems.css({
width: itemsWidth,
height: _this._getItemHeight(),
overflow: 'hidden'
});
} else {
rotatorOuter.css({
height: containerHeight,
overflow: "hidden"
});
}
//构建滚动轴
if (setting.scrollBar && isShowBar) {
_this._buildScrollBar();
}
//构建导航
if (setting.navBar && isShowBar) {
_this._buildNavControl();
}
//注册item的点击事件
if (setting.itemClick != null && $.isFunction(setting.itemClick)) {
rotator.delegate(".vi-rotator-item", "click", function () {
rotatorItem.removeClass("active");
$(this).addClass("active");
setting.itemClick($(this));
});
}
//设置默认开始项
if (setting.startItem == null && $.type(setting.startItem) == "number") {
if (setting.startItem <= 0) { return; }
this.setStartItem(setting.startItem);
}
//设置自动循环播放
if (setting.autoPlay) {
//未实现
}
},
_getContainerWidth: function () {
var rotator = this.rotator;
return rotator.outerWidth();
},
_getContainerHeight: function () {
var rotator = this.rotator;
return rotator.outerHeight();
},
_getItem: function () {
return this.rotator.find(".vi-rotator-items > ul > li");
},
_getItemWidth: function () {
//这里Item的宽度通过样式定义
return this._getItem().outerWidth(true);
},
_getItemHeight: function () {
//这里Item的高度通过样式定义
return this._getItem().outerHeight(true);
},
_getItemsWidth: function () {
var itemsWidth = 0;
this._getItem().each(function (i, n) {
itemsWidth += $(n).outerWidth(true);
});
return itemsWidth;
},
_getItemsHeight: function () {
var itemsHeight = 0;
this._getItem().each(function (i, n) {
itemsHeight += $(n).outerHeight(true);
});
return itemsHeight;
},
_getBarStepDistance: function (moveDistance) {
var _this = this,
vars = _this.vars,
setting = _this.setting,
containerWidth = _this._getContainerWidth(),
containerHeight = _this._getContainerHeight(),
itemsWidth = _this._getItemsWidth(),
itemsHeight = _this._getItemsHeight();
if (itemsWidth <= 0 || containerWidth <= 0) {
return 0;
}
if (itemsHeight <= 0 || containerHeight <= 0) {
return 0;
}
if (setting.direction == vars.direction.horizontal) {
return moveDistance / itemsWidth * containerWidth;
} else {
return moveDistance / itemsHeight * containerHeight;
}
},
_buildScrollBar: function () {
var _this = this,
vars = _this.vars,
rotator = _this.rotator,
setting = _this.setting,
itemsWidth = _this._getItemsWidth(),
itemsHeight = _this._getItemsHeight(),
containerWidth = _this._getContainerWidth(),
containerHeight = _this._getContainerHeight(),
moveDistance = setting.moveDistance,
barStepDistance = _this._getBarStepDistance(moveDistance);
if ($.fn.viScrollBar) {
$(".vi-rotator-container", rotator).append("<div class=\"vi-scroll-bar\"></div>");
var scrollBar = $(".vi-scroll-bar", rotator);
if (_this.isHorizontal) {
scrollBar.addClass(setting.direction);
scrollBar.width(containerWidth);
} else {
scrollBar.addClass(setting.direction);
var boxWidth = containerWidth - scrollBar.outerWidth(true);
_this.rotatorOuter.width(boxWidth);
var rotatorItem = _this.rotatorItem;
rotatorItem.width(boxWidth - (rotatorItem.outerWidth(true) - rotatorItem.outerWidth()));
scrollBar.height(containerHeight);
}
scrollBar.viScrollBar({
animSpeed: setting.animSpeed,
maxDistance: _this.isHorizontal ? itemsWidth : itemsHeight,
direction: setting.direction,
onChanging: function (e) {
if (_this.isHorizontal) {
_this.slideAnimate(-itemsWidth / containerWidth * e);
} else {
_this.slideAnimate(-itemsHeight / containerHeight * e);
}
}
});
var scrollBarObj = scrollBar.data("viScrollBar");
rotator.delegate(".vi-prev", "click", function (e) {
scrollBarObj.scrollTo(barStepDistance, function () { });
});
rotator.delegate(".vi-next", "click", function (e) {
scrollBarObj.scrollTo(-barStepDistance, function () { });
});
}
},
_buildNavControl: function () {
var _this = this,
vars = _this.vars,
rotator = _this.rotator,
setting = _this.setting,
moveDistance = setting.moveDistance,
barStepDistance = _this._getBarStepDistance(moveDistance);
var rotatorOuter = _this.rotatorOuter;
$("<a href=\"javascript:void(0);\" class=\"vi-prev\">" + setting.prevText + "</a>").insertBefore(rotatorOuter);
$("<a href=\"javascript:void(0);\" class=\"vi-next\">" + setting.nextText + "</a>").insertAfter(rotatorOuter);
var prevElement = $(".vi-prev", rotator);
var nextElement = $(".vi-next", rotator);
prevElement.addClass(setting.direction);
nextElement.addClass(setting.direction);
setting.prevClass && prevElement.addClass(setting.prevClass);
setting.nextClass && nextElement.addClass(setting.nextClass);
//注册移动事件
//计算出能够移动的最大距离
var maxMoveDistance = _this.isHorizontal ? _this._getItemsWidth() - _this._getContainerWidth() : _this._getItemsHeight() - _this._getContainerHeight();
var moveDirection = _this.moveDirection;
var rotatorItems = _this.rotatorItems;
rotator.delegate(".vi-prev", "click", function (e) {
var marginDistance = Math.abs(parseFloat(rotatorItems.css(moveDirection)));
if (maxMoveDistance >= marginDistance) {
var prevMoveDistance = Math.min(maxMoveDistance - Math.abs(marginDistance), moveDistance);
_this.slideAnimate(prevMoveDistance, vars.prev, e);
}
});
rotator.delegate(".vi-next", "click", function (e) {
var marginDistance = parseFloat(rotatorItems.css(moveDirection));
if (maxMoveDistance >= Math.abs(marginDistance) && marginDistance < 0) {
var nextMoveDistance = moveDistance;
if (Math.floor(maxMoveDistance - Math.abs(marginDistance)) !== 0) {
nextMoveDistance = Math.min(Math.abs(marginDistance), moveDistance);;
}
_this.slideAnimate(nextMoveDistance, vars.next, e);
}
});
},
slideAnimate: function (moveDistance, moveDirection, e) {
var _this = this,
rotator = _this.rotator,
vars = _this.vars,
setting = _this.setting,
rotatorItems = _this.rotatorItems;
if (setting == null) { setting = $.fn.viRotator.defaults; }
if (moveDirection == null) {
rotatorItems.css(_this.moveDirection, moveDistance);
} else {
var moveDirect = moveDirection === vars.prev ? '-=' : '+=';
if (setting.beforeChange && $.isFunction(setting.beforeChange)) {
setting.beforeChange.call(_this, moveDirection, e);
}
//难点是:如何设置动画结束的逻辑,需要计算当items到达最极限的位置,
//将不再执行任何动画
var animateOption = _this.isHorizontal ? { marginLeft: moveDirect + moveDistance } : { marginTop: moveDirect + moveDistance };
rotatorItems.stop().animate(animateOption, {
queue: false,
duration: setting.animSpeed,
easing: 'linear',
complete: function () {
if (setting.afterChange && $.isFunction(setting.afterChange)) {
setting.afterChange.call(_this, moveDirection, e);
}
}
});
}
},
setStartItem: function (index) {
var _this = this,
rotator = _this.rotator,
itemIndex = 0,
rotatorItemSize = _this._getItem().length;
if (index > rotatorItemSize) {
itemIndex = rotatorItemSize - 1;
} else {
itemIndex = index > 0 ? index - 1 : 0;
}
var currentItem = _this.rotator.find("li:eq(" + itemIndex + ")");
var scrollLeft = currentItem.position().left;
_this.slideAnimate(scrollLeft);
}
};
$.fn.viRotator = function (options) {
return this.each(function (key, value) {
var element = $(this);
// Return early if this element already has a plugin instance
if (element.data('viRotator')) return element.data('viRotator');
// Pass options to plugin constructor
var viRotator = new ViRotator(element, options);
// Store plugin object in this element's data
element.data('viRotator', viRotator);
});
};
//Default settings
$.fn.viRotator.defaults = {
moveDistance: 130,
startItem: 0,
animSpeed: 300,
autoPlay: false, //暂时没有实现
scrollBar: true,
navBar: true,
prevText: 'Prev',
nextText: 'Next',
direction: "horizontal",
itemClick: $.noop(),
beforeChange: $.noop(),
afterChange: $.noop()
};
})(jQuery);