-
Notifications
You must be signed in to change notification settings - Fork 1
/
doublezoom.js
115 lines (101 loc) · 3.35 KB
/
doublezoom.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
(function ($) {
$.fn.doublezoom = function () {
/* define working variables */
var stop1 = false,
stop2 = true;
var img = $(this);
/* Create lens: */
var jLens = jQuery("<div>", {
class: "overlay",
title: "Lens overlay",
});
/* Insert lens: */
img.parent().parent().append(jLens);
/* get the result DIV images */
var resultLeft = img
.parent()
.parent()
.parent()
.find(".doublezoom-landing-left");
var resultRight = img
.parent()
.parent()
.parent()
.find(".doublezoom-landing-right");
/* Calculate the ratio between result DIV and lens: */
/* Left ratios */
var cxl = resultLeft.outerWidth() / jLens.outerWidth();
var cxr = resultRight.outerWidth() / jLens.outerWidth();
/* Right ratios */
var cyl = resultLeft.outerHeight() / jLens.outerHeight();
var cyr = resultRight.outerHeight() / jLens.outerHeight();
/* set the result DIV images */
resultLeft.css("background-image", "url(" + img.attr("src") + ")");
resultRight.css("background-image", "url(" + img.attr("src") + ")");
/* set the dimensions of the result DIV images */
resultLeft.css(
"background-size",
img.width() * cxl + "px " + img.height() * cyl + "px"
);
resultRight.css(
"background-size",
img.width() * cxr + "px " + img.height() * cyr + "px"
);
/* Add event listeners for mousemove and click events */
$(this).mousemove(function (e) {
move(e);
});
$(jLens).mousemove(function (e) {
move(e);
});
$(this).click(function (e) {
trottle(e);
});
$(jLens).click(function (e) {
trottle(e);
});
/* mouse move */
function move(e) {
/* calculare x and y for lens movement*/
var x = e.pageX - img.offset().left - jLens.width() / 2;
var y = e.pageY - img.offset().top - jLens.height() / 2;
/* set lens border limits */
if (x < 0) {
x = 0;
}
if (y < 0) {
y = 0;
}
if (x > img.width() - jLens.outerWidth()) {
x = img.width() - jLens.outerWidth();
}
if (y > img.height() - jLens.outerHeight()) {
y = img.height() - jLens.outerHeight();
}
/* Set the position of the lens: */
jLens.css({ top: y, left: x });
/* handle clicks */
if (!stop1) {
resultLeft.css(
"background-position",
"-" + x * cxl + "px -" + y * cyl + "px"
);
}
if (!stop2) {
resultRight.css(
"background-position",
"-" + x * cxr + "px -" + y * cyr + "px"
);
}
}
/* handle stop'n'go on mouse click */
function trottle(e) {
if (stop2) {
stop1 = !stop1;
}
if (stop1) {
stop2 = !stop2;
}
}
};
})(jQuery);