作者:zgshenxz_474 | 来源:互联网 | 2023-05-21 16:26
ImplayingwiththeHTML5draganddropandtrackingthemousepositionwhiledragging.我正在玩HTML5拖放
I'm playing with the HTML5 drag and drop and tracking the mouse position while dragging.
我正在玩HTML5拖放,拖拽鼠标的位置。
OffsetX and OffsetY works awesome until you release the mouse, the offsets jump to a negative number on the last drag event dispatched
OffsetX和OffsetY非常棒,直到您释放鼠标,偏移量跳转到最后一个拖动事件的负数。
here's the html:
html:
here's the css:
css:
#dragger{
-webkit-user-drag: element;
width: 100px;
height: 100px;
background: hsla(200, 100%, 50%, 0.4);
}
and the js
和js
$('#dragger').bind('drag', function (e) {
$('#console').html(e.originalEvent.offsetX);
})
You can also test out at http://jsfiddle.net/Eu2mz/5/
您还可以在http://jsfiddle.net/Eu2mz/5/上测试。
Also I'm just trying to get it to work in webkit for now.
我现在只是想让它在webkit中工作。
4 个解决方案
0
I had the same problem and came up with a solution that will work 99.99999% of the time. the explanation is below:
我遇到了同样的问题,想出了一个解决方案,它可以达到99.99999%的时间。下面的解释是:
Solution (should work in every case your users will run into)
eng.window.addEventListener(
"drag"
,function(event){
//If both screenX and screenY are 0, likely the user just released.
if(!event.screenX && !event.screenY) return;
//Your code here
}
);
Explanation
I looked through the event returned on releasing the mouse and compared it to the event immediately before, and couldn't find anything that will work in 100% of the cases- there's no easy "buttonPressed" feature or the like hidden inside the object.
我查看了一下发布的事件,并将它与之前的事件进行了比较,并且在100%的情况下都找不到任何可以工作的东西——没有简单的“buttonPressed”功能或者隐藏在对象内部的东西。
I did see though that every single measure of cursor position is changed when you release: clientX
, layerX
, offsetX
, pageX
, screenX
, and regular x
/y
. They all default to the top-left value possible, which may be the top-left corner of the window- or the screen.
我确实看到了,当您发布时,每个指针位置的度量都发生了变化:clientX、layerX、offsetX、pageX、screenX和regular x/y。它们都默认为左上角的值,这可能是窗口的左上角或屏幕的左上角。
But what is the likelihood that your user will go into fullscreen mode drag and drop an element into the very top-left pixel of their screen? (In Chrome for me, screenX
is actually set to the left edge of the window; but screenY
takes Chrome's HUD into account)
但是你的用户进入全屏模式拖拽一个元素到屏幕左上角的可能性有多大呢?(在Chrome中,screenX实际上是设置在窗口的左边缘;但是screenY把Chrome的HUD考虑进去了)
So while the above won't work in that 1 fringe case (that your users will likely never-ever run into), it will work every other time.
因此,虽然上面的内容不会在一个附带的情况下工作(用户可能从未遇到过),但它会在每隔一段时间运行。
Tested in Chrome.
在Chrome进行测试。