react使用拖拽,缩放组件,采用react-rnd解决

news/2025/2/25 15:02:20

项目中需求,要求给商品图片添加促销标签,并且可拉伸大小,和拖拽位置
最后选择用react-rnd来实现
话不多说,直接上代码!!!

1.在项目根目录下执行以下代码,引入react-rnd

yarn add react-rnd

2.在项目中直接引用,以下是最简单的示例

import React, { Component } from 'react';
import { Rnd } from 'react-rnd';

interface State {
    /**
     * 正在操作中
     */
    isDragging: boolean;
    /**
     * 拉伸大小
     */
    size: {
        width: number;
        height: number;
    };
    /**
     * 位置
     */
    position: {
        x: number;
        y: number;
    };
}
export class ProductMainImageWatermarkRulePreview extends Component<unknown, State> {
    constructor(props: any) {
        super(props);
        this.state = {
            isDragging: false,
            size: {
                width: 100,
                height: 100,
            },
            position: {
                x: 0,
                y: 0,
            },
        };
    }
    handleDragStart = () => {
        this.setState({
            isDragging: true,
        });
    };
    handleDragStop = (e: any, d: any) => {
        this.setState({
            isDragging: false,
            position: {
                x: d.x,
                y: d.y,
            },
        });
    };
    handleResizeStart = () => {
        this.setState({
            isDragging: true,
        });
    };
    handleResizeStop = (e: any, direction: any, ref: any, delta: any, position: any) => {
        this.setState({
            isDragging: false,
            size: {
                width: ref.style.width,
                height: ref.style.height,
            },
            position: position,
        });
    };
    render() {
        const { position, isDragging, size } = this.state;
        return (
            <div style={{ width: 375, height: 375, backgroundColor: 'gray' }}>
                <Rnd
                    // default={{ x: position.x, y: position.y, width: size.width, height: size.height }}
                    // minHeight={1}
                    // maxHeight={375}
                    // minWidth={1}
                    // maxWidth={375}
                    size={size}
                    position={position}
                    bounds="parent"
                    onDragStart={this.handleDragStart}
                    onDragStop={this.handleDragStop}
                    onResizeStart={this.handleResizeStart}
                    onResizeStop={this.handleResizeStop}
                    resizeParentMore={true} // 如果需要阻止父容器跟随大小变化,可以设置为false
                    enableResizing={{
                        top: true,
                        right: true,
                        bottom: true,
                        left: true,
                        topRight: true,
                        bottomRight: true,
                        bottomLeft: true,
                        topLeft: true,
                    }}
                    resizeHandles={['se', 'sw', 'ne', 'nw']}
                    style={{ opacity: isDragging ? 0.8 : 1 }}
                    onClick={(e: any) => e.stopPropagation()}
                >
                    <div>
                        <img
                            src="https://b-puzhehei-cdn.co-mall.net/global/magic-backend/invitation-activity/button-picture.png"
                            alt=""
                            style={{ width: '100%', height: '100%' }}
                        />
                    </div>
                </Rnd>
            </div>
        );
    }
}

实现效果如下
在这里插入图片描述

还有一些属性,罗列出来,希望对大家有帮助~

default: { x: number; y: number; width?: number | string; height?: number | string;};                设定默认的一些属性,如初始坐标和宽高
size?: { width: (number | string), height: (number | string) };  组件的大小,即宽度与高度
position?: { x: number, y: number };  组件的坐标,即横坐标与纵坐标
resizeGrid?: [number, number];   重置大小时的增量,默认为[1, 1]
dragGrid?: [number, number];    拖拽时的增量,默认为[1, 1]
lockAspectRatio?: boolean | number;    锁定纵横比,可设置为布尔值或数字值,当设置为true时,组件会锁定纵横比,并且该值为组件初始宽高的比值;而设置为数字时,组件调整大小时会以该值作为纵横比来调整
enableResizing?: ?Enable   用以设置是否可调整大小,可从组件各个方向上或整体来设置:
disableDragging?: boolean;     是否禁用拖拽
dragAxis?: 'x' | 'y' | 'both' | 'none'    设置组件的拖拽方向
bounds?: string;    组件的边界:可设置为父组件的名称或者window、body或者一个选择器的名称(需要带上符号. or #)

方法

onResizeStart?: RndResizeStartCallback; // 开始调整大小时调用

onResize?: RndResizeCallback; // 组件调整大小时调用

onResizeStop?: RndResizeCallback; // 组件停止调整大小时调用

onDragStart: DraggableEventHandler; // 组件开始拖拽时调用

onDrag: DraggableEventHandler; // 组件拖拽时调用

onDragStop: DraggableEventHandler; // 组件停止拖拽时调用

http://www.niftyadmin.cn/n/5865631.html

相关文章

Java 大视界 -- 深入剖析 Java 大数据实时 ETL 中的数据质量保障策略(97)

&#x1f496;亲爱的朋友们&#xff0c;热烈欢迎来到 青云交的博客&#xff01;能与诸位在此相逢&#xff0c;我倍感荣幸。在这飞速更迭的时代&#xff0c;我们都渴望一方心灵净土&#xff0c;而 我的博客 正是这样温暖的所在。这里为你呈上趣味与实用兼具的知识&#xff0c;也…

fps动作系统4.1:移动系统

文章目录 移动控制&#xff1a;只需要将输入传给移动组件即可实现add movement input&#xff1a;将输入传给移动组件 场景坐标系如同东南西北&#xff0c;在这个场景也有绝对的xyz。 箭头&#xff1a;物体的单位方向向量。默认指向场景x。旋转体&#xff1a;向量旋转过的角度。…

HarmonyOS使用系统图标

HarmonyOS图标符号是系统内置的一套图标资源库。开发者可以通过图标的资源名称&#xff0c;利用SymbolGlyph和SymbolSpan组件可以高效索引并使用相应的图标。使用HarmonyOS Symbol&#xff0c;开发者不仅可以轻松地通过图标名称引用图标资源&#xff0c;从而简化开发流程。还能…

鸿蒙开发深入浅出03(封装通用LazyForEach实现懒加载)

鸿蒙开发深入浅出03&#xff08;封装通用LazyForEach实现懒加载&#xff09; 1、效果展示2、ets/models/BasicDataSource.ets3、ets/models/HomeData.ets4、ets/api/home.ets5、ets/pages/Home.ets6、ets/views/Home/SwiperLayout.ets7、后端代码 1、效果展示 2、ets/models/Ba…

bind()函数的概念和使用案例

在计算机网络编程中&#xff0c;bind() 是一个用于将一个套接字&#xff08;socket&#xff09;与一个特定的网络地址和端口号关联起来的系统调用。这个函数通常在服务器端编程中使用&#xff0c;用于指定服务器将监听哪个网络接口和端口号上的连接请求。 bind() 的概念 套接…

2007年诺基亚内部对iPhone的竞争分析报告

2007年iPhone发布后&#xff0c;诺基亚内部至少有9名员工指出其触屏界面、互联网整合能力将颠覆市场&#xff0c;并建议开发同类产品&#xff0c;但高管因当时占据全球50%市场份额而轻视威胁&#xff0c;认为苹果的高价和虚拟键盘会限制其普及。 诺基亚虽然意识到需推出触屏手机…

angular日历

说明: 写一个简单的日历功能 效果图&#xff1a; step1:C:\Users\Administrator\WebstormProjects\untitled4\src\app\calendar\calendar.component.ts import { Component, signal } from angular/core; import { CommonModule } from angular/common; import { MatButtonM…

中国的Cursor! 字节跳动推出Trae,开放Windows版(附资源),开发自己的网站,内置 GPT-4o 强大Al模型!

Trae是什么 Trae 是字节跳动推出的免费 AI IDE&#xff0c;通过 AI 技术提升开发效率。支持中文&#xff0c;集成了 Claude 3.5 和 GPT-4 等主流 AI 模型&#xff0c;完全免费使用。Trae 的主要功能包括 Builder 模式和 Chat 模式&#xff0c;其中 Builder 模式可帮助开发者从…