JS拖拽功能

第一种默认可以拖拽的,比如图片

<div draggable="true"></div>

拖拽时:

开始 :dragstart        进行中:drag       结束:dragend

进入区域

进入时: dragenter      进入后:dragover      离开:dragleave      放置:drop

第二种鼠标事件:

鼠标按下:mousedown

鼠标移动:mousemove

鼠标松开:mouseup

再根据定位计算元素的位置完成拖拽

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现js拖拽功能,可以使用html5的拖放API或者jQuery UI等库来实现。下面是一个使用jQuery UI实现拖拽功能的例子: ```html <!DOCTYPE html> <html> <head> <title>拖拽功能实现</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.12.4.js"></script> <script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/jsPlumb/2.4.7/js/jsplumb.min.js"></script> <style type="text/css"> #container{ position: relative; width: 500px; height: 500px; border: 1px solid #000; } .block{ position: absolute; width: 50px; height: 50px; background-color: #f00; cursor: move; text-align: center; line-height: 50px; font-size: 20px; color: #fff; } </style> <script type="text/javascript"> $(function(){ //初始化jsPlumb实例对象 jsPlumb.ready(function(){ var instance = jsPlumb.getInstance({ Endpoint: ["Dot", {radius:2}], Connector:"StateMachine", HoverPaintStyle: {stroke:"#1e8151", strokeWidth:2 }, ConnectionOverlays: [ [ "Arrow", { location:1, id:"arrow", length:14, foldback:0.8 } ] ] }); }); //初始化拖拽功能 $(".block").draggable({ containment: "#container", stop: function(event, ui) { //更新jsplumb连接线 jsPlumb.repaint(ui.helper); } }); //初始化连接功能 jsPlumb.connect({ source: "block1", target: "block2" }); }); </script> </head> <body> <div id="container"> <div id="block1" class="block">1</div> <div id="block2" class="block" style="top: 100px; left: 100px;">2</div> </div> </body> </html> ``` 在上面的代码中,首先通过jQuery UI的draggable方法初始化了拖拽功能,然后通过jsPlumb的connect方法初始化了连接功能。同时在jsPlumb.ready方法中初始化了jsPlumb实例对象,以便后续使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值