Skip to content

Commit

Permalink
fix(core): fix edge cases where intersection observer doesn't trigger…
Browse files Browse the repository at this point in the history
… due to rounded value
  • Loading branch information
Grsmto committed Aug 17, 2020
1 parent b19e584 commit 827d6f6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import 'scroll-snap-carousel/dist/styles.css';

const getPageIndex = (index, length, offset) => {
return Math.max(0, index - offset);
return Math.min(Math.max(0, index - offset), length - offset - 1);
};

const Examples = () => {
Expand Down
12 changes: 6 additions & 6 deletions packages/examples/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ body {
}

.slide {
width: calc(33.33% - ((2 * 100px) / 3));
width: calc(33.33% - ((2 * 10vw) / 3));
flex-shrink: 0;
height: 100%;
scroll-snap-align: center;
Expand All @@ -38,24 +38,24 @@ body {
}

.slide:first-child {
margin-left: 100px;
margin-left: 10vw;
}

.slide:last-child {
margin-right: 100px;
margin-right: 10vw;
}

@media (max-width: 640px) {
.slide {
width: 50%;
width: calc(50% - ((2 * 5vw) / 2));
}

.slide:first-child {
margin-left: 50px;
margin-left: 5vw;
}

.slide:last-child {
margin-right: 50px;
margin-right: 5vw;
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/scroll-snap-carousel/src/getActiveSnap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export const getActiveSnap = ({
entries.forEach((entry) => {
if (!activeSnapIndex) return;

if (entry.isIntersecting && entry.intersectionRatio === 1) {
if (entry.isIntersecting && entry.intersectionRatio >= 0.99) {
activeSnapObserver.unobserve(children[activeSnapIndex]);
activeSnapObserver.observe(children[children.length - 2]);

Expand All @@ -217,7 +217,7 @@ export const getActiveSnap = ({
{
root,
rootMargin: rootMarginEdges,
threshold: [0, 1],
threshold: [0, 0.99],
}
);

Expand Down

0 comments on commit 827d6f6

Please sign in to comment.