当前位置: 首页 > 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 <liclassName = { 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中改就好了

http://www.lryc.cn/news/31376.html

相关文章:

  • 假如你知道这样的JVM
  • MyBatis框架快速入门 - 基本使用
  • Linux命令---文件管理
  • getDerivedStateFromProps和getSnapshotBeforeUpdate
  • 【Docker】如何在内网快速搭建docker并安装Oracle11g
  • 为啥用 时序数据库 TSDB
  • Linux命令·cp
  • SAP GUI快捷键
  • 【Java】序列化与反序列化
  • 面向对象的使用
  • LPDDR4x 的 学习总结(3) - SDRAM基本功能
  • 设计模式(三)--适配器模式(Adapter Pattern)
  • Web服务器基础介绍与Apache的简单介绍(LAMP架构与搭建论坛)
  • Linux 进程:exec函数簇
  • 极简RSS订阅器Miniflux
  • 网络通信快速入门
  • 【阅读文档】Vue.js 2.0 之教程文档
  • Docker【基本使用】
  • 算法leetcode|39. 组合总和(rust重拳出击)
  • JavaSE学习笔记总结day18
  • HybridFusion: LiDAR和视觉交叉源点云融合
  • 走进JVM
  • C语言-基础了解-15-C函数指针与回调函数
  • react和vue在响应式上的不同理解
  • 多线程二 多线程了解与使用
  • 嵌入式 Linux 的僵尸进程是什么?
  • 【刷题笔记】笔记一
  • 浏览器主页被hao123劫持的解决方案
  • 华为OD机试题 - 热点网络统计(JavaScript)| 含代码编写思路
  • IT项目经理的自我修养手册