1.1.1 • Published 2 years ago

img-preview-for-react v1.1.1

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

基于react实现的图片预览弹层:

安装使用npm i img-preview-for-react

API

参数说明类型默认值版本
imageArr图片项Array<CardImagesItemsProps>[]
imgIndex当前展示图片索引number0
showThumbnail是否展示底部缩略图booleanfalse
close点击关闭按钮回调function-
type CardImagesItemsProps = {
  imageTitle?: string; // 图片标题,如果不传,则不大图预览时不展示标题
  thumbnail?: string; // 缩略图url,如果不传,则使用大图URL
  url?: string; // 大图url
};

DEMO

image

image

import React, { Component } from 'react'
import ImgPreview from 'img-preview-for-react'
let imgData = [
  {
    url: 'https://img.ljcdn.com/beike/crep_c/1639033696525.jpg',
    imgTitle: '图片1'
  },
  {
    url: 'https://s2.loli.net/2021/12/10/uyKi9V7hlfDJnO4.png',
    imgTitle: '图片2'
  },
  {
    url: 'https://img.ljcdn.com/beike/crep_c/1625043131421.jpg',
    imgTitle: '图片3'
  }
]
export default class index extends Component {
  constructor(props) {
    super(props)
    this.state = {
      showImgPreview: false,
      imgIndex: 0
    }
  }
  openImgPreview = (index) => {
    this.setState({
      showImgPreview: true,
      imgIndex: index
    })
  }
  render() {
    return (
      <div>
        <ul>
          {imgData.map((item, index) => {
            return <img
              key={index}
              style={{ width: '100px', height: '100px' }}
              src={item.url}
              onClick={this.openImgPreview.bind(this, index)}></img>
          })}
        </ul>
        {this.state.showImgPreview ?
          <ImgPreview imageArr={imgData}
            imgIndex={this.state.imgIndex}
            showThumbnail={false} /> : null}
      </div>
    )
  }
}