iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >React拖拽调整大小的组件
  • 244
分享到

React拖拽调整大小的组件

2024-04-02 19:04:59 244人浏览 独家记忆
摘要

本文实例为大家分享了React拖拽调整大小的组件,供大家参考,具体内容如下 一、实现流程 1.使用React.cloneElement加强包裹组件,在包裹的组件设置绝对定位,并在组件

本文实例为大家分享了React拖拽调整大小的组件,供大家参考,具体内容如下

一、实现流程

1.使用React.cloneElement加强包裹组件,在包裹的组件设置绝对定位,并在组件内加上四个可调整大小的拖动条,在点击拖动条并进行拖动时会改变DragBox的大小,如下:

2.使用:

<DragBox dragAble={true} minWidth={350} minHeight={184} edgeDistance={[10, 10, 10, 10]} draGCallback={this.dragCallback} >
    {}
   <div style={{ top: 100 + 'px', left: 100 + 'px', width: 350, height: 184, backgroundColor: "white" }}>
        <div style={{ backgroundColor: "yellow", width: "100%", height: 30 }}></div>
        <div style={{ backgroundColor: "green", width: "100%", height: 30 }}></div>
        <div style={{ backgroundColor: "blue", width: "100%", height: 30 }}></div>
    </div>
</DragBox>

二、代码

DragBox组件

import React, { Component, Fragment } from 'react';
import styles from "./DragBox.less";


class DragBox extends Component {
    constructor(props) {
        super(props);
        // 父组件盒子
        this.containerRef = React.createRef();
        // 是否开启尺寸修改
        this.reSizeAble = false;
        // 鼠标按下时的坐标,并在修改尺寸时保存上一个鼠标的位置
        this.clientX, this.clientY;
        // 鼠标按下时的位置,使用n、s、w、e表示
        this.direction = "";
        // 拖拽盒子里浏览器上下左右边缘的距离,如果小于这个距离就不会再进行调整宽高
        this.edgeTopDistance = props.edgeDistance[0] || 10;
        this.edgeBottomDistance = props.edgeDistance[1] || 10;
        this.edgeLeftDistance = props.edgeDistance[2] || 10;
        this.edgeRightDistance = props.edgeDistance[3] || 10;
    }

    componentDidMount(){
        // body监听移动事件
        document.body.addEventListener('mousemove', this.move);
        // 鼠标松开事件
        document.body.addEventListener('mouseup', this.up);
    }

    
    clearEventListener() {
        document.body.removeEventListener('mousemove', this.move);
        document.body.removeEventListener('mouseup', this.up);
    }

    componentWillUnmount() {
        this.clearEventListener();
    }

    
    up = () => {
        this.reSizeAble = false;
        this.direction = "";
    }

    
    down = (e, direction) => {
        this.direction = direction;
        this.reSizeAble = true;
        this.clientX = e.clientX;
        this.clientY = e.clientY;
    }

    
    changeLeftAndTop = (event, changeLeft, changeTop, delta) => {
        let ww = document.documentElement.clientWidth;
        let wh = window.innerHeight;

        if (event.clientY < 0 || event.clientX < 0 || event.clientY > wh || event.clientX > ww) {
            return false;
        }
        if (changeLeft) { 
            this.containerRef.current.style.left = Math.max(this.containerRef.current.offsetLeft + delta, this.edgeLeftDistance) + 'px'; 
        }
        if (changeTop) { 
            this.containerRef.current.style.top = Math.max(this.containerRef.current.offsetTop + delta, this.edgeTopDistance) + 'px'; 
        }
    }

    
    move = (e) => {
        // 当开启尺寸修改时,鼠标移动会修改div尺寸
        if (this.reSizeAble) {
            let finalValue;
            // 鼠标按下的位置在上部,修改高度
            if (this.direction === "top") {
                // 1.距离上边缘10 不修改
                // 2.因为按着顶部修改高度会修改top、height,所以需要判断e.clientY是否在offsetTop和this.clientY之间(此时说明处于往上移动且鼠标位置在盒子上边缘之下),不应该移动和调整盒子宽高
                if (e.clientY <= this.edgeTopDistance || (this.containerRef.current.offsetTop < e.clientY && e.clientY  < this.clientY)){ 
                    this.clientY = e.clientY;
                    return; 
                }
                finalValue = Math.max(this.props.minHeight, this.containerRef.current.offsetHeight + (this.clientY - e.clientY));
                // 移动的距离,如果移动的距离不为0需要调整高度和top
                let delta = this.containerRef.current.offsetHeight - finalValue;
                if(delta !== 0){
                    this.changeLeftAndTop(e, false, true, delta); 
                    this.containerRef.current.style.height = finalValue + "px";
                }
                this.clientY = e.clientY;
            } else if (this.direction === "bottom") {// 鼠标按下的位置在底部,修改高度
                // 1.距离下边缘10 不修改
                // 2.判断e.clientY是否处于往下移动且鼠标位置在盒子下边缘之上,不应该调整盒子宽高
                if (window.innerHeight - e.clientY <= this.edgeBottomDistance || (this.containerRef.current.offsetTop + this.containerRef.current.offsetHeight > e.clientY && e.clientY  > this.clientY)) { 
                    this.clientY = e.clientY;
                    return; 
                }
                finalValue = Math.max(this.props.minHeight, this.containerRef.current.offsetHeight + (e.clientY - this.clientY));
                this.containerRef.current.style.height = finalValue + "px";
                this.clientY = e.clientY;
            } else if (this.direction === "right") { // 鼠标按下的位置在右边,修改宽度 
                // 1.距离右边缘10 不修改
                // 2.判断e.clientY是否处于往右移动且鼠标位置在盒子右边缘之左,不应该调整盒子宽高
                if (document.documentElement.clientWidth - e.clientX <= this.edgeRightDistance || (this.containerRef.current.offsetLeft + this.containerRef.current.offsetWidth > e.clientX && e.clientX  > this.clientX)) { 
                    this.clientX = e.clientX;
                    return;
                }
                // 最小为UI设计this.props.minWidth,最大为 改边距离屏幕边缘-10,其他同此
                let value = this.containerRef.current.offsetWidth + (e.clientX - this.clientX);
                finalValue = step(value, this.props.minWidth, document.body.clientWidth - this.edgeRightDistance - this.containerRef.current.offsetLeft);
                this.containerRef.current.style.width = finalValue + "px";
                this.clientX = e.clientX;
            } else if (this.direction === "left") {// 鼠标按下的位置在左边,修改宽度
                // 1.距离左边缘10 不修改
                // 2.因为按着顶部修改高度会修改left、height,所以需要判断e.clientY是否在offsetLeft和this.clientY之间(此时说明处于往左移动且鼠标位置在盒子左边缘之左),不应该移动和调整盒子宽高
                if (e.clientX <= this.edgeLeftDistance || (this.containerRef.current.offsetLeft < e.clientX && e.clientX  < this.clientX)) { 
                    this.clientX = e.clientX;
                    return; 
                }
                let value = this.containerRef.current.offsetWidth + (this.clientX - e.clientX);
                finalValue = step(value, this.props.minWidth, this.containerRef.current.offsetWidth - this.edgeLeftDistance + this.containerRef.current.offsetLeft);
                // 移动的距离,如果移动的距离不为0需要调整宽度和left
                let delta = this.containerRef.current.offsetWidth - finalValue;
                if(delta !== 0){
                    // 需要修改位置,直接修改宽度只会向右增加
                    this.changeLeftAndTop(e, true, false, delta); 
                    this.containerRef.current.style.width = finalValue + "px";
                }
                this.clientX = e.clientX;
            }
            this.props.dragCallback && this.props.dragCallback(this.direction, finalValue);
        }
    }

    render() {
        // 四个红色盒子 用于鼠标移动到上面按下进行拖动
        const children = (
            <Fragment key={"alphaBar"}>
                <div key={1} className={styles.alphaTopBar} onMouseDown={(e) => this.down(e, "top")}></div>
                <div key={2} className={styles.alphaBottomBar} onMouseDown={(e) => this.down(e, "bottom")}></div>
                <div key={3} className={styles.alphaLeftBar} onMouseDown={(e) => this.down(e, "left")}></div>
                <div key={4} className={styles.alphaRightBar} onMouseDown={(e) => this.down(e, "right")}></div>
            </Fragment>
        );

        // 给传进来的children进行加强:添加position:"absolute",添加四个用于拖动的透明盒子
        const childrenProps = this.props.children.props;

        const cloneReactElement = React.cloneElement(
            this.props.children,
            {
                style: {
                    // 复用原来的样式
                    ...childrenProps.style,
                    // 添加position:"absolute"
                    position: "absolute"
                },
                ref: this.containerRef
            },
            // 复用children,添加四个用于拖动的红色盒子
            [childrenProps.children, children]
        );

        return (
            <Fragment>
                {
                    cloneReactElement
                }
            </Fragment>
        );
    }
}


function step(value, min, max) {
    if (value < min) {
        return min;
    } else if (value > max) {
        return max;
    } else {
        return value;
    }
}

export default DragBox;

### DragBox组件拖动条的样式

.alphaTopBar{
    position: absolute;
    width: 100%;
    height: 8px;
    top: -5px;
    left: 0;
    background-color: red;
    cursor: row-resize;
  }
  .alphaBottomBar{
    position: absolute;
    width: 100%;
    height: 8px;
    bottom: -5px;
    left: 0;
    background-color: red;
    cursor: row-resize;
  }
  .alphaLeftBar{
    position: absolute;
    width: 8px;
    height: 100%;
    top: 0;
    left: -5px;
    background-color: red;
    cursor: col-resize;
  }
  .alphaRightBar{
    position: absolute;
    width: 8px;
    height: 100%;
    top: 0;
    right: -5px;
    background-color: red;
    cursor: col-resize;
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: React拖拽调整大小的组件

本文链接: https://www.lsjlt.com/news/166038.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • React拖拽调整大小的组件
    本文实例为大家分享了React拖拽调整大小的组件,供大家参考,具体内容如下 一、实现流程 1.使用React.cloneElement加强包裹组件,在包裹的组件设置绝对定位,并在组件...
    99+
    2024-04-02
  • react拖拽组件react-sortable-hoc的使用
    目录1.文件12.文件23.使用使用react-sortable-hoc实现拖拽 如图: 提示:下面案例可供参考 1.文件1 代码如下(示例):文件名称:./dragcompone...
    99+
    2023-02-24
    react拖拽组件 react sortable-hoc
  • react拖拽组件react-sortable-hoc如何使用
    这篇“react拖拽组件react-sortable-hoc如何使用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“react...
    99+
    2023-07-05
  • react-beautiful-dnd 实现组件拖拽功能
    目录1.安装2.APi3.react-beautiful-dnd demo3.1 demo1 纵向组件拖拽3.2 demo2 水平拖拽3.3 demo3实现一个代办事项的拖拽(纵向 ...
    99+
    2024-04-02
  • react-beautiful-dnd如何实现组件拖拽
    这篇文章将为大家详细讲解有关react-beautiful-dnd如何实现组件拖拽,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。1.安装在已有react项目中 执行以下命令 so easy。# ...
    99+
    2023-06-20
  • 利用JavaScript怎么实现一个拖拽鼠标调整div大小的功能
    本篇文章为大家展示了利用JavaScript怎么实现一个拖拽鼠标调整div大小的功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。Java的特点有哪些Java的特点有哪些1.Java语言作为静态面向...
    99+
    2023-06-07
  • C#wpfCanvas中实现控件拖动调整大小的示例
    目录前言一、功能说明二、如何实现?1.继承Adorner2.使用Thumb3.实现拖动逻辑三、完整代码四、使用示例总结前言 我们做图片编辑工具、视频编辑工具、或者画板有时需要实现控件...
    99+
    2022-11-13
    C# wpf Canvas拖动 C# wpf Canvas
  • 怎么在Html5中实现一个react拖拽排序组件
    今天就跟大家聊聊有关怎么在Html5中实现一个react拖拽排序组件,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。第一步是先了解H5拖放的相关属性,MDN上有详细的说明,链接为htt...
    99+
    2023-06-09
  • C#wpfGrid中实现控件拖动调整大小的示例代码
    目录前言一、功能说明二、如何实现?1.继承Adorner2.使用Thumb3.实现拖动逻辑三、完整代码四、使用示例总结前言 在《C# wpf Canvas中实现控件动态调整大小》中我...
    99+
    2022-11-13
    C# wpf Grid拖动调整大小 C# wpf Grid拖动
  • vue实现可拖拽div大小的方法
    下面看下vue中可拖拽div大小的方法。 可封装为全局方法在项目中所需要地方直接调用(mixins) 方法: 参数: 1.allBox:最外层第div class;2.leftBox...
    99+
    2024-04-02
  • Vue实现可拖拽组件的方法
    本文为大家分享了Vue实现可拖拽、拖拽组件,供大家参考,具体内容如下 描述: 组件仅封装拖拽功能,内容通过#header、#default、#footer插槽 自定义 效果:&nbs...
    99+
    2024-04-02
  • react如何改变组件大小
    这篇文章主要讲解了“react如何改变组件大小”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“react如何改变组件大小”吧!react改变组件大小的方法:1、使用“React.cloneEl...
    99+
    2023-07-04
  • react怎么改变组件大小
    本教程操作环境:Windows10系统、react18版、Dell G3电脑。react怎么改变组件大小?手写一个React拖拽调整大小的组件一、实现流程1.使用React.cloneElement加强包裹组件,在包裹的组件设置绝对定位,并...
    99+
    2023-05-14
    组件 React
  • vue实现拖动调整左右两侧容器大小
    本文实例为大家分享了vue实现拖动调整左右两侧容器大小的具体代码,供大家参考,具体内容如下 页面布局 <template>     <a-layout class...
    99+
    2024-04-02
  • 基于Vue3的全屏拖拽上传组件
    本文主要介绍了基于Vue3的全屏拖拽上传组件,分享给大家,具体如下: 知识点 浏览器拖拽 api fetch 请求 vue3 说来话长,长话短说,关于 ...
    99+
    2024-04-02
  • Vue中拖拽组件开发的示例分析
    这篇文章主要为大家展示了“Vue中拖拽组件开发的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Vue中拖拽组件开发的示例分析”这篇文章吧。为什么选择Vu...
    99+
    2024-04-02
  • linux文件系统的大小怎么调整
    今天小编给大家分享一下linux文件系统的大小怎么调整的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了...
    99+
    2024-04-02
  • Python怎么调整图片的文件大小
    本篇内容主要讲解“Python怎么调整图片的文件大小”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Python怎么调整图片的文件大小”吧!问题描述Python调整图片文件的占用空间大小,而不是分...
    99+
    2023-07-05
  • vue3实现手机上可拖拽元素的组件
    前言: 用vue3实现一个可在手机上拖拽元素的组件,可拖拽至任意位置,并且可以防止拖拽元素移出屏幕边缘。 <script setup> import { ref } fr...
    99+
    2024-04-02
  • Vue 可拖拽组件Vue Smooth DnD的使用详解
    目录简介和 Demo 展示API: Container属性生命周期回调事件API: Draggable实战简介和 Demo 展示 最近需要有个拖拽列表的需求,发现一个简单好用的 Vu...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作