react中实现可拖动div

更新日期: 2020-02-24阅读: 3.1k标签: react

把拖动div功能用react封装成class,在页面直接引入该class即可使用。

title为可拖动区域。panel为要实现拖动的容器。

优化了拖动框超出页面范围的情况,也优化了拖动太快时鼠标超出可拖动区域的情况,优化了拖动会卡顿的情况。

 

页面中添加引入方法:

<Draggable panelId="要拖动容器的id" titleId="容器内标题的id" contentId="容器内除标题外的其他部分id" setPanelPosition={this.setPanelPosition.bind(this)}/>

页面中添加拖拽回调函数

  //推拽回调函数
  setPanelPosition(left,top){
    this.setState({pageX: left, pageY: top})
  }

要拖动的div如下:

<div id="要拖动的id" style={{left:this.state.pageX,top:this.state.pageY}}></div>

 

封装的class代码

import React from 'react';
class Draggable extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
    };
  }
  //拖拽
  initDrag(){
    let {panelId,titleId,contentId} = this.props;
    this.paneldom = document.getElementById(panelId);
    this.titleDom = document.getElementById(titleId);
    this.contentDom = document.getElementById(contentId);
    this.backgroundDom = document.body;
    this.bindEvent();
  }

  //region event
  componentDidMount() {
    this.initDrag();
  }
  bindEvent(){
    this.titleDom.onmousedown = this.onMouseDown.bind(this);
    this.titleDom.onmouseup = this.onMouseUp.bind(this);
    this.titleDom.onmousemove = this.onMouseMove.bind(this);

    this.contentDom.onmouseup = this.onContentMouseUp.bind(this);
    this.contentDom.onmousemove = this.onContentMouseMove.bind(this);

    this.backgroundDom.onmouseup = this.onBackgroundMouseUp.bind(this);
    this.backgroundDom.onmousemove = this.onBackgroundMouseMove.bind(this);
    let step = ()=>{
      this.activeAnimation = true;
      window.requestAnimationFrame(step);
    };
    window.requestAnimationFrame(step);
  }

  /**
   * 鼠标按下,设置modal状态为可移动,并注册鼠标移动事件
   * 计算鼠标按下时,指针所在位置与modal位置以及两者的差值
   **/
  onMouseDown (e) {
    const position = this.getPosition(e)
    this.setState({moving: true, diffX: position.diffX, diffY: position.diffY})
  }

  // 松开鼠标,设置modal状态为不可移动
  onMouseUp (e) {
    const { moving } = this.state
    moving && this.setState({moving: false});
  }

  // 鼠标移动重新设置modal的位置
  onMouseMove (e) {
    const {moving, diffX, diffY} = this.state
    if (moving) {
      if(this.activeAnimation){
        // 获取鼠标位置数据
        const position = this.getPosition(e)
        // 计算modal应该随鼠标移动到的坐标
        const x = position.mouseX - diffX
        const y = position.mouseY - diffY
        // 窗口大小,结构限制,需要做调整,减去侧边栏宽度
        const { clientWidth, clientHeight } = document.documentElement
        const modal = this.panelDom
        if (modal) {
          // 计算modal坐标的最大值
          const maxHeight = clientHeight - modal.offsetHeight
          const maxWidth = clientWidth - modal.offsetWidth
          // 判断得出modal的最终位置,不得超出浏览器可见窗口
          const left = x > 0 ? (x < maxWidth ? x : maxWidth) : 0
          const top = y > 0 ? (y < maxHeight ? y : maxHeight) : 0
          if(this.props.setPanelPosition){
            this.props.setPanelPosition(left,top);
          }
        }
        this.activeAnimation = false;
      }
    }
  }
  onContentMouseMove(e){
    let obj = {};
    obj.target = this.titleDom;
    obj.pageX = e.pageX;
    obj.screenY = e.screenY;
    this.onMouseMove(obj);
  }
  onContentMouseUp(){
    this.onMouseUp();
  }
  onBackgroundMouseMove(e){
    let obj = {};
    obj.target = this.titleDom;
    obj.pageX = e.pageX;
    obj.screenY = e.screenY;
    this.onMouseMove(obj);
  }
  onBackgroundMouseUp(){
    this.onMouseUp();
  }
  //endregion

  //region request
  // 获取鼠标点击title时的坐标、title的坐标以及两者的位移
  getPosition (e) {
    // 标题DOM元素titleDom
    const titleDom = e.target
    // titleDom的坐标(视窗)
    const X = titleDom.getBoundingClientRect().left
    // 由于Y轴出现滚动条,需要与鼠标保持一致,存储页面相对位置
    const Y = this.panelDom.offsetTop

    // 鼠标点击的坐标(页面)
    let mouseX = e.pageX
    let mouseY = e.screenY
    // 鼠标点击位置与modal的位移
    const diffX = mouseX - X
    const diffY = mouseY - Y
    return {X, Y, mouseX, mouseY, diffX, diffY}
  }
  //endregion

  //region render
  //endregion

  //region clear
  //endregion

  render() {
    return (
      <>
      </>
    );
  }
}
export default Draggable;

链接: https://www.fly63.com/article/detial/8754

如何优雅的设计 React 组件

如今的 Web 前端已被 React、Vue 和 Angular 三分天下,尽管现在的 jQuery 已不再那么流行,但 jQuery 的设计思想还是非常值得致敬和学习的,特别是 jQuery 的插件化。

React深度编程:受控组件与非受控组件

受控组件与非受控组件在官网与国内网上的资料都不多,有些人觉得它可有可不有,也不在意。这恰恰显示React的威力,满足不同规模大小的工程需求。

React框架学习_关于React两种构建应用方式选择

一般在传统模式下,我们构建前端项目很简单。就是下载各种js文件,如JQuery、Echart等,直接放置在html静态文件。Webpack则是JavaScript中比较知名的打包工具。这两个构建工具构成了React应用快速搭建的基础。

Gatsby.js_一款基于React.js静态站点生成工具

Gatsby能快速的使用 React 生态系统来生成静态网站,可以结合React Component、Markdown 和服务端渲染来完成静态网站生成让他更强大。

React创建组件的三种方式及其区别

React推出后,出于不同的原因先后出现三种定义react组件的方式,殊途同归;具体的三种方式:函数式定义的无状态组件、es5原生方式React.createClass定义的组件、es6形式的extends React.Component定义的组件

react生命周期详解_深入理解React生命周期

React主要思想是通过构建可复用组件来构建用户界面,每个组件都有自己的生命周期,它规定了组件的状态和方法需要在哪个阶段改变和执行。所谓组件就是有限状态机,,表示有限个状态以及在这些状态之间的转移和动作行为的模型。

React + Webpack 构建打包优化

React 相关的优化:使用 babel-react-optimize 对 React 代码进行优化,检查没有使用的库,去除 import 引用,按需打包所用的类库,比如 lodash 、echarts 等.Webpack 构建打包存在的问题两个方面:构建速度慢,打包后的文件体积过大

react router中页面传值的三种方法

这篇文章主要介绍React Router定义路由之后如何传值,有关React和React Router 。react router中页面传值的三种方法:props.params、query、state

react 高阶组件的 理解和应用

react 高阶组件简单的理解是:一个包装了另一个基础组件的组件。高阶组件的两种形式:属性代理(Props Proxy)、反向继承 (Inheritance Inversion)

react中的refs属性的使用方法

React 支持一种非常特殊的属性 Ref ,你可以用来绑定到 render() 输出的任何组件上。这个特殊的属性允许你引用 render() 返回的相应的支撑实例( backing instance )。这样就可以确保在任何时间总是拿到正确的实例

点击更多...

内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!