antd跑马灯实现左右箭头切换(React版本)

我们在查找antd官网,使用轮播图的时候,发现左右点击切换轮播图被去掉了,这个功能也没有。既然没有那就实现一个吧。

效果:

react部分代码

import React, { useRef } from 'react'
import { Carousel } from 'antd';
import styles from './index.less'

const Screen = () => {
    const myRef = useRef(null);
    return (
        <div className={styles.wrap}>
            <div className={styles.btnLeft} onClick={() => { myRef?.current?.prev?.() }}>&lt;</div>
            <div className={styles.btnRight} onClick={() => { myRef?.current?.next?.() }}>&gt;</div>
            <Carousel autoplay effect="fade" ref={myRef} dots={false}  >
                <div>
                    <div key={'1'} className={styles.content1} >111</div>
                </div>
                <div>
                    <div key={'2'} className={styles.content2} >222</div>
                </div>
                <div>
                    <div key={'3'} className={styles.content3} >333</div>
                </div>
                <div>
                    <div key={'4'} className={styles.content4} >444</div>
                </div>
            </Carousel>
        </div>

    )
}

export default Screen

index.less

.wrap {
    height: 100vh;
    position: relative;

    :global(.ant-carousel .slick-prev,
        .ant-carousel .slick-next,
        .ant-carousel .slick-prev:hover,
        .ant-carousel .slick-next:hover) {
        font-size: inherit;
        color: currentColor;
    }

    .content1 {
        margin: 0;
        height: 100vh;
        color: white;
        text-align: center;
        background: #364d79;
        line-height: 100vh;
    }

    .content2 {
        margin: 0;
        height: 100vh;
        color: white;
        text-align: center;
        background: #364d79;
        line-height: 100vh;

    }

    .content3 {
        margin: 0;
        height: 100vh;
        color: white;
        text-align: center;
        background: #364d79;
        line-height: 100vh;

    }

    .content4 {
        margin: 0;
        height: 100vh;
        color: white;
        text-align: center;
        background: #364d79;
        line-height: 100vh;

    }


    .btnLeft {
        position: absolute;
        top: 50%;
        left: 15px;
        color: white;
        font-size: 40px;
        z-index: 10;
        cursor: pointer;
    }

    .btnRight {
        position: absolute;
        top: 50%;
        right: 15px;
        color: white;
        font-size: 40px;
        z-index: 10;
        cursor: pointer;
    }
}
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React Antd 的 Pagination 组件可以通过监听页码变化的事件来更新数据。 具体来说,你可以在 Pagination 组件的 `onChange` 回调函数中处理页码变化事件,当用户点击不同的页码时,就可以在该回调函数中调用更新数据的方法,并将新数据渲染到页面上。 以下是一个示例代码,展示如何使用 Pagination 组件实现切换页面更新数据: ```jsx import { useState, useEffect } from 'react'; import { Pagination } from 'antd'; function MyComponent() { const [currentPage, setCurrentPage] = useState(1); const [data, setData] = useState([]); // 模拟异步请求数据 async function fetchData(page) { const res = await fetch(`https://example.com/api/data?page=${page}`); const json = await res.json(); setData(json.data); } // 初始化时获取第一页数据 useEffect(() => { fetchData(1); }, []); // 监听页码变化事件,更新数据 function handlePageChange(page) { setCurrentPage(page); fetchData(page); } return ( <div> {/* 渲染数据 */} {data.map(item => ( <div key={item.id}>{item.name}</div> ))} {/* 渲染 Pagination 组件 */} <Pagination current={currentPage} total={100} onChange={handlePageChange} /> </div> ); } ``` 在这个示例中,我们使用 `useState` 钩子来管理当前页码和数据状态。`useEffect` 钩子在组件挂载时获取第一页数据。`handlePageChange` 函数用于处理页码变化事件,并在变化时调用 `fetchData` 方法来获取新的数据。最后,我们在组件中渲染数据和 Pagination 组件,并将 `current` 属性设置为当前页码,`total` 属性设置为数据的总数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值