React實現(xiàn)動態(tài)輪播圖的使用示例
前言
輪播組件是常見的一種方式,用來展示圖像、信息或者是廣告。我們可以使用React來創(chuàng)建一個輪播組件,并且利用其中的State和effect Hook來創(chuàng)建一款動態(tài)的、可以自動播放的輪播組件。
效果
輪播組件會展示一個平鋪的圖片列表。在圖片列表下方是一組小圓點,每個小圓點對應(yīng)一個圖片。當一個新的圖片顯示時,相對應(yīng)的小圓點會高亮。組件會每隔一段時間自動切換圖片,并且當它切換到最后一張圖片后,會繼續(xù)切回第一張圖片。

原理分析
輪播組件的實現(xiàn)依賴于React的**useState和useEffect**這兩個Hook。
- 我們首先通過**
useState創(chuàng)建一個index變量以及一個setIndex函數(shù)。index**記錄了我們當前展示圖片的位置。 - 下面我們會創(chuàng)建一個名為**
list**的變量,它會把圖片數(shù)組中第一張圖片復(fù)制一份添加到數(shù)組末尾。 - 接下來我們在**
useEffect中設(shè)置一個定時任務(wù),每隔2秒鐘更新一次index**的值,等于在每兩秒鐘播放下一張圖片。 - 最后,我們根據(jù)**
index的值,來計算需要平移的比例,然后定義一個transform** CSS屬性,此步驟定義了圖片滾動所需要的動畫效果。


代碼實現(xiàn)
- 首先,我們導(dǎo)入所需的模塊以及定義圖片數(shù)據(jù)。
import React, { useState } from "react";
import classnames from "classnames";
import "./index.less";
const dataSource = [
{
picture:
"<https://cdn.fmlg1688.cn/build-material/3c76b0a215c64baebded1a690e1ff989.blob>",
title: "是怎么做出來的",
},
{
picture:
"<https://cdn.fmlg1688.cn/build-material/image/b3528a0b59f34e32aa4aae4652bee76f.jpeg>",
title: "成功案例",
},
{
picture:
"<https://cdn.fmlg1688.cn/build-material/3c76b0a215c64baebded1a690e1ff989.blob>",
title: "仿木欄桿-03",
},
];
- 然后實現(xiàn)輪播組件的代碼:
export default function Carousel() {
const [index, setIndex] = useState(0);
const carouselRef = useRef<HTMLDivElement>(null);
const list = useMemo(() => {
return [...dataSource, dataSource[0]];
}, [dataSource]);
useEffect(() => {
const goNext = () => {
setTimeout(() => {
if (index + 1 === list.length) {
if (carouselRef.current?.style) {
carouselRef.current.style.transform = "translateX(0%)";
}
setIndex(0);
} else {
setIndex(index + 1);
if (index + 1 === list.length - 1) {
setTimeout(() => {
setIndex(0);
}, 300);
}
}
// setIndex((inx) => {
// if (inx + 1 === list.length) {
// return 0;
// } else {
// if (inx + 1 === list.length) {
// goNext();
// }
// ret
// });
}, 2000);
};
goNext();
}, [index]);
console.log("index:", index);
return (
<div className="carousel-container">
<div
ref={carouselRef}
className="carousel"
style={{
width: `${100 * list.length}%`,
transform: `translateX(-${(100 / list.length) * index}%)`,
transition: 0 === index ? `` : "transform 0.3s",
}}
>
{list.map((data) => {
return (
<div className="carousel-item">
<img src={data.picture} />
</div>
);
})}
</div>
<ul className="dot">
{dataSource.map((data, inx) => {
return (
<li
className={classnames("dot-item", {
"dot-item--active": index === inx,
})}
></li>
);
})}
</ul>
</div>
);
}
export default function Carousel() {
const [index, setIndex] = useState(0);
const carouselRef = useRef<HTMLDivElement>(null);
const list = useMemo(() => {
return [...dataSource, dataSource[0]];
}, [dataSource]);
useEffect(() => {
const goNext = () => {
setTimeout(() => {
if (index + 1 === list.length) {
if (carouselRef.current?.style) {
carouselRef.current.style.transform = "translateX(0%)";
}
setIndex(0);
} else {
setIndex(index + 1);
if (index + 1 === list.length - 1) {
setTimeout(() => {
setIndex(0);
}, 300);
}
}
}, 2000);
};
goNext();
}, [index]);
console.log("index:", index);
return (
<div className="carousel-container">
<div
ref={carouselRef}
className="carousel"
style={{
width: `${100 * list.length}%`,
transform: `translateX(-${(100 / list.length) * index}%)`,
transition: 0 === index ? `` : "transform 0.3s",
}}
>
{list.map((data) => {
return (
<div className="carousel-item">
<img src={data.picture} />
</div>
);
})}
</div>
<ul className="dot">
{dataSource.map((data, inx) => {
return (
<li
className={classnames("dot-item", {
"dot-item--active": index === inx,
})}
></li>
);
})}
</ul>
</div>
);
}
在這個組件中:
- 我們定義了一個React引用**
carouselRef**,通過這個引用我們可以獲取到對應(yīng)渲染的DOM對象。 - 在每2秒鐘,我們通過**
goNext函數(shù)來觸發(fā)輪播圖的切換。有一個特別的處理,當輪播切換到復(fù)制的第一張圖片時,改變index**到0并且立即清除過渡效果,使得輪播圖看起來像是循環(huán)播放的。 - 當創(chuàng)建輪播組件的HTML部分時,我們基于
index值增加transform和transition樣式,通過CSS動畫實現(xiàn)了輪播的滾動效果。 - 最后我們定義了一個小圓點列表,它的作用是表示當前顯示的是哪張圖片。小圓點的數(shù)量等于圖片的數(shù)量,但是并不包括復(fù)制的第一張圖片。哪個小圓點處于活動狀態(tài)是基于**
index**值確定的。
總結(jié)
這個輪播圖基于React的**useState和useEffect實現(xiàn),利用了transform和transition**來呈現(xiàn)滑動的動畫效果。
到此這篇關(guān)于React實現(xiàn)動態(tài)輪播圖的使用示例的文章就介紹到這了,更多相關(guān)React 動態(tài)輪播圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決React?hook?'useState'?cannot?be?called?in?
這篇文章主要為大家介紹了React?hook?'useState'?cannot?be?called?in?a?class?component報錯解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12
ahooks useVirtualList 封裝虛擬滾動列表
這篇文章主要為大家介紹了ahooks useVirtualList 封裝虛擬滾動列表詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09

