当前位置: 首页 > news >正文

react-swipeable-views轮播图实现下方的切换点控制组件

本文是react通过react-swipeable-views创建公共轮播图组件的续文

上一文 我们创建了这样的一个轮播图组件
在这里插入图片描述
但我们已经看到的轮播图 下面都会有小点 展示当前所在的位置
但react-swipeable-views 并没有直接提供 我们需要自己去编写这个组件

我们在components下的 rotationChart 创建一个 Pagination.jsx 组件

然后 在同目录下创建一个样式文件 我这里叫 Pagination.css
参考代码如下

.swiper-pagination{
    position: absolute;
    bottom: 10px;
    right: 10px;
    display: inline-block;
    height: auto;
    width: 100%;
}
ul {
    width: 100%;
    height: auto;
    text-align: center;
}
li {
    list-style: none;
    display: inline-block;
    height: 10px;
    width: 10px;
    border-radius: 50%;
    background-color:#ccc;
    margin: 0 3px;
}
li.selected {
    background-color:rgb(233,32,61);
}

Pagination.jsx 参开代码如下

import React from 'react';

import './Pagination.css';

export default class Pagination extends React.Component{
    render(){
        const quantity = this.props&&this.props.quantity?new Array(this.props.quantity).fill(1):[];
        const currentIndex = this.props&&this.props.currentIndex?this.props.currentIndex:0;
        return (
            <div className = "swiper-pagination">
                <ul>
                    {
                        quantity.map((element,index) => {
                            return <li
                              className = { currentIndex === index?'selected':'' }
                              key = { index }
                            ></li>
                        })
                    }
                </ul>
            </div>
        )
    }
}

这里 我们又多接收了一个参数 叫 currentIndex 判断 如果 currentIndex 和 当前下班的index相同 则给与选中状态

那么 我们来到 components/rotationChart下的index.jsx 更改代码如下

import React from 'react';
import SwipeableViews from 'react-swipeable-views';
import Pagination from './Pagination';

import './index.css';

export default class Swiper extends React.Component{
    constructor(props){
    super(props);
        this.state = {
            currentIndex: 0
        }
    }
    handleChangeIndex = (index) => {
        this.setState({
            currentIndex: index
        })
    }
    render(){
        const banners = this.props&&this.props.banners?this.props.banners:[];
        const height = this.props&&this.props.height?this.props.height:"200px";
        const width = this.props&&this.props.width?this.props.width:"400px";
        return (
            <div className = "swiper" style = { {height,width} }>
                <SwipeableViews onChangeIndex={ this.handleChangeIndex }>
                     {
                        banners.map((element ,index) => {
                            return (
                                <div className='swiper-view' key= { index }>
                                    <img src={ element } alt=""/>
                                </div>
                            )
                        })
                     }
                </SwipeableViews>
                <Pagination currentIndex = { this.state.currentIndex } quantity = { banners.length }/>
            </div>
        )
    }
}

这里我们用了 react-swipeable-views组件中的 onChangeIndex函数 监听了切换事件 返回一个参数

就是当前轮播图所在的下标 然后给到 Pagination组件 运行结果如下
在这里插入图片描述
在这里插入图片描述
此时 我们下方的三个小点就实现了 因为这个组件是自己写的 如果想改样式 直接在 Pagination.css中改就好了

相关文章:

  • Java线程知识点总结
  • Android Compose——一个简单的Bilibili APP
  • 世界顶级五大女程序媛,不仅技术强还都是美女
  • 2023年再不会Redis,就要被淘汰了
  • 【学习笔记】深入理解JVM之垃圾回收机制
  • 【数据结构】链式二叉树
  • 自学大数据第三天~终于轮到hadoop了
  • 应用层协议 HTTP HTTPS
  • Linux内核学习笔记——页表的那些事。
  • 一文带你入门,领略angular风采(上)!!!
  • 嵌入式学习笔记——STM32硬件基础知识
  • 2023年“网络安全”赛项浙江省金华市选拔赛 任务书
  • Qt安装与使用经验分享;无.pro文件;无QTextCodec file;Qt小试;界面居中;无缝;更换Qt图标;更换Qt标题。
  • MyBatis常用的俩种分页方式
  • Python---正则表达式
  • ➹使用webpack配置多页面应用(MPA)
  • AngularJS指令开发(1)——参数详解
  • Codepen 每日精选(2018-3-25)
  • co模块的前端实现
  • CSS 提示工具(Tooltip)
  • Debian下无root权限使用Python访问Oracle
  • electron原来这么简单----打包你的react、VUE桌面应用程序
  • httpie使用详解
  • IndexedDB
  • js对象的深浅拷贝
  • laravel with 查询列表限制条数
  • Linux各目录及每个目录的详细介绍
  • spark本地环境的搭建到运行第一个spark程序
  • Vue.js源码(2):初探List Rendering
  • 分布式事物理论与实践
  • 聚类分析——Kmeans
  • 浅谈Golang中select的用法
  • 腾讯优测优分享 | 你是否体验过Android手机插入耳机后仍外放的尴尬?
  • 怎么把视频里的音乐提取出来
  • # Pytorch 中可以直接调用的Loss Functions总结:
  • # 执行时间 统计mysql_一文说尽 MySQL 优化原理
  • (2015)JS ES6 必知的十个 特性
  • (力扣)1314.矩阵区域和
  • (一)Java算法:二分查找
  • .net 8 发布了,试下微软最近强推的MAUI
  • .NET 中什么样的类是可使用 await 异步等待的?
  • .NET(C#) Internals: as a developer, .net framework in my eyes
  • .netcore 如何获取系统中所有session_ASP.NET Core如何解决分布式Session一致性问题
  • .NET开源项目介绍及资源推荐:数据持久层 (微软MVP写作)
  • /dev/sda2 is mounted; will not make a filesystem here!
  • ??如何把JavaScript脚本中的参数传到java代码段中
  • @AutoConfigurationPackage的使用
  • @RunWith注解作用
  • [ vulhub漏洞复现篇 ] JBOSS AS 4.x以下反序列化远程代码执行漏洞CVE-2017-7504
  • [ vulhub漏洞复现篇 ] Jetty WEB-INF 文件读取复现CVE-2021-34429
  • [3D基础]理解计算机3D图形学中的坐标系变换
  • [8481302]博弈论 斯坦福game theory stanford week 1
  • [Android 13]Input系列--获取触摸窗口
  • [Bada开发]初步入口函数介绍
  • [C#]winform制作仪表盘好用的表盘控件和使用方法