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
|
import React, { useState, useEffect } from "react";
import { FiChevronRight, FiChevronLeft } from "react-icons/fi";
import { FaQuoteRight } from "react-icons/fa";
import data from "./data";
import styles from ".index.module.css";
const Slider = () => {
const [people, setPeople] = useState(data);
const [index, setIndex] = useState(0);
useEffect(() => {
const lastIndex = people.length - 1;
if (index < 0) {
setIndex(lastIndex);
}
if (index > lastIndex) {
setIndex(0);
}
}, [index, people]);
useEffect(() => {
let slider = setInterval(() => {
setIndex(index + 1);
}, 3000);
return () => clearInterval(slider);
}, [index]);
return (
<div className={styles.main}>
<section className={styles.section}>
<div className={styles.title}>
<h2>
<span>/</span>reviews
</h2>
</div>
<div className={styles.sectionCenter}>
{people.map((person, personIndex) => {
const { id, image, name, title, quote } = person;
let position = "nextSlide";
if (personIndex === index) {
position = "activeSlide";
}
if (
personIndex === index - 1 ||
(index === 0 && personIndex === people.length - 1)
) {
// 条件1:把一个slide挪到左边
// 条件2: index为0时 把数组最后一个挪到最左边
position = "lastSlide";
}
return (
<article
className={`${styles.article} ${styles[position]}`}
key={id}
>
<img src={image} alt={name} className={styles.personImg} />
<h4>{name}</h4>
<p className={styles.title}>{title}</p>
<p className={styles.text}>{quote}</p>
<FaQuoteRight className={styles.icon} />
</article>
);
})}
<button className={styles.prev} onClick={() => setIndex(index - 1)}>
<FiChevronLeft />
</button>
<button className={styles.next} onClick={() => setIndex(index + 1)}>
<FiChevronRight />
</button>
</div>
</section>
</div>
);
};
export default Slider;
|