Skip to content

Commit f4c2f74

Browse files
committed
fix broken media scroller script
1 parent 373133c commit f4c2f74

2 files changed

Lines changed: 53 additions & 51 deletions

File tree

_layouts/default.html

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -51,60 +51,62 @@
5151
});
5252

5353
// 2. Media Scroller Logic
54-
let isScrolling = false;
55-
const container = scroller.closest('.project-media');
56-
const dotsContainer = container?.querySelector('.scroller-dots');
57-
const items = scroller.querySelectorAll('.media-item');
58-
59-
// Helper to get exact scroll distance between items
60-
const getScrollStep = () => items[1].offsetLeft - items[0].offsetLeft;
61-
62-
// A. Click on dots to jump to image
63-
if (dotsContainer) {
64-
const dots = dotsContainer.querySelectorAll('.dot');
65-
dots.forEach((dot, i) => {
66-
dot.style.cursor = 'pointer'; // Make it look clickable
67-
dot.addEventListener('click', () => {
68-
const scrollStep = getScrollStep();
69-
scroller.scrollTo({
70-
left: i * scrollStep,
71-
behavior: 'smooth'
54+
document.querySelectorAll('.media-scroller').forEach((scroller) => {
55+
let isScrolling = false;
56+
const container = scroller.closest('.project-media');
57+
const dotsContainer = container?.querySelector('.scroller-dots');
58+
const items = scroller.querySelectorAll('.media-item');
59+
60+
// Helper to get exact scroll distance between items
61+
const getScrollStep = () => {
62+
if (items.length <= 1) return scroller.clientWidth;
63+
return items[1].offsetLeft - items[0].offsetLeft;
64+
};
65+
66+
// A. Click on dots to jump to image
67+
if (dotsContainer) {
68+
const dots = dotsContainer.querySelectorAll('.dot');
69+
dots.forEach((dot, i) => {
70+
dot.addEventListener('click', () => {
71+
scroller.scrollTo({
72+
left: i * getScrollStep(),
73+
behavior: 'smooth'
74+
});
7275
});
7376
});
74-
});
75-
}
76-
77-
// B. Wheel Scroll Logic (Desktop)
78-
scroller.addEventListener('wheel', (evt) => {
79-
if (window.innerWidth > 768) {
80-
evt.preventDefault();
81-
if (isScrolling) return;
82-
83-
const threshold = 15;
84-
if (Math.abs(evt.deltaY) > threshold) {
85-
isScrolling = true;
86-
const direction = evt.deltaY > 0 ? 1 : -1;
87-
const scrollStep = getScrollStep();
88-
89-
scroller.scrollBy({
90-
left: scrollStep * direction,
91-
behavior: 'smooth'
92-
});
77+
}
9378

94-
setTimeout(() => { isScrolling = false; }, 500);
79+
// B. Wheel Scroll Logic (Desktop)
80+
scroller.addEventListener('wheel', (evt) => {
81+
if (window.innerWidth > 768) {
82+
// Only prevent default if we are scrolling horizontally
83+
if (Math.abs(evt.deltaY) > Math.abs(evt.deltaX)) {
84+
evt.preventDefault();
85+
if (isScrolling) return;
86+
87+
const threshold = 10;
88+
if (Math.abs(evt.deltaY) > threshold) {
89+
isScrolling = true;
90+
const direction = evt.deltaY > 0 ? 1 : -1;
91+
92+
scroller.scrollBy({
93+
left: getScrollStep() * direction,
94+
behavior: 'smooth'
95+
});
96+
97+
setTimeout(() => { isScrolling = false; }, 500);
98+
}
99+
}
95100
}
96-
}
97-
}, { passive: false });
98-
99-
// C. Update Dots on Scroll
100-
scroller.addEventListener('scroll', () => {
101-
if (!dotsContainer) return;
102-
const dots = dotsContainer.querySelectorAll('.dot');
103-
if (items.length > 1) {
104-
const scrollStep = getScrollStep();
105-
const index = Math.round(scroller.scrollLeft / scrollStep);
101+
}, { passive: false });
102+
103+
// C. Update Dots on Scroll
104+
scroller.addEventListener('scroll', () => {
105+
if (!dotsContainer || items.length <= 1) return;
106+
const dots = dotsContainer.querySelectorAll('.dot');
107+
const index = Math.round(scroller.scrollLeft / getScrollStep());
106108
dots.forEach((dot, i) => dot.classList.toggle('active', i === index));
107-
}
109+
});
108110
});
109111
</script>
110112
</body>

style.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ body {
200200
.media-scroller {
201201
width: 100%;
202202
display: flex;
203-
align-items: flex-end;
204-
overflow-x: scroll;
203+
align-items: flex-start;
204+
overflow-x: auto;
205205
scroll-snap-type: x mandatory;
206206
gap: 20px;
207207
padding: 50px 10%;

0 commit comments

Comments
 (0)