jquery.artZoom_wev8.js
11.8 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
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
/*
* artZoom 1.0.7
* Date: 2011-06-22
* (c) 2009-2011 TangBin, http://www.planeArt.cn
*
* This is licensed under the GNU LGPL, version 2.1 or later.
* For details, see: http://creativecommons.org/licenses/LGPL/2.1/
*/
(function (document, $, log) {
$.fn.artZoom = function (config) {
config = $.extend({}, $.fn.artZoom.defaults, config);
var tmpl, viewport,
$this = this,
loadImg = {},
path = config.path,
loading = path + '/loading_wev8.gif',
max = path + '/zoomin.cur',
min = path + '/zoomout.cur';
new Image().src = loading;
max = 'url(\'' + max + '\'), pointer';
min = 'url(\'' + min + '\'), pointer';
tmpl = [
'<div class="ui-artZoom-toolbar" style="display:none">',
'<span class="ui-artZoom-buttons" style="display:none">',
'<a href="#" data-go="left" class="ui-artZoom-left"><span></span>',
config.left,
'</a>',
'<a href="#" data-go="right" class="ui-artZoom-right"><span></span>',
config.right,
'</a>',
'<a href="#" data-go="source" class="ui-artZoom-source"><span></span>',
config.source,
'</a>',
'<a href="#" data-go="hide" class="ui-artZoom-hide"><span></span>',
config.hide,
'</a>',
'</span>',
'<span class="ui-artZoom-loading">',
'<img data-live="stop" src="',
loading,
'" style="',
'display:inline-block;*zoom:1;*display:inline;vertical-align:middle;',
'width:16px;height:16px;"',
' />',
' <span>Loading..</span>',
'</span>',
'</div>',
'<div class="ui-artZoom-box" style="display:none">',
'<span class="ui-artZoom-photo" data-go="hide"',
' style="display:inline-block;*display:inline;*zoom:1;overflow:hidden;position:relative;cursor:',
min,
'">',
'<img data-name="thumb" data-go="hide" data-live="stop" src="',
loading,
'" />',
'</span>',
'</div>'
].join('');
// jQuery浜嬩欢浠g悊
this.live('click', function (event) {
if (this.nodeName !== 'IMG' && this.getAttribute('data-live') === 'stop') return false;
var $artZoom, buttonClick,
that = this,
$this = $(that),
$parent = $this.parent(),
src = that.src,
show = $this.attr('data-artZoom-show') || src,
source = $this.attr('data-artZoom-source') || show,
maxWidth = config.maxWidth || ($parent[0].nodeName === 'A' ? $this.parent() : $this).parent().width(),
maxHeight = config.maxHeight || 99999;
maxWidth = maxWidth - config.borderWidth;
// 瀵瑰寘鍚湪閾炬帴鍐呯殑鍥剧墖杩涜鐗规畩澶勭悊
if ($parent[0].nodeName === 'A') {
show = $parent.attr('data-artZoom-show') || $parent.attr('href');
source = $parent.attr('data-artZoom-source') || $parent.attr('rel');
};
// 绗竴娆$偣鍑?
if (!$this.data('artZoom')) {
var wrap = document.createElement('div'),
$thumb, $box, $show;
$artZoom = $(wrap);
wrap.className = 'ui-artZoom ui-artZoom-noLoad';
wrap.innerHTML = tmpl;
($parent[0].nodeName === 'A' ? $this.parent() : $this).before(wrap);
$this.data('artZoom', $artZoom);
$box = $artZoom.find('.ui-artZoom-box');
$thumb = $artZoom.find('[data-name=thumb]');
// 蹇€熻幏鍙栧ぇ鍥惧昂瀵?
imgReady(show, function () {
var width = this.width,
height = this.height,
maxWidth2 = Math.min(maxWidth, width);
height = maxWidth2 / width * height;
width = maxWidth2;
// 鎻掑叆澶у浘骞朵娇鐢ㄩ€愭笎娓呮櫚鍔犺浇鐨勬晥鏋?
$thumb.attr('src', src).
css(config.blur ? {
width: width + 'px',
height: height + 'px'
} : {display: 'none'}).
after([
'<img class="ui-artZoom-show" title="',
that.title,
'" alt="',
that.alt,
'" src="',
show,
'" style="width:',
width,
'px;height:',
height, // IE8 瓒呴暱鍥剧墖height灞炴€уけ鏁圔UG锛屾敼鐢–SS
'px;position:absolute;left:0;top:0;background:transparent"',
' />'
].join(''));
$show = $artZoom.find('.ui-artZoom-show');
$thumb.attr('class', 'ui-artZoom-show');
$artZoom.addClass('ui-artZoom-ready');
$artZoom.find('.ui-artZoom-buttons').show();
$this.data('artZoom-ready', true);
$this.hide();
$box.show();
// 澶у浘瀹屽叏鍔犺浇瀹屾瘯
}, function () {
$thumb.removeAttr('class').hide();
$show.css({
position: 'static',
left: 'auto',
top: 'auto'
});
$artZoom.removeClass('ui-artZoom-noLoad');
$artZoom.find('.ui-artZoom-loading').hide();
$this.data('artZoom-load', true);
// 鍥剧墖鍔犺浇閿欒
}, function () {
$artZoom.addClass('ui-artZoom-error');
log('jQuery.fn.artZoom: Load "' + show + '" Error!');
});
} else {
$this.hide();
};
$artZoom = $this.data('artZoom');
buttonClick = function (event) {
var target = this,
go = target.getAttribute('data-go'),
live = target.getAttribute('data-live'),
degree = $this.data('artZoom-degree') || 0,
elem = $artZoom.find('.ui-artZoom-show')[0];
if (live === 'stop') return false;
if (/img|canvas$/i.test(target.nodeName)) go = 'hide';
switch (go) {
case 'left':
degree -= 90;
degree = degree === -90 ? 270 : degree;
break;
case 'right':
degree += 90;
degree = degree === 360 ? 0 : degree;
break;
case 'source':
window.open(source || show || src);
break;
case 'hide':
$this.show();
$artZoom.find('.ui-artZoom-toolbar').hide();
$artZoom.hide();
$artZoom.find('[data-go]').die('click', buttonClick);
break;
};
if ((go === 'left' || go === 'right') && $this.data('artZoom-load')) {
imgRotate(elem, degree, maxWidth, maxHeight);
$this.data('artZoom-degree', degree);
};
return false;
};
$artZoom.show().find('.ui-artZoom-toolbar').slideDown(150);
$artZoom.find('[data-go]').live('click', buttonClick);
return false;
});
// 缁欑洰鏍囩缉鐣ュ浘搴旂敤澶栭儴鎸囬拡鏍峰紡
this.live('mouseover', function () {
if (this.className !== 'ui-artZoom-show') this.style.cursor = max;
});
// 棰勫姞杞芥寚閽堝舰鐘跺浘鏍?
if (this[0]) this[0].style.cursor = max;
return this;
};
$.fn.artZoom.defaults = {
path: './images',
left: '\u5de6\u65cb\u8f6c',
right: '\u53f3\u65cb\u8f6c',
source: '\u770b\u539f\u56fe',
hide: '\xd7',
blur: true,
preload: true,
maxWidth: null,
maxHeight: null,
borderWidth: 18
};
/**
* 鍥剧墖鏃嬭浆
* @version 2011.05.27
* @author TangBin
* @param {HTMLElement} 鍥剧墖鍏冪礌
* @param {Number} 鏃嬭浆瑙掑害 (鍙敤鍊? 0, 90, 180, 270)
* @param {Number} 鏈€澶у搴﹂檺鍒?
* @param {Number} 鏈€澶ч珮搴﹂檺鍒?
*/
var imgRotate = $.imgRotate = function () {
var eCanvas = '{$canvas}',
isCanvas = !!document.createElement('canvas').getContext;
return function (elem, degree, maxWidth, maxHeight) {
var x, y, getContext,
resize = 1,
width = elem.naturalWidth,
height = elem.naturalHeight,
canvas = elem[eCanvas];
// 鍒濇杩愯
if (!elem[eCanvas]) {
// 鑾峰彇鍥惧儚鏈簲鐢ㄦ牱寮忕殑鐪熷疄澶у皬 (IE鍜孫pera鏃╂湡鐗堟湰)
if (!('naturalWidth' in elem)) {
var run = elem.runtimeStyle, w = run.width, h = run.height;
run.width = run.height = 'auto';
elem.naturalWidth = width = elem.width;
elem.naturalHeight = height = elem.height;
run.width = w;
run.height = h;
};
elem[eCanvas] = canvas = document.createElement(isCanvas ? 'canvas' : 'span');
elem.parentNode.insertBefore(canvas, elem.nextSibling);
elem.style.display = 'none';
canvas.className = elem.className;
canvas.title = elem.title;
if (!isCanvas) {
canvas.img = document.createElement('img');
canvas.img.src = elem.src;
canvas.appendChild(canvas.img);
canvas.style.cssText = 'display:inline-block;*zoom:1;*display:inline;' +
// css reset
'padding:0;margin:0;border:none 0;position:static;float:none;overflow:hidden;width:auto;height:auto';
};
};
var size = function (isSwap) {
if (isSwap) width = [height, height = width][0];
if (width > maxWidth) {
resize = maxWidth / width;
height = resize * height;
width = maxWidth;
};
if (height > maxHeight) {
resize = resize * maxHeight / height;
width = maxHeight / height * width;
height = maxHeight;
};
if (isCanvas) (isSwap ? height : width) / elem.naturalWidth;
};
switch (degree) {
case 0:
x = 0;
y = 0;
size();
break;
case 90:
x = 0;
y = -elem.naturalHeight;
size(true);
break;
case 180:
x = -elem.naturalWidth;
y = -elem.naturalHeight
size();
break;
case 270:
x = -elem.naturalWidth;
y = 0;
size(true);
break;
};
if (isCanvas) {
canvas.setAttribute('width', width);
canvas.setAttribute('height', height);
getContext = canvas.getContext('2d');
getContext.rotate(degree * Math.PI / 180);
getContext.scale(resize, resize);
getContext.drawImage(elem, x, y);
} else {
canvas.style.width = width + 'px';
canvas.style.height = height + 'px';// 瑙e喅IE8浣跨敤婊ら暅鍚庨珮搴︿笉鑳借嚜閫傚簲
canvas.img.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(rotation=' + degree / 90 + ')';
canvas.img.width = elem.width * resize;
canvas.img.height = elem.height * resize;
};
};
}();
/**
* 鍥剧墖澶存暟鎹姞杞藉氨缁簨浠?- 鏇村揩鑾峰彇鍥剧墖灏哄
* @version 2011.05.27
* @author TangBin
* @see http://www.planeart.cn/?p=1121
* @param {String} 鍥剧墖璺緞
* @param {Function} 灏哄灏辩华
* @param {Function} 鍔犺浇瀹屾瘯 (鍙€?
* @param {Function} 鍔犺浇閿欒 (鍙€?
* @example imgReady('http://www.google.com.hk/intl/zh-CN/images/logo_cn_wev8.png', function () {
alert('size ready: width=' + this.width + '; height=' + this.height);
});
*/
var imgReady = (function () {
var list = [], intervalId = null,
// 鐢ㄦ潵鎵ц闃熷垪
tick = function () {
var i = 0;
for (; i < list.length; i++) {
list[i].end ? list.splice(i--, 1) : list[i]();
};
!list.length && stop();
},
// 鍋滄鎵€鏈夊畾鏃跺櫒闃熷垪
stop = function () {
clearInterval(intervalId);
intervalId = null;
};
return function (url, ready, load, error) {
var onready, width, height, newWidth, newHeight,
img = new Image();
img.src = url;
// 濡傛灉鍥剧墖琚紦瀛橈紝鍒欑洿鎺ヨ繑鍥炵紦瀛樻暟鎹?
if (img.complete) {
ready.call(img);
load && load.call(img);
return;
};
width = img.width;
height = img.height;
// 鍔犺浇閿欒鍚庣殑浜嬩欢
img.onerror = function () {
error && error.call(img);
onready.end = true;
img = img.onload = img.onerror = null;
};
// 鍥剧墖灏哄灏辩华
onready = function () {
newWidth = img.width;
newHeight = img.height;
if (newWidth !== width || newHeight !== height ||
// 濡傛灉鍥剧墖宸茬粡鍦ㄥ叾浠栧湴鏂瑰姞杞藉彲浣跨敤闈㈢Н妫€娴?
newWidth * newHeight > 1024
) {
ready.call(img);
onready.end = true;
};
};
onready();
// 瀹屽叏鍔犺浇瀹屾瘯鐨勪簨浠?
img.onload = function () {
// onload鍦ㄥ畾鏃跺櫒鏃堕棿宸寖鍥村唴鍙兘姣攐nready蹇?
// 杩欓噷杩涜妫€鏌ュ苟淇濊瘉onready浼樺厛鎵ц
!onready.end && onready();
load && load.call(img);
// IE gif鍔ㄧ敾浼氬惊鐜墽琛宱nload锛岀疆绌簅nload鍗冲彲
img = img.onload = img.onerror = null;
};
// 鍔犲叆闃熷垪涓畾鏈熸墽琛?
if (!onready.end) {
list.push(onready);
// 鏃犺浣曟椂鍙厑璁稿嚭鐜颁竴涓畾鏃跺櫒锛屽噺灏戞祻瑙堝櫒鎬ц兘鎹熻€?
if (intervalId === null) intervalId = setInterval(tick, 40);
};
};
})();
}(document, jQuery, function (msg) {window.console && console.log(msg)}));