React 简易图片查看器的实现

效果图如上

react.tsx

import React from 'react';
import { message, Spin } from 'antd';
import { StepBackwardOutlined, StepForwardOutlined, CloseOutlined, ReloadOutlined } from '@ant-design/icons'
import styles from './index.less';


export interface ImgPath {
  fileKey: string;
  filePath: string;
}
interface ViewerProps {
  imgUrls: ImgPath[];
  onClose: () => void;
  viewVisible: boolean;
}
interface viewerState {
  imgUrls: ImgPath[];
  current: number;
  imgLoaded: boolean;
  transform: number;
};
class RcViewer extends React.Component<ViewerProps>{
  state: viewerState = {
    imgUrls: [],
    current: 0,
    imgLoaded: false,
    transform: 0,
  };
  constructor(props: ViewerProps) {
    super(props);
  };
  static getDerivedStateFromProps(nextProps: any, prevState: any) {
    if (nextProps.imgUrls) {
      return {
        imgUrls: nextProps.imgUrls,
      };
    }
    return null;
  }
  pre = () => {
    const { current, imgUrls } = this.state;
    let index = current;
    index--;
    if (index < 0) {
      message.error("没有上一张啦");
      index = imgUrls.length - 1;
    }
    this.setState({
      current: index,
    })
  };
  next = () => {
    const { current, imgUrls } = this.state;
    let index = current;
    index++;
    if (index > imgUrls.length - 1) {
      message.error("这是最后一张");
      index = 0;
    }
    this.setState({
      current: index,
    })
  };
  close = () => {
    const { onClose } = this.props;
    onClose();
    this.setState({
      imgUrls: [],
      transform: 0,
    })
  };
  handleImgLoad = () => {
    this.setState({
      imgLoaded: true,
    })
  };
  hanleTrans = () => {
    this.setState({
      transform: this.state.transform + 90,
    })
  };
  render() {
    const { imgUrls, current, imgLoaded, transform } = this.state;
    const { viewVisible } = this.props;
    return (
      <div className={styles.content} style={{ display: viewVisible ? 'block' : 'none' }}>
        <div className={styles.shadeBox}>
          <div className={styles.imgoutsideBox} >
            <div className={styles.imgBox}>
              {!imgLoaded && <Spin />}
              <img
                src={imgUrls[current] ? imgUrls[current].filePath : ''}
                onLoad={this.handleImgLoad}
                title="图片"
                style={{ display: imgLoaded ? 'block' : 'none', transform: `rotate(${transform}deg)` }} />
            </div>
            <div className={styles.iconStyle} onClick={this.pre}>
              <StepBackwardOutlined title="上一张" />
            </div>
            <div className={styles.iconStyle} onClick={this.next} >
              <StepForwardOutlined title="下一张" />
            </div>
            <div className={styles.iconStyle} onClick={this.hanleTrans} >
              <ReloadOutlined title="旋转" />
            </div>
          </div>
          <div className={styles.closeIcon} onClick={this.close}>
            <CloseOutlined />
          </div>
        </div>
      </div>
    )
  }
};
export default RcViewer;

index.less 

.content{
  position: fixed;
  top:0px;
  left: 0px;
  width: 100%;
  height: 100%;
  z-index: 9999;
  .shadeBox{
    box-sizing: border-box;
    width: 100%;
    height: 100%;
    margin: 0 auto;
    padding: 0;
    color: rgba(0, 0, 0, 0.65);
    list-style: none;
    font-feature-settings: 'tnum';
    position: absolute;
    top:64px;
    bottom: 64px;
    z-index: 1060;
    display: block;
    min-width: 250px;
    min-height: 250px;
    visibility: visible;
  }
  .iconStyle{
    display: inline-block;
    height: 2rem;
    width: 2rem;
    margin: 10px;
    line-height: 2rem;
    border:1px solid #ddd;
    text-align: center;
    border-radius: 50%;
    background-color: #fff;
    cursor: pointer; 
    z-index: 999;
  }
  .iconStyle:active{
    color:#108ee9;
  }
  .imgoutsideBox{
    box-sizing:border-box;
    width: 70%;
    height: 70%;
    margin:20px auto;
    padding:46px 28px;
    text-align: center;
    background-color: rgba(0, 0, 0, 0.75);
    border-radius: 2px;
    box-shadow: 0 3px 6px -4px rgba(0, 0, 0, 0.12), 0 6px 16px 0 rgba(0, 0, 0, 0.08), 0 9px 28px 8px rgba(0, 0, 0, 0.05);
    overflow: hidden;
    .imgBox{
      width: 100%;
      height: 100%;
      overflow: hidden;
      img{
        // width:100%;
        display: block;
        margin: 0 auto;
        height: 100%;
        cursor: pointer;
      }
    }
  }
  .closeIcon{
    display: block;
    height: 3rem;
    width: 3rem;
    line-height: 3rem;
    text-align: center;
    border-radius: 50%;
    border: 1.5px solid #ddd;
    background-color:#fff;
    cursor: pointer; 
    margin: 30px auto 0 auto;
  }
  .closeIcon:active{
    color:#108ee9;
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于 React 图片查看组件,你可以使用第三方库来实现,比如 react-image-gallery 或者 react-lightbox。这些库提供了丰富的功能和样式,可以帮助你实现图片的缩放、旋转、轮播等效果。 具体步骤如下: 1. 首先,安装所需的库。你可以使用 npm 或者 yarn 进行安装,比如运行以下命令来安装 react-image-gallery: ``` npm install react-image-gallery ``` 2. 在你的组件中引入所需的库,比如: ```jsx import React from 'react'; import ImageGallery from 'react-image-gallery'; ``` 3. 创建一个图片数组,用于展示图片。每个图片对象应该包含图片的 URL、标题、描述等信息。比如: ```jsx const images = [ { original: 'http://example.com/image1.jpg', thumbnail: 'http://example.com/thumb1.jpg', description: 'Image 1' }, { original: 'http://example.com/image2.jpg', thumbnail: 'http://example.com/thumb2.jpg', description: 'Image 2' }, // 其他图片... ]; ``` 4. 在你的组件中使用 ImageGallery 组件,并将图片数组作为 props 传递给它。比如: ```jsx function MyImageGallery() { return ( <div> <h1>图片查看</h1> <ImageGallery items={images} /> </div> ); } ``` 5. 根据需要,你还可以配置 ImageGallery 组件的其他属性来自定义样式和行为。比如,你可以设置缩略图的大小、缩放模式、是否显示标题等。具体可配置项可以参考库的文档。 这样,你就可以在你的 React 应用中使用这个图片查看组件了。记得根据你选择的库,查阅其文档以获取更多用法和配置信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值