forked from Harvela/Harvely-Landing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage-slider.tsx
111 lines (100 loc) · 2.73 KB
/
image-slider.tsx
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
'use client';
import React, { useEffect, useState } from 'react';
import { cn } from '@/utils/cn';
export const InfiniteMovingImages = ({
items,
direction = 'left',
speed = 'slow',
pauseOnHover = true,
className,
}: {
items: string[];
direction?: 'left' | 'right';
speed?: 'fast' | 'normal' | 'slow';
pauseOnHover?: boolean;
className?: string;
}) => {
const containerRef = React.useRef<HTMLDivElement>(null);
const scrollerRef = React.useRef<HTMLUListElement>(null);
const getDirection = () => {
if (containerRef.current) {
if (direction === 'left') {
containerRef.current.style.setProperty(
'--animation-direction',
'forwards',
);
} else {
containerRef.current.style.setProperty(
'--animation-direction',
'reverse',
);
}
}
};
const [start, setStart] = useState(false);
const getSpeed = () => {
if (containerRef.current) {
if (speed === 'fast') {
containerRef.current.style.setProperty('--animation-duration', '20s');
} else if (speed === 'normal') {
containerRef.current.style.setProperty('--animation-duration', '40s');
} else {
containerRef.current.style.setProperty('--animation-duration', '200s');
}
}
};
function addAnimation() {
if (containerRef.current && scrollerRef.current) {
const scrollerContent = Array.from(scrollerRef.current.children);
scrollerContent.forEach((item) => {
const duplicatedItem = item.cloneNode(true);
if (scrollerRef.current) {
scrollerRef.current.appendChild(duplicatedItem);
}
});
getDirection();
getSpeed();
setStart(true);
}
}
useEffect(() => {
addAnimation();
}, []);
return (
<div
ref={containerRef}
className={cn(
'scroller relative z-20 max-w-7xl overflow-hidden',
className,
)}
>
<ul
ref={scrollerRef}
className={cn(
' flex min-w-full shrink-0 w-max flex-nowrap gap-[1px] bg-white',
start && 'animate-scroll ',
pauseOnHover && 'hover:[animation-play-state:paused]',
)}
>
{items.map((item, idx) => (
<li
className=" relative flex w-[90vw] max-w-full shrink-0 flex-row md:w-[60vw]"
style={
{
// background:
// 'linear-gradient(180deg, var(--slate-800), var(--slate-900)',
}
}
key={idx}
>
<img
src={item}
alt="pupils"
className="h-[40vh] w-[100vw] object-cover md:h-[95vh] md:w-full"
/>{' '}
</li>
))}
</ul>
</div>
);
};