react 对img图片进行放大 缩小 拖拽

javascript 复制代码
import React, { Component } from 'react';
import BaseComponent from "../../../../base/BaseComponent";
import zoomInIcon from "./images/zoom-in.png";
import zoomOutIcon from "./images/zoom-out.png";

export default class DetailPicture extends BaseComponent {
    static defaultProps = {};
    static propTypes = {};

    constructor(props) {
        super(props);
        this.state = {
            zoomLevel: 100, // 初始缩放级别为100%
            dragging: false,
            offsetX: 0,
            offsetY: 0,
            translateX: 0,
            translateY: 0,
            lastTranslateX: 0,
            lastTranslateY: 0,
            scale: 1, // 缩放比例
        };
    }

    handleZoomIn = () => {
        // 将缩放级别增加20%
        this.setState(prevState => ({
            zoomLevel: Math.min(prevState.zoomLevel + 20, 500),
            scale: (prevState.zoomLevel + 20) / 100,
        }));
    };

    handleZoomOut = () => {
        // 将缩放级别减小20%
        this.setState(prevState => ({
            zoomLevel: Math.max(prevState.zoomLevel - 20, 20),
            scale: (prevState.zoomLevel - 20) / 100,
        }));
    };

    handleMouseDown = (event) => {
        event.preventDefault();
        this.setState({
            dragging: true,
            offsetX: event.clientX,
            offsetY: event.clientY,
            lastTranslateX: this.state.translateX,
            lastTranslateY: this.state.translateY,
        });
        document.addEventListener('mousemove', this.handleMouseMove);
        document.addEventListener('mouseup', this.handleMouseUp);
    };

    handleMouseMove = (event) => {
        if (this.state.dragging) {
            const { offsetX, offsetY, lastTranslateX, lastTranslateY, scale } = this.state;
            const dx = (event.clientX - offsetX) / scale;
            const dy = (event.clientY - offsetY) / scale;
            this.setState({
                translateX: lastTranslateX + dx,
                translateY: lastTranslateY + dy,
            });
        }
    };

    handleMouseUp = () => {
        this.setState({ dragging: false });
        document.removeEventListener('mousemove', this.handleMouseMove);
        document.removeEventListener('mouseup', this.handleMouseUp);
    };


    render() {
        const { detailData = {} } = this.props;
        const { storageFile = {} } = detailData;
        const { path = "" } = storageFile;
        const { scale , translateX , translateY} = this.state;

        return (
            <div style={{ position: 'relative', width: '100%', height: '100%', display: 'flex' }}>
                <img
                    src={path}
                    style={{
                        maxWidth: '100%',
                        maxHeight: '100%',
                        margin: 'auto',
                        transform: `scale(${scale}) translate(${translateX}px, ${translateY}px)`, // 根据缩放级别和平移应用变换
                        transition: 'transform 0.3s ease-in-out', // 添加平滑的过渡效果
                        cursor: this.state.dragging ? 'grabbing' : 'grab',
                    }}
                    id={this.props.id}
                    onMouseDown={this.handleMouseDown}
                />

                <div style={{ position: 'absolute', top: 10, right: 10, zIndex: 1 }}>
                    <img
                        src={zoomOutIcon}
                        alt="Zoom In"
                        onClick={this.handleZoomIn}
                        style={{ cursor: 'pointer', marginRight: '5px' }}
                    />
                    <img
                        src={zoomInIcon}
                        alt="Zoom Out"
                        onClick={this.handleZoomOut}
                        style={{ cursor: 'pointer' }}
                    />
                </div>
            </div>
        );
    }
}

效果展示

可放大 缩小 并且拖拽

相关推荐
EricWang13582 分钟前
[OS] 项目三-2-proc.c: exit(int status)
服务器·c语言·前端
September_ning2 分钟前
React.lazy() 懒加载
前端·react.js·前端框架
web行路人12 分钟前
React中类组件和函数组件的理解和区别
前端·javascript·react.js·前端框架
超雄代码狂34 分钟前
ajax关于axios库的运用小案例
前端·javascript·ajax
长弓三石42 分钟前
鸿蒙网络编程系列44-仓颉版HttpRequest上传文件示例
前端·网络·华为·harmonyos·鸿蒙
小马哥编程43 分钟前
【前端基础】CSS基础
前端·css
嚣张农民1 小时前
推荐3个实用的760°全景框架
前端·vue.js·程序员
周亚鑫1 小时前
vue3 pdf base64转成文件流打开
前端·javascript·pdf
Justinc.2 小时前
CSS3新增边框属性(五)
前端·css·css3
neter.asia2 小时前
vue中如何关闭eslint检测?
前端·javascript·vue.js