开发者

HTML5 Drag and Drop

I've been having t开发者_开发百科rouble coming up with a way of making a drag and drop area on a web page. I have multiple resizable <div>s, and I want to be able to drag these anywhere. Think of it like dragging desktop icons around the desktop and placing them anywhere. It would be nice if I could add buttons to these <div>s to change their z-indexes and have them overlap. Would this require use of <canvas>? I am currently using <section> for the drag area.

Thanks!


If you want to do the drag-n-drop yourself, you may want to have one div enclosing the draggable div, so you can use the top of the larger div as the draggable area.

So, you have

<div id='draggablediv' style='backgroundcolor: blue;'>
<div class='draggable' style='position: relative; top: 5em; left: 0em;'>...
</div></div>

This code is purely for example, won't work, btw.

So, on the draggablediv you would put an onclick event handler, and this would start an onmousemove handler and onmouseup handler. The last one is to drop, but you may also want to have onblur in case the mouse moves outside of the browser.

Then, as the mouse moves, just reposition the div, so these divs would need to be absolute positioned, or relative positioned (absolute would be easier).

It is important to remove the event handlers by setting them to null when the mouse button is released.

If not in a droppable area then make certain to put the div back where it started, so you will want a closure so you can remember the original top/left coordinates of the div.

You will want to get familiar with this functionality:

(function g(someval) {
    var a = someval;
   return h() {
   }
})(origval);

For an example search for getImgInPositionedDivHtml in http://jibbering.com/faq/notes/closures/

In order to change the z-index you may want to have a +/- in the div and when that is clicked on the z-index is changed.

Here is a page that talks about changing the z-index.

http://msdn.microsoft.com/en-us/library/ms533005(v=vs.85).aspx


I don't think you can do that with HTML-Only, however this is some example of how you could do it with javascript:

<html>
    <head>
        <style>
            .draggable {
                position: absolute;
                cursor: default;
                background-color: purple;
                top: 0px;
                left: 0px;
            }
        </style>
    </body>
    <body onmouseup="stopMovement()">
        <div id="draggable" class="draggable" onmousedown="startMovement(event)">
            Drag me around :D
        </div>
        <script>
            var drg = document.getElementById("draggable");
            var xDisplacement = 0;
            var yDisplacement = 0;
            function startMovement(e) {
                xDisplacement = e.pageX - getComputedStyle(drg).left.substring(0, getComputedStyle(drg).left.length - 2);
                yDisplacement = e.pageY - getComputedStyle(drg).top.substring(0, getComputedStyle(drg).top.length - 2);
                document.body.onmousemove = moveDraggable;
            }
            function stopMovement() {
                document.body.onmousemove = null;
            }
            function moveDraggable(e) {
                drg.style.top = e.pageY - yDisplacement;
                drg.style.left = e.pageX - xDisplacement;
            }
        </script>
    </body>
</html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜