React 模态框的拉伸和拖动

简介: React 模态框的拉伸和拖动

拖动使用 react-draggable 插件

App.js

import { NodeExpandOutlined } from '@ant-design/icons';
import React, { useRef, useState } from 'react'; // 引入useState和useRef
import Draggable from 'react-draggable';
export default function Problem() {
  const [width, setWidth] = useState(400);
  const [height, setHeight] = useState(450);
  // 使用useRef保存调整大小时的宽度和高度
  const resizeRef = useRef({
    isResizing: false,
    newWidth: null,
    newHeight: null
  })
  const [tuod, setTuod] = useState(true)
  const handleResizeMouseDown = (resizeType, e) => {
    let x = e.pageX
    let y = e.pageY
    console.log(x, y);
    const parent = document.querySelector('.box_'); // 获取父级元素
    const parentRect = parent.getBoundingClientRect(); // 获取父级元素的位置信息
    const bleft = parentRect.left; // 获取父级元素相对于屏幕右边的坐标
    const btop = parentRect.top; // 获取父级元素的宽度
    const resizeFunction = resizeType === 'right'
      ? e => {
        if (x > e.pageX) {
          console.log('小');
          resizeRef.current.newWidth = e.pageX - e.target.getBoundingClientRect().left;
          resizeRef.current.isResizing = true;
        } else {
          console.log('大');
          resizeRef.current.newWidth = e.pageX - e.target.getBoundingClientRect().left - bleft;
          resizeRef.current.isResizing = true;
        }
      }
      : e => {
        if (y > e.pageY) {
          console.log('小');
          resizeRef.current.newHeight = e.pageY - e.target.getBoundingClientRect().top + 50;
          resizeRef.current.isResizing = true;
        } else {
          console.log('大');
          resizeRef.current.newHeight = e.pageY - e.target.getBoundingClientRect().top - btop + 50;
          resizeRef.current.isResizing = true;
        }
      };
    setTuod(false)
    const updateFunction = resizeType === 'right' ? setWidth : setHeight;
    const handleMouseUp = () => {
      setTuod(true)
      document.removeEventListener('mousemove', resizeFunction); //删除监听事件
      document.removeEventListener('mouseup', handleMouseUp);
      if (resizeRef.current.isResizing) { //当它修改成功后
        updateFunction(resizeType === 'right' ? resizeRef.current.newWidth : resizeRef.current.newHeight);
        resizeRef.current.isResizing = false;
      }
    };
    document.addEventListener('mousemove', resizeFunction);
    document.addEventListener('mouseup', handleMouseUp);
  };
  const [tdwidth, setTeidth] = useState(25);
  const [istou, setIstou] = useState(false)
  const tuoing = () => {
    setIstou(true)
  }
  const tuoed = () => {
    setIstou(false)
  }
  const [isshow, setIshow] = useState(true)
  return (
    <div className='problem' >
      <div style={{
        opacity: !isshow ? 0.5 : 1, pointerEvents:
          !isshow ? "none" : ""
      }}>
        <div>
          <h2 onClick={() => setIshow(!isshow)}>点击</h2>
        </div>
        <div>
          <p>www</p>
          <p>www</p>
          <p>www</p>
          <p>www</p>
        </div>
      </div>
      <Draggable axis="both" handle=".handle" defaultPosition={{ x: 0, y: 0 }} bounds=".problem" onDrag={tuoing} onStop={tuoed}>
        <div
          className="box_" style={{ display: isshow ? "none" : "", opacity: istou ? 0.8 : 1, backgroundColor: "white", position: "fixed", left: "200px", top: "100px", width: `${width}px`, height: `${height}px`, boxShadow: '0 0 5px 0px grey', userSelect: 'none' }}>
          <div className="handle">
            <span>用户模块管理</span>
            <span style={{ display: 'inline-block', float: "right", marginRight: "5px", fontSize: "20px" }} onClick={() => setIshow(true)}>x</span>
          </div>
          <div
            className='resize' style={{ position: 'absolute', top: 0, right: -1, width: '2px', height: '100%', cursor: 'e-resize' }}
            onMouseDown={(e) => handleResizeMouseDown('right', e)} />
          <div
            className='resizeBottom' style={{ position: 'absolute', bottom: 0, right: -1, width: '100%', height: '2px', cursor: 'n-resize' }}
            onMouseDown={(e) => handleResizeMouseDown('bottom', e)} />
          <div className='neirong'>
            <div style={{
              opacity: !tuod ? 0.3 : 1,
              display: istou ? "none" : ""
            }}>
              <h2>内容</h2>
            </div>
          </div>
        </div>
      </Draggable>
    </div>
  );
}

App.css

.content_home{
    height: 617px;
    width: 375px;
}
.footer{
    position: fixed;
    bottom: 0px;
    height: 50px;
    width: 375px;
}
.top{
    position: fixed;
    top: 0px;
    height: 50px;
    width: 375px;
}
.content {
    position: fixed;
    top: 50px;
    height: 567px;
    width: 375px;
    overflow-y: auto;
}
.content::-webkit-scrollbar {
    display: none;
}
.problem{
    position: fixed;
    top: 50px;
    height: 567px;
    width: 100vw;
    overflow-y: auto;
}
.handle{
    background-color: #6c95db;
    color: white;
    height: 35px;
    line-height: 35px;
    text-indent: 1em;
    cursor: move;
}
相关文章
|
前端开发 UED
React 模态框 Modal 组件详解
【10月更文挑战第27天】本文介绍了如何在 React 中实现一个功能完善的模态框组件。从基础概念入手,逐步讲解了简单的模态框实现、CSS 样式、传递子组件、键盘事件处理等高级功能。同时,还探讨了常见问题及易错点,如背景点击关闭、键盘事件冲突和动画效果。通过本文,读者可以全面了解 React 模态框组件的实现细节。
701 1
|
JavaScript 前端开发 容器
详细介绍React模态框组件react-modal
该组件实现了模态框的一些效果。这是react-modal官网的配置参数。
6213 0
|
1月前
|
缓存 前端开发 JavaScript
React Hooks深度解析与最佳实践:提升函数组件能力的终极指南
🌟蒋星熠Jaxonic,前端探索者。专注React Hooks深度实践,从原理到实战,分享状态管理、性能优化与自定义Hook精髓。助力开发者掌握函数组件的无限可能,共赴技术星辰大海!
React Hooks深度解析与最佳实践:提升函数组件能力的终极指南
|
6月前
|
缓存 前端开发 数据安全/隐私保护
如何使用组合组件和高阶组件实现复杂的 React 应用程序?
如何使用组合组件和高阶组件实现复杂的 React 应用程序?
268 68
|
6月前
|
缓存 前端开发 Java
在 React 中,组合组件和高阶组件在性能方面有何区别?
在 React 中,组合组件和高阶组件在性能方面有何区别?
244 67
|
6月前
|
前端开发 JavaScript 安全
除了高阶组件和render props,还有哪些在 React 中实现代码复用的方法?
除了高阶组件和render props,还有哪些在 React 中实现代码复用的方法?
268 62
|
9月前
|
移动开发 前端开发 API
React 音频播放器组件 Audio Player
本文介绍如何使用React创建音频播放器组件,涵盖核心功能如播放/暂停、进度条、音量控制和时间显示。通过HTML5 `&lt;audio&gt;` 元素和React的声明式状态管理,实现交互式音频播放。常见问题包括控件不响应、进度条无法更新和音量控制失灵,并提供解决方案。此外,还讨论了浏览器兼容性、异步错误处理和性能优化等易错点及避免方法。
689 123
|
8月前
|
前端开发 JavaScript
除了使用Route组件,React Router还有其他方式处理404错误页面吗
除了使用Route组件,React Router还有其他方式处理404错误页面吗
227 58
|
8月前
|
前端开发
React 中高阶组件的原理是什么?
React 中高阶组件的原理是什么?
225 57
|
8月前
|
前端开发 开发者
除了函数组件和类组件,React 还有其他创建组件的方式吗?
除了函数组件和类组件,React 还有其他创建组件的方式吗?
178 57