我希望在我的放弃操作失败时捕获事件,因为用户没有放在正确的位置,或者可能是他自己已经通过点击“Esc”键取消了它。
答案 0 :(得分:0)
看看这个脚本。花了我一些时间来写和完成它。 => 的 DEMO 强>
如果这是你想要的,请告诉我。
注意:=> 此代码与旧版(没有HTML5
支持的浏览器)浏览器(例如IE8
等)不兼容
首先,我们通过将draggable
属性设置为true
来定义我们的可拖动元素
<div id="draggable" draggable="true"> Drag this div </div>
然后我们定义dropzone (target:where the element should be dropped on)
<div class="dropzone" id="dropzone">Dropzone</div>
我们需要在释放元素时获取鼠标的坐标,因此我们知道鼠标是否已在dropzone
上释放(我已放置alert
显示dragend
上是否发生dropzone
事件的通知。如果dropzone
上的事件发出 dropped on the dropzone
,则会提醒 dropped not on the dropzone
)
function cords(event)
我们还需要获取dropzone
元素的坐标,这样我们就能找到drop
事件的位置(成功与否(*手动取消){{1} })或掉在错误的地方))发生了。
Esc
var elTop = dragZone.getBoundingClientRect().top + getDocTopLeftPos().fT;
var elLeft = dragZone.getBoundingClientRect().left;
var elBottom = elTop + parseInt(getElProps(dragZone,'height'));
我们将通过var elRight = elLeft + parseInt(getElProps(dragZone,'width'));
活动了解drag
操作是否成功
<强> HTML 强>
(on)dragend
<强> CSS 强>
<div id="draggable" draggable="true">
Drag this div
</div>
<div class="dropzone" id="dropzone">Dropzone</div>
<强>的Javascript 强>
#draggable {
width: 200px;
height: 50px;
text-align: center;
background: red;
margin-bottom:50px;
}
.dropzone {
width: 200px;
height: 200px;
background: brown;
margin-bottom: 10px;
padding: 10px;
text-align:center
}