React 图像查看器,支持旋转、缩放、放大等功能
[![NPM 版本][npm-image]]npm-url
React 图像查看器。
因为我无法在 React 中舒服地使用 viewerjs,所以我创建了 React-viewer 来替代它。
React >= 16.8.0 | React-dom >= 16.8.0
npm install react-viewer --saveimport * as React from 'react';
import Viewer from 'react-viewer';
function App() {
const [ visible, setVisible ] = React.useState(false);
return (
);
}React-viewer 使用浏览器 DOM API,并且必须在禁用 SSR 的情况下加载。在
Next.js App Router, 将动态导入放置在客户端组件中:
'use client';
import dynamic from 'next/dynamic';
const ReactViewer = dynamic(
() => import('react-viewer').then((mod) => mod.default),
{ ssr: false },
);
export default function Viewer() {
return (
{}}
images={[{src: ''}]}
/>
);
}页面路由器使用相同的动态导入,但不需要
'use client' 指令。3.x 不支持直接服务器渲染。
| props | type | 默认值 | 描述 | 是否必需 |
|---|---|---|---|---|
| visible | boolean | false | 显示在查看器中 | true |
| onClose | function | - | 指定在 Visible 关闭时调用的函数 | true |
| images | ImageDecorator[] | [] | 图像源数组 | true |
| activeIndex | number | 0 | 当前图像索引 | false |
| zIndex | number | 1000 | 视图器 CSS z-index | false |
| container | HTMLElement | null | 设置父节点(内联模式) | false |
| drag | boolean | true | 是否拖动图像 | false |
| attribute | boolean | true | 是否显示图像属性 | false |
| zoomable | boolean | true | 是否显示"放大"按钮 | false |
| 可旋转 | 布尔值 |
暂无开放 Issues,或尚未同步最近议题。