-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagnify.js
160 lines (135 loc) · 6.41 KB
/
magnify.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
(function ($, window, document) {
var Magnifier = {
magnifier: 0,
glass: 0,
zoom: 1.5,
coverFactor: 1,
nativeW: 0,
nativeH: 0,
CoverOffsetX: 0,
CoverOffsetY: 0,
backgroundSize: "cover", // COVER o CONTAIN
enabled: true,
/* standard selection */
selectors: {
glass_class: 'glass'
, glass: '.glass'
, thumb: '.thumb'
, active_class: 'active'
},
init: function (el) {
//add glass to each magnifier
Magnifier.glass = $('<div></div>').addClass(Magnifier.selectors.glass_class);
// get the native image size of each magnifier source image
Magnifier.magnifier = $(el);
var $thumb = Magnifier.magnifier.find(Magnifier.selectors.thumb);
Magnifier.reInit();
$thumb.before(Magnifier.glass.css({
'background-image': 'url(' + $thumb.attr('src') + ')'
}));
// EVENTI
Magnifier.magnifier.on('mousemove', Magnifier.behaviors.mousemove);
$(window).on('resize', Magnifier.behaviors.resize);
$(document).keydown(function(e) {
if (e.keyCode == 27) { // escape key (keycode '27')
Magnifier.enabled = false;
Magnifier.behaviors.fadeOut();
}
});
$thumb.on('mouseleave', function () {
Magnifier.behaviors.fadeOut();
});
return Magnifier;
},
reInit: function () {
var $thumb = Magnifier.magnifier.find(Magnifier.selectors.thumb);
// +++
Magnifier.backgroundSize = $thumb.css('background-size');
var image_object = new Image();
image_object.src = $thumb.attr("src");
// onload img
image_object.onload = function () {
//calcolo per le dimensioni dell'immagine con COVER
var nativeWidth = image_object.width;
var nativeHeight = image_object.height;
Magnifier.nativeW = nativeWidth;
Magnifier.nativeH = nativeHeight;
var thumbWidth = $thumb.width();
var thumbHeight = $thumb.height();
var coverFactor = Magnifier.backgroundSize=="cover" ? Math.max(thumbWidth / nativeWidth, thumbHeight / nativeHeight) : Math.min(thumbWidth / nativeWidth, thumbHeight / nativeHeight);
Magnifier.coverFactor = coverFactor;
var finalWidth = nativeWidth * coverFactor * Magnifier.zoom;
var finalHeight = nativeHeight * coverFactor * Magnifier.zoom;
// offset impostato dal COVER center center
Magnifier.CoverOffsetX = ($thumb.width() - nativeWidth * coverFactor) / 2;
Magnifier.CoverOffsetY = ($thumb.height() - nativeHeight * coverFactor) / 2;
Magnifier.glass.css('background-size', finalWidth + 'px ' + finalHeight + 'px');
}
Magnifier.glass.css('background-image', 'url(' + $thumb.attr('src') + ')');
return Magnifier;
},
setZoom: function(z) {
Magnifier.zoom = z;
Magnifier.reInit();
},
behaviors: {
fadeDelay: 300,
/* fade in/out glass overlay if mouse is outside container */
isHover: function (cw, ch, mx, my) {
return ((mx < cw && my < ch && mx > 0 && my > 0) && !(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)));
},
fadeOut: function () {
Magnifier.glass.fadeOut(Magnifier.behaviors.fadeDelay);
}
,
mousemove: function (e) {
if (!Magnifier.enabled) return;
var $magnifier = $(this)
, offset = $magnifier.offset() // relative position
, mx = e.pageX - offset.left // relative to mouse
, my = e.pageY - offset.top // relative to mouse
, $glass = $magnifier.find(Magnifier.selectors.glass)
, $thumb = $magnifier.find(Magnifier.selectors.thumb)
, rx, ry, bgp // relative ratios
, glass_width = $glass.width()
, glass_height = $glass.height()
;
if (Magnifier.behaviors.isHover($magnifier.width(), $magnifier.height(), mx, my)) {
$glass.fadeIn(Magnifier.behaviors.fadeDelay); // show glass
//The background position of .glass will be changed according to the position
//of the mouse over the .small image. So we will get the ratio of the pixel
//under the mouse pointer with respect to the image and use that to position the
//large image inside the magnifying glass
Rx = -Math.round((mx) * Magnifier.nativeW * Magnifier.coverFactor * Magnifier.zoom / $thumb.width() - glass_width / 2);
Ry = -Math.round((my) * Magnifier.nativeH * Magnifier.coverFactor * Magnifier.zoom / $thumb.height() - glass_height / 2);
var cx = mx * 2 * Magnifier.CoverOffsetX / $thumb.width() - Magnifier.CoverOffsetX;
var cy = my * 2 * Magnifier.CoverOffsetY / $thumb.height() - Magnifier.CoverOffsetY;
bgp = (Rx - cx) + "px " + (Ry - cy) + "px";
$glass.css({
left: mx - glass_width / 2
, top: my - glass_height / 2
, backgroundPosition: bgp
});
}//-- if visible
else {
// hide
Magnifier.behaviors.fadeOut();
}
},//-- fn mousemove
resize: function () {
Magnifier.reInit();
}
}
};////---- Magnifier
$.fn.magnifier = function () {
return this.each(function () {
if ($(this).data("magnify-init") === true) {
return false;
}
$(this).data("magnify-init", true);
var magnifier = Object.create(Magnifier);
var M = magnifier.init(this);
$(this).data("magnifier", M);
});
};
}(jQuery, window, document));