-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_zoom.js
235 lines (131 loc) · 4.77 KB
/
my_zoom.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
var incrementScale = .3;
var map = [];
var back_map = [];
var $container = $("#container-zoom");
var $image = $("#map-image");
var $parent = $("#range");
function resetZoomDefault() {
$container.css('top', 'auto');
$container.css('left', 'auto');
$container.css('width', 'auto');
$container.css('height', 'auto');
$image.css('top', 'auto');
$image.css('left', 'auto');
$image.attr("data-zoom", 0);
map = [];
back_map = [];
}
function initializesZoomImage() {
resetZoomDefault();
var dwidth = $parent.width();
if (dwidth === null) {} else {
w = document.querySelector('#map-image').naturalWidth;
h = document.querySelector('#map-image').naturalHeight;
var nuevo_a = Math.round(dwidth * h / w);
$parent.css("height", nuevo_a);
$image.css("height", nuevo_a);
$image.css("width", dwidth);
}
}
function minus_zoom() {
var dwidth = $parent.width();
naturalSizeImage();
nuevo_a = Math.round(dwidth * h / w);
var count_zoom = $image.attr("data-zoom");
if (count_zoom <= 0) {
var t_zoom = parseFloat(count_zoom);
} else {
var t_zoom = parseFloat(count_zoom) - 1;
$image.attr("data-zoom", t_zoom);
var count_zoom2 = $image.attr("data-zoom");
// var dwidth = $parent.width();
var f_s = t_zoom * incrementScale;
var first = dwidth * f_s / 1 + dwidth;
var second = nuevo_a * f_s / 1 + nuevo_a;
$image.css('width', first);
$image.css('height', second);
activateZoom(first, second);
if (back_map.length == 4) {
$image.css('left', back_map[0]);
$image.css('top', back_map[1]);
}
if (back_map.length > 4) {
c1 = count_zoom * 2 - 4;
c2 = count_zoom * 2 - 3;
$image.css('left', back_map[c1]);
$image.css('top', back_map[c2]);
}
if (count_zoom2 == 0) {
$image.css('left', '0px');
$image.css('top', '0px');
resetZoomDefault();
}
} //if
}
function more_zoom() {
var dwidth = $parent.width();
naturalSizeImage();
nuevo_a = Math.round(dwidth * h / w);
count_zoom = $image.attr("data-zoom");
t_zoom = parseFloat(count_zoom) + 1;
$image.attr("data-zoom", t_zoom);
first = dwidth * (t_zoom * incrementScale) / 1 + dwidth;
second = nuevo_a * (t_zoom * incrementScale) / 1 + nuevo_a;
$image.css('width', first);
$image.css('height', second);
activateZoom(first, second, 'more');
}
function activateZoom(w, h, type) {
dwidth = $parent.width();
dheight = $parent.height();
x_w = w * 2 - dwidth;
x_h = h * 2 - dheight;
primer_p = w - dwidth;
segund_p = h - dheight;
h_h = h - dheight;
w_w = w - dwidth;
map.push(w_w);
map.push(h_h);
position = $image.position();
count_zoom = $image.attr("data-zoom");
if (type == 'more') {
m_w = count_zoom * map[0]; //total anch
m_h = count_zoom * map[1]; //total alto
aplicado_a = m_w - map[1];
porcentaje_alto = aplicado_a - position.top * 100;
f_h = porcentaje_alto / aplicado_a;
aplicado_alto = m_w * f_h / 100;
aplicado_l = m_h - map[1];
porcentaje_ancho = aplicado_l - position.left * 100;
f_h = porcentaje_ancho / aplicado_l;
aplicado_ancho = m_h * f_h / 100;
if (aplicado_ancho < 0) { aplicado_ancho = -1 * aplicado_ancho; }
if (aplicado_alto < 0) { aplicado_alto = -1 * aplicado_alto; }
if (back_map.length >= 2) {
$image.css('top', aplicado_alto);
$image.css('left', aplicado_ancho);
back_map.push(aplicado_ancho);
back_map.push(aplicado_alto);
} else {
back_map.push(w_w / 2);
back_map.push(h_h / 2);
$image.css('top', h_h / 2);
$image.css('left', w_w / 2);
}
}
$container.css('top', -1 * segund_p);
$container.css('left', -1 * primer_p);
$container.css('width', x_w);
$container.css('height', x_h);
$image.draggable({
containment: $('#container-zoom')
});
}
function naturalSizeImage () {
return w = document.querySelector('#map-image').naturalWidth;
return h = document.querySelector('#map-image').naturalHeight;
}
$(document).ready(function() {
initializesZoomImage();
});
window.addEventListener("resize", initializesZoomImage);