Repository navigation
Expand file tree
/
Copy pathimgView.js
More file actions
100 lines (85 loc) · 2.92 KB
/
Copy pathimgView.js
File metadata and controls
100 lines (85 loc) · 2.92 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
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
function warpImg() {
// Define CSS for full-screen zoom viewer
const zoomStyle = `
.zoom-container {
display: inline-block;
position: relative;
cursor: zoom-in;
}
.zoom-overlay {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
background-color: rgba(0, 0, 0, 0.8);
display: flex;
justify-content: center;
align-items: center;
transform: scale(0);
transition: transform 0.3s ease;
z-index: 9999; /* Ensure it stays on top */
}
.zoom-overlay img {
max-width: 90%;
max-height: 90%;
}
`;
const styleTag = document.createElement('style');
styleTag.innerHTML = zoomStyle;
document.head.appendChild(styleTag);
// Get the width of the article container
const article = document.querySelector('article');
const articleWidth = article.offsetWidth;
// Add event listeners to each image element
document.querySelectorAll('article img').forEach(img => {
const container = document.createElement('div');
container.classList.add('zoom-container');
img.onload = () => {
img.style.display = 'block';
};
// Set the width of zoom-container to the image's natural width
img.addEventListener('load', () => {
container.style.width = Math.min(articleWidth / 4, img.naturalWidth) + 'px';
});
const overlay = document.createElement('div');
overlay.classList.add('zoom-overlay');
const zoomImg = document.createElement('img');
zoomImg.src = img.src;
overlay.appendChild(zoomImg);
container.appendChild(overlay);
img.parentNode.insertBefore(container, img);
container.appendChild(img);
// Image click event to show the full-screen overlay
img.addEventListener('click', () => {
overlay.style.transform = 'scale(1)';
});
// Click event on the overlay to hide it
overlay.addEventListener('click', () => {
overlay.style.transform = 'scale(0)';
});
});
}
function resizeOversizedImages(img, articleWidth) {
// Apply max-width to resize the image to fit the viewport
img.style.maxWidth = '100%';
img.style.height = 'auto'; // Maintain aspect ratio
}
function initImageResize() {
// Get the width of the article container
const article = document.querySelector('article');
if (!article) return;
const articleWidth = article.offsetWidth;
// Loop through each image inside the article container
document.querySelectorAll('article img').forEach(img => {
// Check if the image is already loaded
if (img.complete) {
resizeOversizedImages(img, articleWidth); // Resize if the image is already loaded
} else {
// Add an event listener to resize when the image is fully loaded
img.addEventListener('load', () => {
resizeOversizedImages(img, articleWidth);
});
}
});
}