Understanding HTML 5 event drag

社会主义新天地 提交于 2019-12-10 15:28:19

问题


I do not understand why the returning value for mouse coordinate clientX in event drag is always 0 or a negative value just before releasing the mouse.

I have prepare an example, when user dragstart, mouse position is correct, same for end dragend... but if you look at the console for drag you will see before dragend a negative value.

Is a normal behavior? Why? I need to avoid this 0 value. any solutions?

http://jsfiddle.net/gg8gLpg0/

  <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <style>
    #test {
        position: absolute;
        width: 100px;
        height: 100px;    
        background-color:red;
    }
        </style>

    </head>
    <body>
        <div id="test" draggable="true">test</div>
        <script>
        var elm = document.getElementById('test');

        elm.addEventListener('drag', function (event) {
            //console.log(event.clientX);
            //console.log(event.clientY);
        }.bind(this));

        elm.addEventListener('dragstart', function (event) {
            console.log('start');
            console.log(event.clientX);
            console.log(event.clientY);
        }.bind(this));

        elm.addEventListener('drag', function (event) {
            console.log('during drag');// PROBLEM HERE
            console.log(event.clientX);
            console.log(event.clientY);
        }.bind(this));

        elm.addEventListener('dragend', function (event) {
            console.log('end');
            console.log(event.clientX);
            console.log(event.clientY);
        }.bind(this));


        </script>

    </body>
    </html>

回答1:


In my tests on Firefox the drag element ONLY fires a 0. AND, the drag event stopped the "mousemove" event from reaching the document level to capture it that way.

jsfiddle.net/qgedt70a/

Mozilla's official docs say the clientX is local for dragged objects and screenX is global coordinates, however both return 0 from the drag event in variations of my tests at the above jsfiddle.

Could be a bug. OR, I'm curious if they have disabled it for some strange security scenario?



来源:https://stackoverflow.com/questions/27156129/understanding-html-5-event-drag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!