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

React使用antd的图片预览组件,点击哪个图片就预览哪个的设置

使用了官方推荐的相册模式的预览,但是点击预览之后,每次都是从图片列表的第一张开始预览,而不是点击哪张就从哪张开始预览:

所以这里我就封装了一下,对初始化预览的列表进行了逻辑处理:

当点击开始预览的时候,要找到当前图片在预览图列表中的索引,然后设置为当前预览图索引,然后等点击左右切换的时候,要改变这个索引,所以要使用onChange函数,等点击关闭按钮的时候,还要重置这个索引为点击时候的图片索引:

import './index.scss'
import { Image } from 'antd'
import { useState } from 'react'export default function ImageItem(props: any) {// console.log('props', props)const preList = ['https://gw.alipayobjects.com/zos/antfincdn/LlvErxo8H9/photo-1503185912284-5271ff81b9a8.webp','https://gw.alipayobjects.com/zos/antfincdn/cV16ZqzMjW/photo-1473091540282-9b846e7965e3.webp','https://gw.alipayobjects.com/zos/antfincdn/x43I27A55%26/photo-1438109491414-7198515b166b.webp',]const [preIndex, setIndex] = useState(preList.indexOf(props.imgUrl))// 当点击前后切换的时候,修改当前预览图const handleSwitch = (current: number, prevCurrent: number) => {console.log('切换预览图', current, prevCurrent)setIndex(current)}// 关闭预览图是重置预览为当前图索引const handleClose = (visible: boolean) => {console.log('关闭预览图', visible)if (!visible) {setIndex(preList.indexOf(props.imgUrl))}}return (<div className="file"><Image.PreviewGroupitems={preList}preview={{minScale: 0.1,current: preIndex,onChange: handleSwitch,onVisibleChange: handleClose,}}><Image className="item-img" src={props.imgUrl} /></Image.PreviewGroup></div>)
}

预览结果: 

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

相关文章:

  • 排序的介绍
  • appuploader使用教程
  • 企业权限管理(七)-权限操作
  • 【深度学习笔记】TensorFlow 常用函数
  • 函数的递归与迭代
  • win10 + VS2022 安装opencv C++
  • nginx反向代理及负载均衡的实现
  • Tomcat部署SpringBoot项目
  • Oracle笔记--dblink
  • Mapbox加载天地图CGCS2000矢量瓦片地图
  • day3 STM32 GPIO口介绍
  • 【ElasticSearch】ElasticSearch 内存设置原则
  • VUE+ElementUI的表单验证二选一必填项,并且满足条件后清除表单验证提示
  • NLP 时事和见解【2023】
  • Windows 环境下 Python3 离线安装 cryptography 失败
  • 年轻代频繁GC ParNew导致http变慢
  • 在.NET 6.0中自定义接口路由
  • Kotlin读写分离CopyOnWriteArrayList
  • centos自动同步北京时间
  • element-ui表格跨页多选实现
  • 线性代数(三) 线性方程组向量空间
  • 学习跨度级交互以提取方面情感三元组
  • 那些年的Xposed开发经验记录
  • android studio内存分析之Memory profiler的使用
  • Qt下载慢/无法下载解决方式
  • 【UE4 RTS】04-Camera Pan
  • 出现raise NotImplementedError报错
  • 第一百二十二天学习记录:C++提高:STL-vector容器(上)(黑马教学视频)
  • 《Linux从练气到飞升》No.11 初识操作系统
  • 什么是 XSS 攻击?