js+css3实现drag拖动排序

一、拖拽html

1、使用拖拽事件的时候,报错‘Cannot set property ‘ondragstart’ of null’:

原因:JS的引进放在了head标签里面,浏览器遇到<script>标签的时候会立即执行脚本,这个时候DOM树还没有构建完成,访问不到节点

解决:

  1. 把JS的引进放在body标签底部
  2. 使用’defer’异步加载js文件,不会影响DOM渲染,js脚本会在DOM渲染完毕后,DOMContentLoaded事件调用前执行

defer和async:
async也是异步加载js文件,区别在于async还是异步执行js文件,先加载完的脚本先执行。(适用于不关心DOM元素的脚本)

二、拖拽事件

拖动目标:
ondragstart:开始拖动
ondrag:正在拖动
ondragend:完成拖动

释放目标:
ondragenter:拖动对象进入容器范围
ondragover:拖动对象在另一容器范围内拖动
ondragleave:拖动对象离开容器范围
ondrop:拖动过程中释放鼠标

三、插入元素

DOM的insertBefore()方法:把一个新元素插到一个现有元素的前面

parentElement.insertBefore(newElement,targetElement)

实现在一个现有元素后面插入新元素:
nextSibling —> 下一个兄弟元素,若没有则为null
把新元素插到目标元素的下一个兄弟元素前面,如果下一个兄弟元素为null将被插到子节点的末尾

parentElement.insertBefore(newElement,targetElement.nextSibling)

四、css3动画

1、获取元素位置
getBoundingClientRect()用于获取页面中某元素的左右上下分别相对浏览器视窗的位置

2、node.nodeType
判断节点类型。如果节点是元素节点返回1,如果是属性节点返回2

3、transform:translate3d(0,0,0)
触发加速,使GPU适应CSS过渡,更加流畅

4、target.offsetWidth
通过获取元素的offsetWidth或offsetHeight触发重绘。

1、判断元素是否在动画中,防止重复动画
在拖动的时候,dragover是会不停触发的,导致过多的加载动画,所以需要判断元素是否已经添加动画,可以设置一个定时器,在事件到了之后将transition和transform清空

if (target.animated) {
	return
}

target.animated = setTimeout(function() {
	_css(target, 'transition', '');
	_css(target, 'transform', '');
	target.animated = false;
}, ms);

五、demo展示与代码

链接

附:原作者地址https://www.jianshu.com/p/a923add40767

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个使用jsplumb+dragable+vue3实现可拖拽的流程编辑器的完整例子: ```vue <template> <div class="container" ref="container"> <div class="node" v-for="node in nodes" :key="node.id" :style="{ left: node.x + 'px', top: node.y + 'px' }" :data-id="node.id" v-draggable> <div class="title">{{ node.title }}</div> <div class="content">{{ node.content }}</div> <div class="endpoint" :class="'endpoint-' + index" v-for="(endpoint, index) in node.endpoints" :key="index" :data-id="node.id + '-' + index"></div> </div> </div> </template> <script> import { onMounted, ref } from 'vue'; import jsPlumb from 'jsplumb'; import 'jsplumb/dist/js/jsplumb.min.css'; import 'dragula/dist/dragula.min.css'; import dragula from 'dragula'; export default { name: 'DraggableFlowchart', setup() { const container = ref(null); const nodes = ref([ { id: 'node1', title: 'Node 1', content: 'This is Node 1', x: 100, y: 100, endpoints: [ ['Right', { uuid: 'node1-right' }], ['Bottom', { uuid: 'node1-bottom' }], ], }, { id: 'node2', title: 'Node 2', content: 'This is Node 2', x: 300, y: 100, endpoints: [ ['Left', { uuid: 'node2-left' }], ['Bottom', { uuid: 'node2-bottom' }], ], }, ]); onMounted(() => { const instance = jsPlumb.getInstance({ // 设置连接线的样式 PaintStyle: { strokeWidth: 2, stroke: '#61B7CF', }, // 鼠标悬停在连接线上的样式 HoverPaintStyle: { strokeWidth: 3, stroke: '#216477', }, // 端点的样式设置 EndpointStyles: [ { fill: '#61B7CF' }, { fill: '#216477' }, ], // 鼠标悬停在端点上的样式 EndpointHoverStyles: [ { fill: '#216477' }, { fill: '#61B7CF' }, ], // 连接线的锚点,可以设置为不同的位置,例如左侧、右侧、中心等位置 Anchors: ['Right', 'Left', 'Top', 'Bottom'], // 设置连接线的类型,可以设置为Bezier、Straight等类型 Connector: ['Bezier', { curviness: 150 }], Container: container.value, }); // 添加节点和端点 nodes.value.forEach((node) => { instance.draggable(node.id); node.endpoints.forEach((endpoint, index) => { instance.addEndpoint(node.id, { anchor: endpoint[0], uuid: endpoint[1].uuid, }); }); }); // 连接两个端点 instance.connect({ uuids: ['node1-right', 'node2-left'], }); // 监听连接事件 instance.bind('connection', (info) => { console.log(`Connection established: ${info.sourceId} -> ${info.targetId}`); }); // 监听断开连接事件 instance.bind('connectionDetached', (info) => { console.log(`Connection detached: ${info.sourceId} -> ${info.targetId}`); }); // 监听删除连接事件 instance.bind('connectionDeleted', (info) => { console.log(`Connection deleted: ${info.sourceId} -> ${info.targetId}`); }); // 监听节点位置改变事件 instance.bind('drag', (info) => { const node = nodes.value.find((n) => n.id === info.el.getAttribute('data-id')); node.x = info.pos[0]; node.y = info.pos[1]; }); // 使用dragula库实现节点拖拽排序 const drake = dragula([container.value], { moves(el, container, handle) { return handle.classList.contains('title'); }, }); // 监听节点排序事件 drake.on('drop', () => { console.log('Node order changed'); }); }); return { container, nodes, }; }, }; </script> <style> .container { width: 100%; height: 100%; position: absolute; } .node { position: absolute; background-color: #fff; border: 1px solid #ddd; border-radius: 5px; padding: 10px; cursor: move; } .title { font-size: 16px; font-weight: bold; margin-bottom: 10px; } .content { font-size: 14px; margin-bottom: 10px; } .endpoint { width: 10px; height: 10px; background-color: #61B7CF; border-radius: 50%; position: absolute; } .endpoint-0 { right: -5px; top: 50%; transform: translateY(-50%); } .endpoint-1 { left: -5px; top: 50%; transform: translateY(-50%); } .endpoint-2 { right: 50%; top: -5px; transform: translateX(50%); } .endpoint-3 { right: 50%; bottom: -5px; transform: translateX(50%); } </style> ``` 在这个例子中,我们首先引入了`jsPlumb`和`dragula`库,并使用`onMounted`钩子来初始化一个`jsPlumb`实例和一个`dragula`实例。在实例化过程中,我们设置了连接线的样式、锚点、连接线类型等参数,然后添加了两个节点和它们的端点,并连接了这两个节点的端点。我们还为节点添加了拖拽和排序的功能,并且监听了连接、断开连接、删除连接和位置改变事件。 请注意,这只是一个简单的例子,仅用于演示如何使用`jsPlumb`和`dragula`实现可拖拽的流程编辑器。实际应用中,您可能需要更复杂的逻辑和交互来处理不同的情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值