MinJieLiu / react-photo-view

An exquisite React photo preview component.
https://react-photo-view.vercel.app
Apache License 2.0
1.57k stars 130 forks source link

使用自定义渲染节点-放大节点时,往左偏移,并且不能拖动 #111

Closed pureHeart11 closed 2 years ago

pureHeart11 commented 2 years ago

使用自定义渲染节点-每次放大节点时,都会往屏幕左上角偏移,直至最后看不到,并且也拖动不了,以下是源码,和文档demo完全一样:(版本v1.2)

import React, { useEffect, useState, useMemo } from 'react';
import { Button } from 'antd';
import { PhotoProvider, PhotoView } from 'react-photo-view';
import 'react-photo-view/dist/react-photo-view.css';

const elementSize = 400;

const Index = () => {
  return (
    <PhotoProvider>
      <PhotoView
        width={elementSize}
        height={elementSize}
        render={({ scale, attrs }) => {
          const width = attrs.style.width;
          const offset = (width - elementSize) / elementSize;
          const childScale = scale === 1 ? scale + offset : 1 + offset;

          return (
            <div {...attrs}>
              <div style={{ transform: `scale(${childScale})` }}>
                <div>Hello world</div>
                <Button>button</Button>
                <input onMouseDown={e => e.stopPropagation()} />
              </div>
            </div>
          );
        }}
      >
        <Button primary>点击打开</Button>
      </PhotoView>
    </PhotoProvider>
  );
};

export default Index;
MinJieLiu commented 2 years ago
return (
  <div {...attrs}>
    <div style={{ transform: `scale(${childScale})`, width: elementSize, transformOrigin: '0 0' }}>
      <div>Hello world</div>
      <Button>button</Button>
      <input onMouseDown={e => e.stopPropagation()} />
    </div>
  </div>
);

添加 width: elementSize, transformOrigin: '0 0'