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

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


代碼實(shí)現(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",
},
];
- 然后實(shí)現(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>
);
}
在這個(gè)組件中:
- 我們定義了一個(gè)React引用**
carouselRef**,通過這個(gè)引用我們可以獲取到對應(yīng)渲染的DOM對象。 - 在每2秒鐘,我們通過**
goNext函數(shù)來觸發(fā)輪播圖的切換。有一個(gè)特別的處理,當(dāng)輪播切換到復(fù)制的第一張圖片時(shí),改變index**到0并且立即清除過渡效果,使得輪播圖看起來像是循環(huán)播放的。 - 當(dāng)創(chuàng)建輪播組件的HTML部分時(shí),我們基于
index值增加transform和transition樣式,通過CSS動(dòng)畫實(shí)現(xiàn)了輪播的滾動(dòng)效果。 - 最后我們定義了一個(gè)小圓點(diǎn)列表,它的作用是表示當(dāng)前顯示的是哪張圖片。小圓點(diǎn)的數(shù)量等于圖片的數(shù)量,但是并不包括復(fù)制的第一張圖片。哪個(gè)小圓點(diǎn)處于活動(dòng)狀態(tài)是基于**
index**值確定的。
總結(jié)
這個(gè)輪播圖基于React的**useState和useEffect實(shí)現(xiàn),利用了transform和transition**來呈現(xiàn)滑動(dòng)的動(dòng)畫效果。
到此這篇關(guān)于React實(shí)現(xiàn)動(dòng)態(tài)輪播圖的使用示例的文章就介紹到這了,更多相關(guān)React 動(dòng)態(tài)輪播圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
react?component?function組件使用詳解
這篇文章主要為大家介紹了react?component?function組件的使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
ReactHook使用useState更新變量后,如何拿到變量更新后的值
這篇文章主要介紹了ReactHook使用useState更新變量后,如何拿到變量更新后的值問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
基于React和antd實(shí)現(xiàn)自定義進(jìn)度條的示例代碼
在現(xiàn)代 Web 開發(fā)中,進(jìn)度條是一種常見且實(shí)用的 UI 組件,用于直觀地向用戶展示任務(wù)的完成進(jìn)度,本文將詳細(xì)介紹如何使用 React 來構(gòu)建一個(gè)自定義的進(jìn)度條,它不僅能夠動(dòng)態(tài)展示進(jìn)度,還具備額外的信息提示功能,需要的朋友可以參考下2025-03-03
Remix 后臺(tái)桌面開發(fā)electron-remix-antd-admin
這篇文章主要為大家介紹了Remix 后臺(tái)桌面開發(fā)electron-remix-antd-admin的過程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04

