晴明的博客园 GitHub      CodePen      CodeWars     

[js] 拖拽元素

#

<!DOCTYPE html>
<html>

    <head>
        <title></title>
    </head>

    <body>
        <div id="status"></div>
        <div id="myDiv1" class="draggable" style="top:100px;left:0px;background:red;width:100px;height:100px;position:absolute"></div>
        <div id="myDiv2" class="draggable" style="background:blue;width:100px;height:100px;position:absolute;top:100px;left:100px"></div>
        <script type="text/javascript">
            //自定义事件
            function EventTarget() {
                this.handlers = {};
            }
            EventTarget.prototype = {
                constructor: EventTarget,
                addHandler: function(type, handler) {
                    if (typeof this.handlers[type] == "undefined") {
                        this.handlers[type] = [];
                    }
                    this.handlers[type].push(handler);
                },
                fire: function(event) {
                    if (!event.target) {
                        event.target = this;
                    }
                    if (this.handlers[event.type] instanceof Array) {
                        var handlers = this.handlers[event.type];
                        for (var i = 0, len = handlers.length; i < len; i++) {
                            handlers[i](event);
                        }
                    }
                },
                removeHandler: function(type, handler) {
                    if (this.handlers[type] instanceof Array) {
                        var handlers = this.handlers[type];
                        for (var i = 0, len = handlers.length; i < len; i++) {
                            if (handlers[i] === handler) {
                                break;
                            }
                        }
                        handlers.splice(i, 1);
                    }
                }
            };
            var DragDrop = function() {
                var dragdrop = new EventTarget(),
                    dragging = null,
                    diffX = 0,
                    diffY = 0;

                function handleEvent(event) {
                    var target = event.target;
                    switch (event.type) {
                        case "mousedown":
                            if (target.className.indexOf("draggable") > -1) {
                                dragging = target;
                                //                                console.log(event.clientX);//鼠标在客户端的位置
                                //                                console.warn(target.offsetLeft);//元素相对父元素的位置
                                //                                console.log(event.clientY);//鼠标在客户端的位置
                                //                                console.warn(target.offsetTop);    //元素相对父元素的位置    
                                //鼠标在元素内的位置
                                diffX = event.clientX - target.offsetLeft;
                                diffY = event.clientY - target.offsetTop;
                                dragdrop.fire({
                                    type: "dragstart",
                                    target: dragging,
                                    x: event.clientX,
                                    y: event.clientY
                                });
                            }
                            break;
                        case "mousemove":
                            if (dragging !== null) {
                                //修正元素位置
                                dragging.style.left = (event.clientX - diffX) + "px";
                                dragging.style.top = (event.clientY - diffY) + "px";
                                dragdrop.fire({
                                    type: "drag",
                                    target: dragging,
                                    x: event.clientX,
                                    y: event.clientY
                                });
                            }
                            break;
                        case "mouseup":
                            dragdrop.fire({
                                type: "dragend",
                                target: dragging,
                                x: event.clientX,
                                y: event.clientY
                            });
                            dragging = null;
                            break;
                    }
                };
                dragdrop.enable = function() {
                    document.addEventListener('mousedown', handleEvent);
                    document.addEventListener('mousemove', handleEvent);
                    document.addEventListener('mouseup', handleEvent);
                };
                dragdrop.disable = function() {
                    document.removeEventListener('mousedown', handleEvent);
                    document.removeEventListener('mousemove', handleEvent);
                    document.removeEventListener('mouseup', handleEvent);
                };
                return dragdrop;
            }();
            DragDrop.enable();
            DragDrop.addHandler("dragstart", function(event) {
                var status = document.getElementById("status");
                status.innerHTML = "Started dragging " + event.target.id;
            });
            DragDrop.addHandler("drag", function(event) {
                var status = document.getElementById("status");
                status.innerHTML += "<br>Dragged " + event.target.id + " to (" + event.x + "," + event.y + ")";
            });
            DragDrop.addHandler("dragend", function(event) {
                var status = document.getElementById("status");
                status.innerHTML += "<br>Dropped " + event.target.id + " at (" + event.x + "," + event.y + ")";
            });
        </script>
    </body>

</html>

 

posted @ 2016-04-05 17:30  晴明桑  阅读(159)  评论(0编辑  收藏  举报