Skip to content

Commit fcac284

Browse files
committed
Fixed slider
1 parent a3c6447 commit fcac284

1 file changed

Lines changed: 22 additions & 46 deletions

File tree

script.js

Lines changed: 22 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ document.addEventListener('DOMContentLoaded', () => {
66
offset: 100
77
});
88

9-
// --- Image Slider Logic ---
9+
// --- Image Slider Logic (Gallery) ---
1010
const slides = document.querySelectorAll('.slide');
1111
const nextBtn = document.querySelector('.next-btn');
1212
const prevBtn = document.querySelector('.prev-btn');
@@ -40,13 +40,7 @@ document.addEventListener('DOMContentLoaded', () => {
4040
prevBtn.addEventListener('click', () => goToSlide(currentSlide - 1));
4141
}
4242

43-
// Auto Advance (Optional)
44-
setInterval(() => {
45-
// goToSlide(currentSlide + 1);
46-
}, 5000);
47-
48-
49-
// --- Comparison Slider Logic (Multi) ---
43+
// --- Comparison Slider Logic (Drag Anywhere) ---
5044
const comparisonContainers = document.querySelectorAll('.img-comparison-container');
5145

5246
comparisonContainers.forEach(container => {
@@ -56,66 +50,66 @@ document.addEventListener('DOMContentLoaded', () => {
5650
if (afterImageWrapper && sliderHandle) {
5751
// 1. Create the vertical line dynamically
5852
const sliderLine = document.createElement('div');
59-
// Apply styles directly to ensure visibility without CSS file edits
6053
Object.assign(sliderLine.style, {
6154
position: 'absolute',
6255
top: '0',
6356
bottom: '0',
6457
width: '2px',
6558
backgroundColor: 'rgba(255, 255, 255, 0.8)',
66-
left: '50%', // Start at center
59+
left: '50%',
6760
transform: 'translateX(-50%)',
68-
zIndex: '20', // Ensure it's above images but below handle if needed
69-
pointerEvents: 'none' // Let clicks pass through to the image
61+
zIndex: '40',
62+
pointerEvents: 'none', // Allow clicks to pass through to container
63+
boxShadow: '0 0 5px rgba(0,0,0,0.5)'
7064
});
7165
container.appendChild(sliderLine);
7266

73-
// Ensure handle is above the line
74-
sliderHandle.style.zIndex = '30';
67+
// Ensure handle is above everything but lets events bubble if needed
68+
sliderHandle.style.zIndex = '50';
69+
sliderHandle.style.pointerEvents = 'none'; // distinct handle clicks not strictly needed if container handles all
7570

7671
let isDragging = false;
7772

7873
const moveSlider = (e) => {
74+
// Prevent calculation if we aren't "active" (though logic below handles this)
75+
// We actually want moveSlider to run ONCE on click, even if not moving yet.
76+
7977
const clientX = e.touches ? e.touches[0].clientX : e.clientX;
8078
const rect = container.getBoundingClientRect();
8179
let x = clientX - rect.left;
8280

81+
// Boundaries
8382
if (x < 0) x = 0;
8483
if (x > rect.width) x = rect.width;
8584

8685
const percentage = (x / rect.width) * 100;
8786
const clipAmount = 100 - percentage;
8887

89-
// Update Image Clip
88+
// Update CSS
9089
afterImageWrapper.style.clipPath = `inset(0 ${clipAmount}% 0 0)`;
91-
92-
// Update Handle Position
9390
sliderHandle.style.left = `${percentage}%`;
94-
95-
// Update Line Position
9691
sliderLine.style.left = `${percentage}%`;
9792
};
9893

99-
// 2. Modified Logic: Only start drag on Handle click, not Container click
10094
const startDrag = (e) => {
10195
isDragging = true;
102-
e.preventDefault(); // Prevent selection behavior
96+
e.preventDefault(); // Stop text selection
97+
moveSlider(e); // Immediately jump to where the user clicked
10398
};
10499

105-
// Attach start listeners ONLY to the handle
106-
sliderHandle.addEventListener('mousedown', startDrag);
107-
sliderHandle.addEventListener('touchstart', startDrag);
100+
// 2. Attach start listeners to the WHOLE CONTAINER
101+
container.addEventListener('mousedown', startDrag);
102+
container.addEventListener('touchstart', startDrag);
108103

109-
// Stop dragging on mouse up
104+
// 3. Global Stop Listeners
110105
window.addEventListener('mouseup', () => isDragging = false);
111106
window.addEventListener('touchend', () => isDragging = false);
112107

113-
// Handle movement (global to avoid losing focus if mouse leaves container)
108+
// 4. Global Move Listeners
114109
window.addEventListener('mousemove', (e) => {
115110
if (!isDragging) return;
116111
moveSlider(e);
117112
});
118-
119113
window.addEventListener('touchmove', (e) => {
120114
if (!isDragging) return;
121115
moveSlider(e);
@@ -125,21 +119,10 @@ document.addEventListener('DOMContentLoaded', () => {
125119
afterImageWrapper.style.clipPath = `inset(0 50% 0 0)`;
126120
sliderHandle.style.left = `50%`;
127121
sliderLine.style.left = `50%`;
128-
129-
// Reset on leave (Optional - usually better to disable this for drag-only UX,
130-
// but keeping per original unless requested to remove.
131-
// If you want the slider to stay where you left it, remove this block).
132-
container.addEventListener('mouseleave', () => {
133-
isDragging = false;
134-
// afterImageWrapper.style.clipPath = `inset(0 50% 0 0)`;
135-
// sliderHandle.style.left = `50%`;
136-
// sliderLine.style.left = `50%`;
137-
});
138122
}
139123
});
140124

141125
// --- Dynamic Background Blur ---
142-
// Update CSS variable for body pseudo-element
143126
window.addEventListener('scroll', () => {
144127
const scrollValues = window.scrollY;
145128
const maxScroll = 600;
@@ -179,19 +162,15 @@ document.addEventListener('DOMContentLoaded', () => {
179162
const nextIndex = (currentIndex + 1) % images.length;
180163
const nextImageSrc = images[nextIndex];
181164

182-
// Get current image
183165
const currentImg = wrapper.querySelector('img:last-of-type');
184166

185-
// Create New Image
186167
const newImg = document.createElement('img');
187168
newImg.src = nextImageSrc;
188169
newImg.className = 'stack-img slide-in-from-right animating';
189170
wrapper.appendChild(newImg);
190171

191-
// Force Reflow
192172
void newImg.offsetWidth;
193173

194-
// Start Animation
195174
requestAnimationFrame(() => {
196175
newImg.style.transform = 'translateX(0)';
197176

@@ -201,7 +180,6 @@ document.addEventListener('DOMContentLoaded', () => {
201180
}
202181
});
203182

204-
// Clean up
205183
setTimeout(() => {
206184
if (currentImg && currentImg.parentNode === wrapper) {
207185
wrapper.removeChild(currentImg);
@@ -228,14 +206,12 @@ document.addEventListener('DOMContentLoaded', () => {
228206
const modalImg = modal.querySelector('.modal-content');
229207
const closeModal = modal.querySelector('.close-modal');
230208

231-
// Logic to open modal
232209
const openModal = (src) => {
233210
modalImg.src = src;
234211
modal.classList.add('show');
235212
document.body.style.overflow = 'hidden';
236213
};
237214

238-
// Attach to existing images
239215
document.querySelectorAll('.gallery-img, .dist-img').forEach(img => {
240216
img.addEventListener('click', () => openModal(img.src));
241217
});
@@ -256,7 +232,7 @@ document.addEventListener('DOMContentLoaded', () => {
256232
}
257233
});
258234

259-
}); // End of DOMContentLoaded
235+
});
260236

261237
function copyCitation() {
262238
const codeBlock = document.getElementById('bibtex');

0 commit comments

Comments
 (0)