-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzoom copy.html
156 lines (146 loc) · 4.87 KB
/
zoom copy.html
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
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style>
* {
box-sizing: border-box;
}
.img-zoom-container {
position: relative;
}
.img-zoom-lens {
position: absolute;
border: 1px solid #d4d4d4;
/*set the size of the lens:*/
width: 40px;
height: 40px;
border-radius: 50%;
background-color: rgba(0, 0, 0, 0.301);
}
.img-zoom-result {
position: absolute;
width: 300px;
height: 300px;
border: 1px solid #d4d4d4;
border-radius: 10px;
}
.img-zoom-result.on {
animation: aparece 0.5s;
}
@keyframes aparece {
to {
opacity: 1;
}
from {
opacity: 0;
}
}
</style>
<script>
function imageZoom(imgID, resultID) {
var img, lens, result, cx, cy;
img = document.getElementById(imgID);
result = document.getElementById(resultID);
/*create lens:*/
lens = document.createElement("DIV");
lens.setAttribute("class", "img-zoom-lens");
/*insert lens:*/
img.parentElement.insertBefore(lens, img);
/*calculate the ratio between result DIV and lens:*/
cx = result.offsetWidth / lens.offsetWidth;
cy = result.offsetHeight / lens.offsetHeight;
/*set background properties for the result DIV:*/
result.style.backgroundImage = "url('" + img.src + "')";
result.style.backgroundSize =
img.width * cx + "px " + img.height * cy + "px";
result.style.display = "none";
/*execute a function when someone moves the cursor over the image, or the lens:*/
lens.addEventListener("mousemove", moveLens);
img.addEventListener("mousemove", moveLens);
lens.addEventListener("mouseout", () => {
result.style.display = "none";
lens.style.display = "none";
});
/*and also for touch screens:*/
lens.addEventListener("touchmove", moveLens);
img.addEventListener("touchmove", moveLens);
lens.addEventListener("touchend", () => {
result.classList.add("on");
lens.style.display = "none";
});
function moveLens(e) {
result.style.display = "block";
lens.style.display = "block";
var pos, x, y;
/*prevent any other actions that may occur when moving over the image:*/
e.preventDefault();
/*get the cursor's x and y positions:*/
pos = getCursorPos(e);
/*calculate the position of the lens:*/
x = pos.x - lens.offsetWidth / 2;
y = pos.y - lens.offsetHeight / 2;
/*prevent the lens from being positioned outside the image:*/
if (x > img.width - lens.offsetWidth) {
x = img.width - lens.offsetWidth;
}
if (x < 0) {
x = 0;
}
if (y > img.height - lens.offsetHeight) {
y = img.height - lens.offsetHeight;
}
if (y < 0) {
y = 0;
}
/*set the position of the lens:*/
lens.style.left = x + "px";
lens.style.top = y + "px";
result.style.left = x + 35 + "px";
result.style.top = y + 35 + "px";
/*display what the lens "sees":*/
result.style.backgroundPosition =
"-" + x * cx + "px -" + y * cy + "px";
}
function getCursorPos(e) {
var a,
x = 0,
y = 0;
e = e || window.event;
/*get the x and y positions of the image:*/
a = img.getBoundingClientRect();
/*calculate the cursor's x and y coordinates, relative to the image:*/
x = e.pageX - a.left;
y = e.pageY - a.top;
/*consider any page scrolling:*/
x = x - window.pageXOffset;
y = y - window.pageYOffset;
return { x: x, y: y };
}
}
</script>
</head>
<body>
<h1>Image Zoom</h1>
<p>Mouse over the image:</p>
<div class="img-zoom-container">
<img id="myimage" src="./img/protuto-2.jpg" width="300" height="240" />
<div id="myresult" class="img-zoom-result"></div>
</div>
<p>
The image must be placed inside a container with relative positioning.
</p>
<p>
The result can be put anywhere on the page, but must have the class name
"img-zoom-result".
</p>
<p>
Make sure both the image and the result have IDs. These IDs are used when
a javaScript initiates the zoom effect.
</p>
<script>
// Initiate zoom effect:
imageZoom("myimage", "myresult");
</script>
</body>
</html>