当前位置:首页 > React

react实现图片视频预览

2026-01-27 14:16:52React

实现图片预览

在React中实现图片预览可以使用第三方库如react-image-lightbox或自定义模态框组件。以下是使用react-image-lightbox的示例:

安装依赖:

npm install react-image-lightbox

代码示例:

react实现图片视频预览

import React, { useState } from 'react';
import Lightbox from 'react-image-lightbox';
import 'react-image-lightbox/style.css';

function ImagePreview() {
  const [isOpen, setIsOpen] = useState(false);
  const images = [
    'image1.jpg',
    'image2.jpg',
    'image3.jpg'
  ];
  const [photoIndex, setPhotoIndex] = useState(0);

  return (
    <div>
      <button onClick={() => setIsOpen(true)}>
        Open Image Viewer
      </button>

      {isOpen && (
        <Lightbox
          mainSrc={images[photoIndex]}
          nextSrc={images[(photoIndex + 1) % images.length]}
          prevSrc={images[(photoIndex + images.length - 1) % images.length]}
          onCloseRequest={() => setIsOpen(false)}
          onMovePrevRequest={() =>
            setPhotoIndex((photoIndex + images.length - 1) % images.length)
          }
          onMoveNextRequest={() =>
            setPhotoIndex((photoIndex + 1) % images.length)
          }
        />
      )}
    </div>
  );
}

实现视频预览

对于视频预览,可以使用HTML5的video标签结合模态框实现:

import React, { useState } from 'react';
import Modal from 'react-modal';

Modal.setAppElement('#root');

function VideoPreview() {
  const [modalIsOpen, setModalIsOpen] = useState(false);

  return (
    <div>
      <button onClick={() => setModalIsOpen(true)}>
        Play Video
      </button>

      <Modal
        isOpen={modalIsOpen}
        onRequestClose={() => setModalIsOpen(false)}
        contentLabel="Video Preview"
      >
        <video width="100%" controls>
          <source src="video.mp4" type="video/mp4" />
          Your browser does not support the video tag.
        </video>
        <button onClick={() => setModalIsOpen(false)}>Close</button>
      </Modal>
    </div>
  );
}

自定义预览组件

如果需要完全自定义的预览功能,可以创建自己的组件:

react实现图片视频预览

import React, { useState } from 'react';

function MediaPreview({ type, src }) {
  const [isPreviewOpen, setIsPreviewOpen] = useState(false);

  return (
    <div>
      {type === 'image' ? (
        <img 
          src={src} 
          alt="Preview" 
          onClick={() => setIsPreviewOpen(true)}
          style={{ cursor: 'pointer', maxWidth: '200px' }}
        />
      ) : (
        <div onClick={() => setIsPreviewOpen(true)}>
          <video 
            src={src} 
            style={{ maxWidth: '200px', cursor: 'pointer' }}
          />
          <div>Click to play</div>
        </div>
      )}

      {isPreviewOpen && (
        <div className="preview-overlay">
          <div className="preview-content">
            <button onClick={() => setIsPreviewOpen(false)}>X</button>
            {type === 'image' ? (
              <img src={src} alt="Full Preview" />
            ) : (
              <video src={src} controls autoPlay />
            )}
          </div>
        </div>
      )}
    </div>
  );
}

使用react-player处理视频

对于更复杂的视频需求,可以使用react-player库:

安装依赖:

npm install react-player

使用示例:

import React, { useState } from 'react';
import ReactPlayer from 'react-player';
import Modal from 'react-modal';

Modal.setAppElement('#root');

function VideoPlayer() {
  const [isOpen, setIsOpen] = useState(false);

  return (
    <div>
      <button onClick={() => setIsOpen(true)}>Play Video</button>

      <Modal
        isOpen={isOpen}
        onRequestClose={() => setIsOpen(false)}
      >
        <ReactPlayer
          url="https://example.com/video.mp4"
          controls
          width="100%"
          height="100%"
        />
        <button onClick={() => setIsOpen(false)}>Close</button>
      </Modal>
    </div>
  );
}

这些方法提供了不同级别的功能,可以根据项目需求选择合适的实现方式。

标签: 图片视频
分享给朋友:

相关文章

vue实现图片

vue实现图片

Vue 实现图片功能的方法 静态图片引入 在 Vue 项目中,可以通过 src 属性直接引入静态图片。图片通常放在 public 或 assets 目录下。 <template> &…

vue实现视频

vue实现视频

Vue 实现视频播放的方法 在 Vue 项目中实现视频播放可以通过多种方式完成,以下是几种常见的方法: 使用 HTML5 的 <video> 标签 在 Vue 组件中直接使用 HT…

css动画制作图片

css动画制作图片

CSS动画制作图片的方法 使用CSS动画可以为图片添加各种动态效果,提升网页的视觉吸引力。以下是几种常见的CSS动画实现方式: 关键帧动画(@keyframes) 通过定义关键帧来控制动画的各个阶…

h5怎么实现上传视频

h5怎么实现上传视频

使用HTML5实现视频上传 HTML5提供了<input type="file">元素用于文件上传,结合<video>标签可以实现视频上传和预览功能。 <input t…

vue实现视频

vue实现视频

Vue 实现视频播放功能 Vue 可以通过集成第三方库或直接使用 HTML5 的 <video> 标签来实现视频播放功能。以下是几种常见的方法: 使用 HTML5 <video&g…

vue上传视频实现

vue上传视频实现

使用 <input type="file"> 上传视频 创建文件选择控件,限制文件类型为视频格式(如 .mp4, .webm)。通过 @change 事件监听文件选择,获取 File 对象…