Skip to content

Commit 21070e0

Browse files
Add Spinner and on image click
1 parent 9e6f861 commit 21070e0

1 file changed

Lines changed: 76 additions & 19 deletions

File tree

‎reader/index.html‎

Lines changed: 76 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
<button class="btn btn-dark" id="view-image-btn" type="button">Go</button>
2525
<a class="btn btn-dark mx-2" href="" target="_blank" type="button">Clone Reader</a>
2626
</div>
27-
27+
<!-- Loading Spinner (Hidden by default) -->
28+
<div class="spinner-border mx-2" role="status" style="display: none;">
29+
<span class="visually-hidden">Loading...</span>
30+
</div>
2831
</div>
2932
</div>
3033

@@ -33,6 +36,8 @@
3336
<!-- Image Display Section -->
3437
<div class="container mt-2 d-flex justify-content-center px-1">
3538
<img id="image-display" src="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==">
39+
<!-- Overlay for click detection -->
40+
<!-- <div class="overlay" style="position: absolute; top: 0; left: 0; right: 0; bottom: 0; cursor: pointer;"></div> -->
3641
</div>
3742

3843
<!-- Pagination Section -->
@@ -71,12 +76,29 @@
7176
const prevBtn = document.getElementById('prev-btn');
7277
const nextBtn = document.getElementById('next-btn');
7378
const lastBtn = document.getElementById('last-btn');
74-
79+
7580
let currentPage = 1;
7681
let maxPage = 1;
7782
let baseUrl = '';
7883
let extension = '';
79-
84+
85+
// Event listener for clicks on the overlay
86+
imageDisplay.addEventListener('click', function(event) {
87+
const imageWidth = imageDisplay.offsetWidth;
88+
89+
// Calculate the click position relative to the image width
90+
const clickPosition = event.clientX - imageDisplay.getBoundingClientRect().left;
91+
92+
// If click is in the left 20% of the image, go to the previous page
93+
if (clickPosition < imageWidth * 0.2) {
94+
navigateToPage(currentPage - 1); // Handle navigation to the previous page
95+
}
96+
// If click is in the right 80% of the image, go to the next page
97+
else if (clickPosition > imageWidth * 0.2) {
98+
navigateToPage(currentPage + 1); // Handle navigation to the next page
99+
}
100+
});
101+
80102
function parseUrl(url) {
81103
const match = url.match(/(\d+)(\.\w+)$/);
82104
if (!match) return null;
@@ -91,36 +113,51 @@
91113
pageDisplay.textContent = `${currentPage} / ${maxPage}`;
92114
}
93115

94-
function navigateToPage(targetPage) {
116+
async function navigateToPage(targetPage) {
95117
if (targetPage < 1) {
96-
alert('Already at the first page');
97118
return;
98119
}
99-
120+
100121
const url = `${baseUrl}${targetPage}${extension}`;
101-
if(targetPage <= maxPage){
102-
currentPage = targetPage
122+
123+
// Show the loading spinner before the image starts loading
124+
const spinner = document.querySelector('.spinner-border');
125+
spinner.style.display = 'inline-block'; // Show the spinner
126+
127+
if (targetPage <= maxPage) {
128+
currentPage = targetPage;
103129
imageDisplay.src = url;
104130
updatePageDisplay();
105131
} else {
106-
const testImage = new Image();
107-
108-
testImage.onload = () => {
132+
try {
133+
// Create a new image instance to check if the image exists
134+
const testImage = new Image();
135+
136+
// Wait for the image to load
137+
await loadImage(testImage, url);
138+
109139
currentPage = targetPage;
110140
if (targetPage > maxPage) maxPage = targetPage;
141+
142+
// Image is loaded, update the image display
111143
imageDisplay.src = url;
112144
updatePageDisplay();
113-
};
114-
115-
testImage.onerror = () => {
145+
} catch (error) {
116146
alert('Page does not exist');
117-
};
118-
119-
testImage.src = url;
147+
}
120148
}
121-
122-
123149

150+
// Hide the spinner after the image has loaded
151+
spinner.style.display = 'none'; // Hide the spinner
152+
}
153+
154+
// Helper function to return a Promise for image loading
155+
function loadImage(img, src) {
156+
return new Promise((resolve, reject) => {
157+
img.onload = resolve; // Resolve the promise when the image loads
158+
img.onerror = reject; // Reject the promise if the image fails to load
159+
img.src = src;
160+
});
124161
}
125162

126163
viewImageBtn.addEventListener('click', () => {
@@ -150,6 +187,26 @@
150187
prevBtn.addEventListener('click', () => navigateToPage(currentPage - 1));
151188
nextBtn.addEventListener('click', () => navigateToPage(currentPage + 1));
152189
lastBtn.addEventListener('click', () => navigateToPage(maxPage));
190+
191+
// Event listener for clicks on the overlay
192+
overlay.addEventListener('click', function(event) {
193+
// Get the width of the image
194+
const imageWidth = imageDisplay.offsetWidth;
195+
196+
// Calculate the click position relative to the image width
197+
const clickPosition = event.clientX - imageDisplay.getBoundingClientRect().left;
198+
199+
// If click is in the left 20% of the image, go to the previous page
200+
if (clickPosition < imageWidth * 0.2) {
201+
navigateToPage(currentPage - 1); // Handle navigation to the previous page
202+
}
203+
// If click is in the right 80% of the image, go to the next page
204+
else if (clickPosition > imageWidth * 0.2) {
205+
navigateToPage(currentPage + 1); // Handle navigation to the next page
206+
}
207+
});
208+
209+
153210
});
154211
</script>
155212

0 commit comments

Comments
 (0)