把拖动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;

react中实现可拖动div的更多相关文章

  1. 理解React中es6方法创建组件的this

    首发于:https://mingjiezhang.github.io/(转载请说明此出处). 在JavaScript中,this对象是运行时基于函数的执行环境(也就是上下文)绑定的. 从react中的 ...

  2. React中props.children和React.Children的区别

    在React中,当涉及组件嵌套,在父组件中使用props.children把所有子组件显示出来.如下: function ParentComponent(props){ return ( <di ...

  3. react中createFactory, createClass, createElement分别在什么场景下使用,为什么要这么定义?

    作者:元彦链接:https://www.zhihu.com/question/27602269/answer/40168594来源:知乎著作权归作者所有,转载请联系作者获得授权. 三者用途稍有不同,按 ...

  4. React中父组件与子组件之间的数据传递和标准化的思考

    React中父组件与子组件之间的数据传递的的实现大家都可以轻易做到,但对比很多人的实现方法,总是会有或多或少的差异.在一个团队中,这种实现的差异体现了每个人各自的理解的不同,但是反过来思考,一个团队用 ...

  5. React中使用CSSTransitionGroup插件实现轮播图

    动画效果,是一个页面上必不可少的功能,学习一个新的东西,当然就要学习,如何用新的东西,用它的方法去实现以前的东西啦.今天呢,我就在这里介绍一个试用react-addons-css-transition ...

  6. React中的路由系统

    React中的路由系统 提起路由,首先想到的就是 ASPNET MVC 里面的路由系统--通过事先定义一组路由规则,程序运行时就能自动根据我们输入的URL来返回相对应的页面.前端中的路由与之类似,前端 ...

  7. 关于React中状态保存的研究

    在使用react搭配react-router做应用的时候,你可能遇到这样的问题,当我从第一个页面过渡到第二个页面,然后返回之后,发现之前的页面的状态全部不见了,即回到了初始的状态. 这点在页面存在多个 ...

  8. React 深入系列1:React 中的元素、组件、实例和节点

    文:徐超,<React进阶之路>作者 授权发布,转载请注明作者及出处 React 深入系列,深入讲解了React中的重点概念.特性和模式等,旨在帮助大家加深对React的理解,以及在项目中 ...

  9. React 中阻止事件冒泡的问题

    在正式开始前,先来看看 JS 中事件的触发与事件处理器的执行. JS 中事件的监听与处理 事件捕获与冒泡 DOM 事件会先后经历 捕获 与 冒泡 两个阶段.捕获即事件沿着 DOM 树由上往下传递,到达 ...

随机推荐

  1. s7-200高速脉冲输出与高速计数器讲解

    s7-200高速脉冲输出与高速计数器讲解

  2. OS_页面置换算法:C++实现

    一.实验目的: 通过模拟实现请求页式存储管理的几种基本页面置换算法,了解虚拟存储技术的特点,掌握虚拟存储请求页式存储管理中几种页面置换算法的基本思想和实现过程,并比较它们的效率. 二.实验内容: 本实 ...

  3. 深入理解JVM(③)ZGC收集器

    前言 ZGC是一款在JDK11中新加入的具有实验性质的低延迟垃圾收集器,目前仅支持Linux/x86-64.ZGC收集器是一款基于Region内存布局的,(暂时)不设分代的,使用了读屏障.染色指针和内 ...

  4. docker部署dubbo怎么实现外部主机访问服务?

    dubbo在分布式项目中太常见了,docker也是现在热门的项目,然而docker的网络配置也是非常麻烦的一件事情,这里给大家介绍一下dubbo实现跨服务器访问服务配置 docker-compose. ...

  5. skywalking面板功能介绍2

    场景: spring-user调用spring-order 1.spring-user部署了两个应用实例 2.spring-order部署了一个实例 应用详情信息在表 从上面表中可以看出spring- ...

  6. Spring Boot]SpringBoot四大神器之Actuator

    论文转载自博客: https://blog.csdn.net/Dreamhai/article/details/81077903 https://bigjar.github.io/2018/08/19 ...

  7. Python方法函数记录

    目录 python 控制台输出的内容保存到txt 文件 eval函数使用 python 控制台输出的内容保存到txt 文件 import sys class Logger(object): def _ ...

  8. 【实践】如何利用tensorflow的object_detection api开源框架训练基于自己数据集的模型(Windows10系统)

    如何利用tensorflow的object_detection api开源框架训练基于自己数据集的模型(Windows10系统) 一.环境配置 1. Python3.7.x(注:我用的是3.7.3.安 ...

  9. Nginx 从入门到放弃(二)

    学习完了nginx的基本知识后,我们来了解下nginx的虚拟主机. 说到虚拟主机,那就得说一说虚拟主机的三种方式了 基于端口的虚拟主机 基于域名的虚拟主机 基于ip的虚拟主机 基于端口实现虚拟主机 只 ...

  10. 06 . Kubernetes之Pod控制器详细介绍及应用

    Pod API属性详解 Pod是k8s集群中的最小编排单位.将这个设计落实到API对象上,容器就成了Pod属性里一个普通的字段.那么到底哪些属性属于Pod对象,哪些属性属于容器的呢?先看下面的一段描述 ...