-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (48 loc) · 1.54 KB
/
index.js
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 React, { useEffect, useState } from 'react';
import './CarouselBody.scss';
export default function CarouselAnimation({ data }) {
const [indexG, setIndex] = useState(0);
const [object, setObject] = useState({});
let indexGlobal = 0;
const quantifyShow = data.length;
useEffect(
() => {
const id = setInterval(() => {
if(indexGlobal >= data.length - 1) {
indexGlobal = 0;
} else {
indexGlobal += 1;
}
setIndex(indexGlobal);
}, 1500);
return () => {
clearInterval(id);
};
},
[] // empty dependency array
);
useEffect(() => {
const newObject = data.reduce((acc, _value, index, array) => {
return {...acc, [index]: (indexG + index >= array.length ? (indexG + index) - (array.length) : indexG + index)};
} ,{});
setObject(newObject);
}, [indexG]);
const classesAnimation = {};
for(let i = 0; i < quantifyShow; i += 1) {
classesAnimation[i] = (i <= (quantifyShow / 2)
? `shadow-right-${Math.floor(quantifyShow / 2) - i + 1}`
: `shadow-left-${i - Math.floor(quantifyShow / 2)}`
)
}
const carouselShowing = (arr) => {
return arr.map((children, index) => {
const classActual = classesAnimation[object[index]] || 'shadow';
return (
<div className={`carousel-item ${classActual}`} key={ index }>
{children}
</div>
);
});
};
return <div className="carousel-animation-body">{carouselShowing(data)}</div>
};