|
51 | 51 | }); |
52 | 52 |
|
53 | 53 | // 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 | + }); |
72 | 75 | }); |
73 | 76 | }); |
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 | + } |
93 | 78 |
|
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 | + } |
95 | 100 | } |
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()); |
106 | 108 | dots.forEach((dot, i) => dot.classList.toggle('active', i === index)); |
107 | | - } |
| 109 | + }); |
108 | 110 | }); |
109 | 111 | </script> |
110 | 112 | </body> |
|
0 commit comments