Spaces:
Sleeping
Sleeping
File size: 1,692 Bytes
e0ad1ce |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
import { EmblaCarouselType } from "embla-carousel";
const TWEEN_FACTOR = 4.2;
const numberWithinRange = (number: number, min: number, max: number): number =>
Math.min(Math.max(number, min), max);
const calculateTweenValuesOpacity = (emblaApi: EmblaCarouselType): number[] => {
const engine = emblaApi.internalEngine();
const scrollProgress = emblaApi.scrollProgress();
return emblaApi.scrollSnapList().map((scrollSnap, index) => {
if (!emblaApi.slidesInView().includes(index)) return 0;
let diffToTarget = scrollSnap - scrollProgress;
if (engine.options.loop) {
engine.slideLooper.loopPoints.forEach((loopItem) => {
const target = loopItem.target();
if (index === loopItem.index && target !== 0) {
const sign = Math.sign(target);
if (sign === -1) diffToTarget = scrollSnap - (1 + scrollProgress);
if (sign === 1) diffToTarget = scrollSnap + (1 - scrollProgress);
}
});
}
const tweenValue = 1 - Math.abs(diffToTarget * TWEEN_FACTOR);
return numberWithinRange(tweenValue, 0, 1);
});
};
export const setupTweenOpacity = (
emblaApi: EmblaCarouselType
): {
applyTweenOpacity: () => void;
removeTweenOpacity: () => void;
} => {
const tweenNodes = emblaApi.slideNodes();
const applyTweenOpacity = (): void => {
const tweenValues = calculateTweenValuesOpacity(emblaApi);
tweenValues.forEach((tweenValue, index) => {
tweenNodes[index].style.opacity = tweenValue.toString();
});
};
const removeTweenOpacity = (): void => {
tweenNodes.forEach((slide) => slide.removeAttribute("style"));
};
return {
applyTweenOpacity,
removeTweenOpacity,
};
};
|