Skip to content

Commit acf675c

Browse files
Merge main into hoodies
2 parents 5b55ef9 + 40d28e6 commit acf675c

2 files changed

Lines changed: 46 additions & 8 deletions

File tree

‎css/freshers.scss‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,6 @@ main {
378378
flex-direction: column;
379379
min-width: 0;
380380
scroll-snap-align: start;
381-
scroll-snap-stop: always;
382381

383382
h3 {
384383
min-height: 2.45em;

‎js/freshers-events.js‎

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,23 @@ document.addEventListener("DOMContentLoaded", () => {
9292
if (!previous || !next) return;
9393

9494
const endTolerance = 4;
95+
let targetScrollLeft = track.scrollLeft;
96+
97+
const getSnapPositions = () => {
98+
const maximumScroll = track.scrollWidth - track.clientWidth;
99+
const firstCard = eventCards[0];
100+
101+
if (!firstCard) return [];
102+
103+
return Array.from(eventCards)
104+
.map((card) =>
105+
Math.min(maximumScroll, card.offsetLeft - firstCard.offsetLeft),
106+
)
107+
.filter(
108+
(position, index, positions) =>
109+
index === 0 || position - positions[index - 1] > endTolerance,
110+
);
111+
};
95112

96113
const updateControls = () => {
97114
const maximumScroll = track.scrollWidth - track.clientWidth;
@@ -100,14 +117,29 @@ document.addEventListener("DOMContentLoaded", () => {
100117
};
101118

102119
const move = (direction) => {
103-
const gap =
104-
Number.parseFloat(window.getComputedStyle(track).columnGap) || 0;
105-
const card = track.querySelector(".freshers-event-card");
120+
const snapPositions = getSnapPositions();
106121

107-
if (!card) return;
122+
if (!snapPositions.length) return;
108123

109-
track.scrollBy({
110-
left: direction * (card.getBoundingClientRect().width + gap),
124+
const currentIndex = snapPositions.reduce(
125+
(closestIndex, position, index) =>
126+
Math.abs(position - targetScrollLeft) <
127+
Math.abs(snapPositions[closestIndex] - targetScrollLeft)
128+
? index
129+
: closestIndex,
130+
0,
131+
);
132+
const targetIndex = Math.min(
133+
snapPositions.length - 1,
134+
Math.max(0, currentIndex + direction),
135+
);
136+
137+
targetScrollLeft = snapPositions[targetIndex];
138+
139+
// Absolute destinations avoid Firefox getting stuck when a smooth
140+
// relative scroll is combined with mandatory scroll snapping.
141+
track.scrollTo({
142+
left: targetScrollLeft,
111143
behavior: window.matchMedia("(prefers-reduced-motion: reduce)").matches
112144
? "auto"
113145
: "smooth",
@@ -117,7 +149,14 @@ document.addEventListener("DOMContentLoaded", () => {
117149
previous.addEventListener("click", () => move(-1));
118150
next.addEventListener("click", () => move(1));
119151
track.addEventListener("scroll", updateControls, { passive: true });
120-
window.addEventListener("resize", updateControls);
152+
track.addEventListener("scrollend", () => {
153+
targetScrollLeft = track.scrollLeft;
154+
updateControls();
155+
});
156+
window.addEventListener("resize", () => {
157+
targetScrollLeft = track.scrollLeft;
158+
updateControls();
159+
});
121160

122161
updateControls();
123162
});

0 commit comments

Comments
 (0)