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)對(duì)應(yīng)一個(gè)圖片。當(dāng)一個(gè)新的圖片顯示時(shí),相對(duì)應(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ù)組末尾。 - 接下來我們?cè)?*
useEffect
中設(shè)置一個(gè)定時(shí)任務(wù),每隔2秒鐘更新一次index
**的值,等于在每?jī)擅腌姴シ畔乱粡垐D片。 - 最后,我們根據(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è)引用我們可以獲取到對(duì)應(yīng)渲染的DOM對(duì)象。 - 在每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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React?Hooks?實(shí)現(xiàn)的中文輸入組件
這篇文章主要為大家介紹了React?Hooks實(shí)現(xiàn)的中文輸入組件示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05解決React?hook?'useState'?cannot?be?called?in?
這篇文章主要為大家介紹了React?hook?'useState'?cannot?be?called?in?a?class?component報(bào)錯(cuò)解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12React中控制子組件顯示隱藏的兩種方式及對(duì)比詳解
在 react 中,如果我們想控制子組件的顯示和隱藏一般有兩種方式,一種是父組件維護(hù)子組件顯示隱藏狀態(tài),另一種則是通過 forwardRef 直接設(shè)置子組件的狀態(tài)進(jìn)行維護(hù),這兩種方式各有優(yōu)缺點(diǎn),以及對(duì)于不同的使用場(chǎng)景不同,今天我們就來簡(jiǎn)單討論下,需要的朋友可以參考下2025-04-04ahooks useVirtualList 封裝虛擬滾動(dòng)列表
這篇文章主要為大家介紹了ahooks useVirtualList 封裝虛擬滾動(dòng)列表詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09ReactNative實(shí)現(xiàn)Toast的示例
這篇文章主要介紹了ReactNative實(shí)現(xiàn)Toast的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12