-
Notifications
You must be signed in to change notification settings - Fork 0
/
viewer.js
519 lines (409 loc) · 11.7 KB
/
viewer.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
/**
* 图片查看器
*/
define(function(require) {
var $ = require('jquery');
var base = require('./base');
var keyboard = require('./keyboard');
var body = $('body');
var html = $('html');
var win = $(window);
var doc = $(document);
var overflow = 'lg-overflow';
var eventScope = 'lg-viewer';
var layout = $('<div>').addClass('lg-viewer');
// 事件
var mousewheel = 'mousewheel.' + eventScope;
var mousescroll = ' DOMMouseScroll.' + eventScope;
var mousemove = 'mousemove.' + eventScope;
var mouseup = 'mouseup.' + eventScope;
var mousedown = 'mousedown.' + eventScope;
var keyup = 'keyup.' + eventScope;
var click = 'click.' + eventScope;
var options = {
pointer: 0, // 从第一个开始处理
escKey: true, // esc操作关闭
closeIcon: true, // 显示关闭按钮
wheelKey: true, // 鼠标滑轮滚动图片缩放
leftRightKey: true, // 左右按键切换图片
loadingIcon: true, // loading图片
targetAttr: '', // 使用什么属性获取图片链接
rotate: 3, // 1ms的旋转角度
reverseHorizontal: 0.05, // 水平旋转
reverseVertical: 0.05, // 水平旋转
scale: 0.05, // 缩放比例
toolbar: true, // 显示工具栏
imgList: true, // 图片列表
sideArrows: false, // 图片左右按钮
targetTrigger: true // 给图片增加点击显示事件
};
var toolbarTemplate = ''
+ ' <ul class="lg-viewer-toolbar">'
+ ' <li class="lg-viewer-zoom-in" data-action="zoom-in"></li>'
+ ' <li class="lg-viewer-zoom-out" data-action="zoom-out"></li>'
+ ' <li class="lg-viewer-reset" data-action="reset"></li>'
+ ' <li class="lg-viewer-prev" data-action="prev"></li>'
+ ' <li class="lg-viewer-next" data-action="next"></li>'
+ ' <li class="lg-viewer-rotate-left" data-action="rotate-left"></li>'
+ ' <li class="lg-viewer-rotate-right" data-action="rotate-right"></li>'
+ ' <li class="lg-viewer-flip-horizontal" data-action="flip-horizontal"></li>'
+ ' <li class="lg-viewer-flip-vertical" data-action="flip-vertical"></li>'
+ ' </ul>'
;
var Viewer = base.inherit({
initialize: function(opts) {
this.opts = $.extend({}, options, opts);
this.size = this._size();
this.resetParam();
this.opts.targetTrigger && this.showPanel();
},
render: function(index) {
var opts = this.opts;
if (!opts.target || !opts.target.length) {
return;
}
this.prepare();
this._render(index);
},
/**
* 点击图片触发界面
*/
showPanel: function() {
var _this = this;
var target = this.opts.target;
target.on(click, function() {
var index = target.toArray().indexOf(this);
_this.render(index);
});
},
image: function(target) {
var img = new Image();
img.src = this.opts.targetAttr && $(target).attr(this.opts.targetAttr) || target.src;
return img;
},
imgsList: function() {
var opts = this.opts;
var target = opts.target;
var list = document.createElement('div');
var _this = this;
list.className = 'lg-position-list';
var imgTmp;
target.each(function(key) {
imgTmp = _this.image(this);
$(imgTmp).data('index', key);
list.appendChild(imgTmp);
});
return $(list);
},
/**
* 添加左右箭头
*/
sideArrows: function(isleft, isright) {
var right = $('<div>').addClass('lg-viewer-right-arrow');
var left = $('<div>').addClass('lg-viewer-left-arrow');
$(right).on('click', this.nextImg.bind(this));
$(left).on('click', this.prevImg.bind(this));
isleft && this.represent.append(left);
isright && this.represent.append(right);
},
prepare: function() {
layout.html('');
var opts = this.opts;
this.imgList = this.imgsList();
this.represent = $('<div>').addClass('lg-represent');
this.toolbar = $('<div>').addClass('lg-viewer-toolbar-wrap').html(toolbarTemplate);
this.close = $('<div class="lg-viewer-button lg-viewer-close"></div>');
this.layout = layout;
!this.opts.closeIcon && this.close.hide();
var bottom = $('<div>').addClass('lg-img-bottom').html(this.imgList);
layout.append(this.close);
layout.append(this.represent);
opts.toolbar && layout.append(this.toolbar);
opts.imgList && layout.append(bottom);
body.append(layout);
this.addListener();
},
/**
* 删除界面和事件
*/
remove: function() {
html.removeClass(overflow);
doc.off(mousewheel).off(mousescroll).off(keyup).off(mousemove);
this.layout.remove();
},
/**
* 渲染图片, 可用于直接触发界面
*
* @param {number} pointer
* @return {jquery} image
*/
_render: function(pointer) {
!html.hasClass(overflow) && html.addClass(overflow);
var opts = this.opts;
var target = opts.target || [];
pointer = pointer !== undefined ? pointer : opts.pointer;
pointer = this.setPointer(pointer);
var img = target[pointer];
if (!img) {
return;
}
this.imgActive(pointer);
opts.pointer = pointer;
var image = this.image(img);
var _this = this;
image = $(image).addClass('lg-represent-img').css('visibility', 'hidden');
this.represent.addClass('lg-loading').html(image);
opts.sideArrows && this.sideArrows(pointer !== 0, pointer !== opts.target.length - 1);
image.on('load', function() {
_this.represent.removeClass('lg-loading');
_this.imgOffset(image);
_this.centreImgList();
image.css('visibility', 'visible');
});
},
_size: function() {
return {
height: win.height(),
width: win.width()
};
},
/**
* 设置图片位置
* @param 图片
* @param 是否可以超过wrap
*/
imgOffset: function(image) {
var height = image.height();
var size = this.size;
var topHeight = size.height - this.imgList.height() - this.toolbar.height();
// 重置图片高度, 宽度会自适应
var _height = Math.min(height, topHeight);
image.css({
height: _height
});
var _width = image.width();
image.css({
top: (topHeight - _height) / 2,
left: (size.width - _width) / 2
});
},
setPointer: function(pointer) {
var opts = this.opts;
var length = opts.target.length;
pointer = +pointer >= length ? length - 1 : pointer;
return pointer < 0 ? 0 : pointer;
},
// 下一张图
nextImg: function() {
var pointer = this.setPointer(++this.opts.pointer);
this._render(pointer);
},
// 前一张图
prevImg: function() {
var pointer = this.setPointer(--this.opts.pointer);
this._render(pointer);
},
// 重置
resetImg: function() {
var pointer = this.setPointer(this.opts.pointer);
this.resetParam();
this._render(pointer);
},
_transform: function(cb) {
var prefix = ['', '-webkit-', '-o-', '-ms-', '-moz-'];
var length = prefix.length;
var image = this.represent.find('img');
var i = 0;
for (; i < length; i++) {
image.css(prefix[i] + 'transform', cb.bind(this)(image));
}
},
resetParam: function() {
this.rotateAngle = 0;
this.horizontal = 1;
this.vertical = 1;
},
/**
* 缩放图片不更改中心点
*
* @param
* @param
*/
fixCenter: function(image, scale) {
var height = image.height();
var width = image.width();
var _height = image.height() * scale;
image.height(_height);
var _width = image.width();
var offset = {};
offset.left = parseFloat(image.css('left'));
offset.top = parseFloat(image.css('top'));
image.css({
top: offset.top - (_height - height) / 2,
left: offset.left - (_width - width) / 2
});
},
// 放大
zoomInImg: function() {
var image = this.represent.find('img');
this.fixCenter(image, 1 + this.opts.scale);
},
// 缩小
zoomOutImg: function() {
var image = this.represent.find('img');
this.fixCenter(image, 1 - this.opts.scale);
},
fixTransform: function() {
return 'scale(' + this.horizontal + ', ' + this.vertical + ') rotate(' + this.rotateAngle + 'deg)';
},
rotateLeftImg: function() {
var interval = setInterval(function() {
this.rotateAngle -= this.opts.rotate;
this._transform(function(image) {
return this.fixTransform();
});
if (this.rotateAngle % 90 === 0) {
this.rotateAngle = this.rotateAngle % 360;
clearInterval(interval);
return;
}
}.bind(this), 1);
},
rotateRightImg: function() {
var interval = setInterval(function() {
this.rotateAngle += this.opts.rotate;
this._transform(function(image) {
return this.fixTransform();
});
if (this.rotateAngle % 90 === 0) {
this.rotateAngle = this.rotateAngle % 360;
clearInterval(interval);
return;
}
}.bind(this), 1);
},
flipHorizontalImg: function() {
var interval = setInterval(function() {
this.horizontal -= this.opts.reverseHorizontal;
if (Math.abs(this.horizontal.toFixed(1)) === 1) {
this.opts.reverseHorizontal *= -1;
clearInterval(interval);
return;
}
this._transform(function(image) {
return this.fixTransform();
});
}.bind(this), 1);
},
flipVerticalImg: function() {
var interval = setInterval(function() {
this.vertical -= this.opts.reverseVertical;
if (Math.abs(this.vertical.toFixed(1)) === 1) {
this.opts.reverseVertical *= -1;
clearInterval(interval);
return;
}
this._transform(function(image) {
return this.fixTransform();
});
}.bind(this), 1);
},
/**
* 拖拽图片
*/
drag: function(e) {
var represent = this.represent.find('img');
var offset = {};
offset.left = parseFloat(represent.css('left'));
offset.top = parseFloat(represent.css('top'));
var spaceX = e.clientX - offset.left;
var spaceY = e.clientY - offset.top;
doc.on(mousemove, function(evt) {
evt.preventDefault && evt.preventDefault();
represent.css({
left: evt.clientX - spaceX,
top: evt.clientY - spaceY
});
});
doc.on(mouseup, function() {
doc.off(mousemove);
doc.off(mouseup);
});
},
/**
* 对元素添加事件
*/
addListener: function() {
var _this = this;
var opts = this.opts;
this.toolbar.on('click', 'li', function() {
var action = $(this).data('action');
var act = action.replace(/-([a-z])/g, function(all, first) {
return (first || '').toUpperCase();
});
_this[act + 'Img']();
});
this.imgList.on('click', 'img', function() {
var index = $(this).data('index');
_this._render(+index);
});
// 拖拽
this.represent.on(mousedown, '.lg-represent-img', this.drag.bind(this));
// 关闭界面
this.close.on('click', function() {
_this.remove();
});
var isUp = function(e) {
if (e.type === 'DOMMouseScroll') {
return e.originalEvent.detail > 0;
} else {
return e.originalEvent.wheelDelta < 0;
}
};
// 放大缩小
opts.wheelKey && this.represent.on([mousewheel, mousescroll].join(' '), '.lg-represent-img', function(e) {
!isUp(e) ? _this.zoomInImg() : _this.zoomOutImg();
});
doc.on(keyup, function(e) {
if (opts.escKey) {
keyboard.isEsc(e) && _this.remove();
}
if (opts.leftRightKey) {
keyboard.isLeft(e) && _this.prevImg();
keyboard.isRight(e) && _this.nextImg();
}
});
},
/**
* 居中图片列表
*/
centreImgList: function() {
var image = this.imgList.find('.lg-img-active');
if (!image.length) {
return;
}
var _left = 0;
image.prevAll().each(function() {
_left += $(this).outerWidth();
});
var width = image.outerWidth();
var size = this.size;
var left = (size.width / 2) - _left - width;
this.imgList.css({
left: left
});
},
/**
* 设置图片
*
* @param {number} 需要显示的图片位置
* @param image 需要显示的图片
*/
imgActive: function(pointer, image) {
var images = this.imgList.find('img');
images.removeClass('lg-img-active');
$(image || images[pointer]).addClass('lg-img-active');
}
});
return Viewer;
});