-
Notifications
You must be signed in to change notification settings - Fork 3
/
ezoom.js
297 lines (266 loc) · 9.04 KB
/
ezoom.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
var ezoom = {};
ezoom = {
thisImg: null,
rotate: 0,
scale: 1,
grabbing: false,
translateX: 0,
translateY: 0,
isShowed: false,
imgTag: null,
zoomModal: null,
options: null,
onInit: function (domElement, options) {
options = options || {};
ezoom.options = options;
ezoom.createZoomModalTag();
// In case the param to be passed to is an url string, else the neatest domeElement <img>
if (ezoom.options.src && typeof ezoom.options.src == "string") {
domElement.on('click', function(e) {
ezoom.thisImg = $(this);
ezoom.show(ezoom.options.src);
});
} else {
domElement.addClass('ezoom').on('click', function(e) {
ezoom.thisImg = $(this);
ezoom.show(ezoom.thisImg.attr('src'));
});
}
ezoom.initMouseScrolling();
ezoom.initEvent();
},
createZoomModalTag: function() {
// Use this if using `$('#zoomModal').hide();`
if ($('#zoomModal').length > 0) return;
var $ezoomWrap = $('<div id="zoomModal" class="modal" style="display:none;position:fixed;z-index: 1000000;padding:0px 0;left:0;top:0;width:100%;height:100%;overflow:auto;background-color:rgba(0,0,0,0.8);transition:.3s"></div>'),
$closeBtn = $('<span title="Close" class="fas fa-times" id="close" style="cursor:pointer;position:fixed;top:15px;right:35px;color:#b5b5b5;font-size:20px;font-weight:700;transition:.3s"></span>'),
$rotateRightBtn = $('<span title="Rotate right" class="fas fa-redo" id="close" style="z-index:1;cursor:pointer;position:fixed;top:15px;right:70px;color:#b5b5b5;font-size:20px;font-weight:700;transition:.3s"></span>'),
$rotateLeftBtn = $('<span title="Rotate left" class="fas fa-undo" id="close" style="z-index:1;cursor:pointer;position:fixed;top:15px;right:105px;color:#b5b5b5;font-size:20px;font-weight:700;transition:.3s"></span>'),
$image = $('<img title="Please scroll the mouse pointer to zoom in/out the image." class="" style="border-radius:5px;cursor:move;margin:auto;display:block;max-width:100%;max-height:100%" id="zoomModalImg">');
if (!ezoom.options.hideControlBtn) {
$ezoomWrap.append($rotateRightBtn, $rotateLeftBtn);
}
$ezoomWrap.append($image, $closeBtn );
$('body').append($ezoomWrap);
ezoom.imgTag = $image;
ezoom.zoomModal = $ezoomWrap;
$closeBtn.on('click', ezoom.remove);
$rotateRightBtn.on('click', function() {
ezoom.doRotate("right");
});
$rotateLeftBtn.on('click', function() {
ezoom.doRotate("left");
});
},
show: function(src) {
ezoom.isShow = true;
ezoom.reset();
$('#zoomModal').show();
$('#zoomModal #zoomModalImg').attr('src', src);
ezoom.poscalSizeAndPosition();
if (typeof ezoom.options.onShow && typeof ezoom.options.onShow == "function") {
ezoom.options.onShow(ezoom);
}
},
remove: function(op) {
ezoom.isShow = false;
$('#zoomModal #zoomModalImg').attr('src', '');
// $('#zoomModal').remove();
$('#zoomModal').hide();
ezoom.setTransformOrigin(1, null, 0, 0);
if (typeof ezoom.options.onClose && typeof ezoom.options.onClose == "function") {
ezoom.options.onClose(ezoom);
}
},
doRotate: function(to) {
const option = to == "left" ? "-90" : "+90";
ezoom.rotate = (ezoom.rotate + +option)%360;
ezoom.setTransformOrigin(1, null, 0, 0);
if (typeof ezoom.options.onRotate && typeof ezoom.options.onRotate == "function") {
ezoom.options.onRotate(ezoom);
}
},
reset: function() {
// scale-1 rotate-0 translateX-0 translateY-0
ezoom.setTransformOrigin(1, 0, 0, 0);
},
poscalSizeAndPosition: function(d) {
var $image = ezoom.thisImg;
var nWidth_ = $image[0].naturalWidth || +Infinity,
nHeight_ = $image[0].naturalHeight || +Infinity,
nWidth = d == 'odd' ? nHeight_ : nWidth_,
nHeight = d == 'odd' ? nWidth_ : nHeight_,
wWidth = $(window).width(),
wHeight = $(window).height(),
aWidth = Math.min(nWidth, wWidth * 0.98),
aHeight = Math.min(nHeight, wHeight * 0.98),
scaleX = aWidth / nWidth,
scaleY = aHeight / nHeight,
scale = Math.min(scaleX, scaleY);
// console.log('nheight,wHeight,aHeight',nHeight,wHeight,aHeight);
// console.log('nwidth,wWidth,aWidth',nWidth,wWidth,aWidth);
// $('#zoomModal').css({
// 'width': nWidth * scale,
// 'height': nHeight * scale
// });
// $('#zoomModal').css({
// 'height': $(window).height(),
// 'top': $(document).scrollTop()
// });
},
setTransformOrigin: function(s, r, x, y) {
if (s) ezoom.scale = s;
if (r != undefined || r != null ) ezoom.rotate = r;
if (x != undefined || x != null) ezoom.translateX = x;
if (y != undefined || y != null) ezoom.translateY = y;
var trans = 'scale(' + ezoom.scale + ') translateX(' + ezoom.translateX + 'px) translateY(' + ezoom.translateY + 'px) rotate(' + ezoom.rotate + 'deg)';
// console.log(trans);
ezoom.imgTag.css({
'-webkit-transform': trans,
'-moz-transform': trans,
'-ms-transform': trans,
'-o-transform': trans,
'transform': trans,
'transform-origin': 'center center'
});
},
initMouseScrolling: function() {
var isFirefox = navigator.userAgent.indexOf("Firefox") > -1 ;
var MOUSEWHEEL_EVENT = isFirefox ? "DOMMouseScroll" : "mousewheel";
// if(document.attachEvent) {
// ezoom.imgTag[0].attachEvent("on" + MOUSEWHEEL_EVENT, function(e) {
// mouseWheelScroll(e);
// });
// } else if(document.addEventListener) {
// ezoom.imgTag[0].addEventListener(MOUSEWHEEL_EVENT, function(e) {
// mouseWheelScroll(e);
// }, false);
// }
if (document.attachEvent) {
ezoom.zoomModal[0].attachEvent("on" + MOUSEWHEEL_EVENT, function(e) {
mouseWheelScroll(e);
});
} else if(document.addEventListener) {
ezoom.zoomModal[0].addEventListener(MOUSEWHEEL_EVENT, function(e) {
mouseWheelScroll(e);
}, false);
}
function mouseWheelScroll(e) {
ezoom._preventDefault(e);
var _delta = parseInt(e.wheelDelta || -e.detail);
if (_delta > 0) {
ezoom.scale += 0.1;
if (ezoom.scale >= 15) {
ezoom.scale = 15;
return;
}
ezoom.setTransformOrigin();
}
else {
ezoom.scale -= 0.1;
if (ezoom.scale <= 0.1) {
ezoom.scale = 0.1;
return;
}
ezoom.setTransformOrigin();
}
}
},
reposition: function() {
if (!ezoom.isShow) return;
ezoom.poscalSizeAndPosition();
},
initEvent: function() {
$(window).on('resize', ezoom.reposition);
$(document).on('scroll', ezoom.reposition);
ezoom.zoomModal.on('mousedown', function (e) {
e = e || window.event;
ezoom.grabbing = true;
if (!e) {
e = window.e;
ezoom.zoomModal.onselectstart = function () {
return false;
}
}
startX = e.clientX;
startY = e.clientY;
translateX_ = ezoom.translateX;
translateY_ = ezoom.translateY;
if (typeof ezoom.options.onMoveStarted && typeof ezoom.options.onMoveStarted == "function") {
ezoom.options.onMoveStarted(ezoom);
}
});
$(document).on('mouseup', function () {
ezoom.grabbing = false;
translateX_ = ezoom.translateX;
translateY_ = ezoom.translateY;
if (ezoom.options.onMovedCompleted && typeof ezoom.options.onMovedCompleted == "function") {
ezoom.options.onMovedCompleted(ezoom);
}
});
$(document).on('mousemove', function (e) {
e = e || window.event;
if (ezoom.grabbing) {
var nowX = e.clientX;
var nowY = e.clientY;
var disX = nowX - startX;
var disY = nowY - startY;
ezoom.translateX = translateX_ + disX;
ezoom.translateY = translateY_ + disY;
ezoom.setTransformOrigin();
ezoom._preventDefault();
if (typeof ezoom.options.onMoving && typeof ezoom.options.onMoving == "function") {
ezoom.options.onMoving(ezoom);
}
return false;
}
});
$(document).on('keydown', function(e) {
e = e || window.event;
ezoom._preventDefault(e);
if (e.keyCode && ezoom.isShow) {
// console.log(e.keyCode);
if (!ezoom.options.hideControlBtn) {
// Arrow direction key and the A D W S key
if(e.keyCode == 37 || e.keyCode == 65 ) { // left A
ezoom.setTransformOrigin(1, 270, 0, 0);
}
if(e.keyCode == 39 || e.keyCode == 68 ) { // right D
ezoom.setTransformOrigin(1, 90, 0, 0);
}
if(e.keyCode == 38 || e.keyCode == 87 ) { // top W
ezoom.setTransformOrigin(1, 0, 0, 0);
}
if(e.keyCode == 40 || e.keyCode == 83 ) { // down S
ezoom.setTransformOrigin(1, 180, 0, 0);
}
}
// ESC key
if (e.keyCode == 27) {
if (ezoom.isShow) {
if (ezoom.scale != 1 || ezoom.rotate != 0 || ezoom.translateX != 0 || ezoom.translateY != 0) {
ezoom.reset();
}
else {
ezoom.remove();
}
}
}
// R key
if (e.keyCode == 82) {
ezoom.doRotate("right");
}
}
});
},
// Prevent the default scroll event of the up and down keys
_preventDefault: function(e) {
if (e && e.preventDefault) {
e.preventDefault();
}
else {
window.event.returnValue = false;
}
return false;
}
}