Skip to content

Commit

Permalink
Merge pull request #30 from osakunta/fix-carousel
Browse files Browse the repository at this point in the history
fix carousel effect not working on page load
  • Loading branch information
YB-BigSwan authored Aug 30, 2024
2 parents bfac6e3 + 3b8e574 commit 29e83df
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions components/Carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ const wrappedDistance = (a: number, b: number) => {

const setOpacity = (emblaApi: EmblaCarouselType) => {
const slideNodes = emblaApi.slideNodes();
const slidesInView = emblaApi.slidesInView();

const currentPosition = emblaApi.scrollProgress();

slidesInView.forEach((slideIndex) => {
slideNodes.forEach((_, slideIndex) => {
const slidePosition = emblaApi.scrollSnapList()[slideIndex];
const opacity = clamp(
1 - wrappedDistance(slidePosition, currentPosition) * 8.4,
Expand All @@ -48,6 +47,7 @@ const Carousel: React.FC<PropType> = ({ slides, options }) => {
useEffect(() => {
if (!emblaApi) return;

setOpacity(emblaApi);
emblaApi.on("reInit", setOpacity).on("scroll", setOpacity);
}, [emblaApi]);

Expand Down

0 comments on commit 29e83df

Please sign in to comment.