-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathmain.js
More file actions
91 lines (44 loc) · 2.65 KB
/
main.js
File metadata and controls
91 lines (44 loc) · 2.65 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
const magnifiedImageContainer = document.querySelector(".magnifiedImage");
const thumbnailImage = document.querySelector(".thumbnailImage");
let thumbnailRectangle = thumbnailImage.getBoundingClientRect();
let magnifiedImageRect = magnifiedImageContainer.getBoundingClientRect();
thumbnailImage.addEventListener("mousemove",onMouseMove);
thumbnailImage.addEventListener("mouseout",onMouseOut);
thumbnailImage.addEventListener("mouseenter",onMouseEnter);
let magnifiedImage;
function onMouseEnter() {
let imgElement = this.querySelector("img");
magnifiedImage = document.createElement("img");
magnifiedImage.src = imgElement.src;
while (magnifiedImageContainer.firstChild) {
magnifiedImageContainer.removeChild(magnifiedImageContainer.firstChild);
}
magnifiedImageContainer.appendChild(magnifiedImage);
}
function onMouseMove (event) {
let magnifierWidth = (magnifiedImageRect.width/magnifiedImage.width)*thumbnailRectangle.width;
let magnifierHeight = (magnifiedImageRect.height/magnifiedImage.height)*thumbnailRectangle.height;
magnifier.style.width = magnifierWidth +"px";
magnifier.style.height = magnifierHeight +"px";
magnifier.style.left = thumbnailRectangle.left +"px";
magnifier.style.top = thumbnailRectangle.top +"px";
magnifiedImageContainer.style.visibility = "visible";
magnifier.style.visibility = "visible";
const mouseX = event.clientX-thumbnailRectangle.left;
const mouseY = event.clientY-thumbnailRectangle.top;
let xPercent = (mouseX/thumbnailRectangle.width)*100;
let yPercent = (mouseY/thumbnailRectangle.height)*100;
let magnifierBoundaryX = 0.5 * (magnifierWidth/thumbnailRectangle.width)*100;
let magnifierBoundaryY = 0.5 * (magnifierHeight/thumbnailRectangle.height)*100;
xPercent = Math.min(Math.max(0,(xPercent-magnifierBoundaryX)/(100-2*magnifierBoundaryX)*100),100);
yPercent = Math.min(Math.max(0,(yPercent-magnifierBoundaryY)/(100-2*magnifierBoundaryY)*100),100);
magnifier.style.left=Math.min(Math.max(thumbnailRectangle.left,event.clientX-magnifierWidth/2),thumbnailRectangle.left+thumbnailRectangle.width-magnifierWidth)+"px";
magnifier.style.top=Math.min(Math.max(thumbnailRectangle.top,event.clientY-magnifierHeight/2),thumbnailRectangle.top+thumbnailRectangle.height-magnifierHeight)+"px";
const translateX = (xPercent/100)*(magnifiedImage.offsetWidth-magnifiedImageRect.width);
const translateY = (yPercent/100)*(magnifiedImage.offsetHeight-magnifiedImageRect.height);
magnifiedImage.style.transform=`translate(${-translateX}px,${-translateY}px)`;
}
function onMouseOut() {
magnifiedImageContainer.style.visibility="hidden";
magnifier.style.visibility="hidden";
}