当前位置:首页 > React

react实现拖拽调整宽度

2026-01-27 16:37:58React

实现拖拽调整宽度的基本思路

在React中实现拖拽调整宽度功能,可以通过监听鼠标事件来控制元素的宽度变化。核心是利用onMouseDownonMouseMoveonMouseUp事件来跟踪拖动操作。

使用useState和useEffect管理状态

定义一个状态变量来存储当前宽度,并通过事件处理函数更新它。useEffect用于添加和清理全局事件监听器。

import React, { useState, useEffect } from 'react';

const ResizableComponent = () => {
  const [width, setWidth] = useState(200);
  const [isDragging, setIsDragging] = useState(false);

  const handleMouseDown = (e) => {
    setIsDragging(true);
    e.preventDefault(); // 防止文本选中等副作用
  };

  useEffect(() => {
    const handleMouseMove = (e) => {
      if (!isDragging) return;
      setWidth(e.clientX); // 根据鼠标位置更新宽度
    };

    const handleMouseUp = () => {
      setIsDragging(false);
    };

    if (isDragging) {
      document.addEventListener('mousemove', handleMouseMove);
      document.addEventListener('mouseup', handleMouseUp);
    }

    return () => {
      document.removeEventListener('mousemove', handleMouseMove);
      document.removeEventListener('mouseup', handleMouseUp);
    };
  }, [isDragging]);

  return (
    <div style={{ display: 'flex' }}>
      <div style={{ width: `${width}px`, border: '1px solid #ccc' }}>
        可调整宽度的区域
      </div>
      <div
        style={{ width: '10px', cursor: 'col-resize', background: '#eee' }}
        onMouseDown={handleMouseDown}
      />
    </div>
  );
};

export default ResizableComponent;

优化拖动体验

添加边界检查和拖动限制,避免宽度超出合理范围。可以在handleMouseMove中增加条件判断:

const handleMouseMove = (e) => {
  if (!isDragging) return;
  const newWidth = e.clientX;
  if (newWidth > 100 && newWidth < 500) { // 限制宽度范围
    setWidth(newWidth);
  }
};

使用自定义Hook封装逻辑

将拖拽逻辑抽象为自定义Hook,提高代码复用性:

const useResizable = (initialWidth, minWidth, maxWidth) => {
  const [width, setWidth] = useState(initialWidth);
  const [isDragging, setIsDragging] = useState(false);

  const handleMouseDown = (e) => {
    setIsDragging(true);
    e.preventDefault();
  };

  useEffect(() => {
    const handleMouseMove = (e) => {
      if (!isDragging) return;
      const newWidth = e.clientX;
      if (newWidth >= minWidth && newWidth <= maxWidth) {
        setWidth(newWidth);
      }
    };

    const handleMouseUp = () => {
      setIsDragging(false);
    };

    if (isDragging) {
      document.addEventListener('mousemove', handleMouseMove);
      document.addEventListener('mouseup', handleMouseUp);
    }

    return () => {
      document.removeEventListener('mousemove', handleMouseMove);
      document.removeEventListener('mouseup', handleMouseUp);
    };
  }, [isDragging, minWidth, maxWidth]);

  return { width, handleMouseDown };
};

// 使用示例
const ResizableComponent = () => {
  const { width, handleMouseDown } = useResizable(200, 100, 500);

  return (
    <div style={{ display: 'flex' }}>
      <div style={{ width: `${width}px`, border: '1px solid #ccc' }}>
        内容区域
      </div>
      <div
        style={{ width: '10px', cursor: 'col-resize', background: '#eee' }}
        onMouseDown={handleMouseDown}
      />
    </div>
  );
};

处理性能问题

频繁的mousemove事件可能导致性能问题。可以通过节流(throttle)或防抖(debounce)来优化:

import { throttle } from 'lodash';

// 在useEffect中
const handleMouseMove = throttle((e) => {
  if (!isDragging) return;
  const newWidth = e.clientX;
  if (newWidth >= minWidth && newWidth <= maxWidth) {
    setWidth(newWidth);
  }
}, 16); // 约60fps

支持垂直拖拽调整高度

类似的方法可以用于调整高度,只需修改事件处理逻辑和样式:

const handleMouseMove = (e) => {
  if (!isDragging) return;
  const newHeight = e.clientY;
  setHeight(newHeight);
};

// 样式调整
<div style={{ height: `${height}px`, position: 'relative' }}>
  <div
    style={{ height: '10px', cursor: 'row-resize', background: '#eee' }}
    onMouseDown={handleMouseDown}
  />
</div>

react实现拖拽调整宽度

标签: 宽度拖拽
分享给朋友:

相关文章

vue实现拖拽计算器

vue实现拖拽计算器

Vue实现拖拽计算器的步骤 创建Vue项目 使用Vue CLI创建一个新项目,确保已安装Vue和必要的依赖项。通过命令行初始化项目并安装依赖。 vue create drag-calculator…

拖拽实现vue

拖拽实现vue

实现拖拽功能的基本思路 在Vue中实现拖拽功能可以通过HTML5的Drag and Drop API或第三方库如vuedraggable来完成。以下是两种常见方法的详细说明。 使用HTML5 Dr…

vue内实现拖拽

vue内实现拖拽

Vue 实现拖拽功能的方法 使用 HTML5 原生拖拽 API HTML5 提供了原生的拖拽 API,可以通过 draggable 属性和相关事件实现拖拽功能。 <template>…

vue实现拖拽div

vue实现拖拽div

实现拖拽功能的基本思路 在Vue中实现拖拽功能可以通过HTML5的拖拽API或第三方库如vuedraggable来完成。以下是两种常见方法的详细说明: 使用HTML5拖拽API HTML5提供了原生…

jquery拖拽

jquery拖拽

jQuery拖拽实现方法 使用jQuery实现拖拽功能可以通过多种方式完成,以下是常见的实现方法: 使用jQuery UI的Draggable组件 jQuery UI提供了现成的拖拽组件,只需引入相…

vue 实现可拖拽

vue 实现可拖拽

实现拖拽功能的基本思路 在Vue中实现拖拽功能可以通过HTML5的Drag and Drop API或第三方库如vuedraggable来完成。以下是两种常见方法的详细说明。 使用HTML5原生拖…