简单的js拖动

window.onload = function () {
            var oDiv = document.getElementById("oDiv"); //oDiv必须使用CSS定位
            oDiv.onmousedown = drag;
            function drag(evt) {
                evt = evt || window.event;
                this.onmouseup = drop;
                this.onmousemove = moveDiv;
                this.offset = {
                    x: evt.offsetX,
                    y: evt.offsetY
                };
            }
            function moveDiv(evt) {
                evt = evt || window.event;
                this.style.left = evt.clientX - this.offset.x + "px";
                this.style.top = evt.clientY - this.offset.y + "px";
            }
            function drop(evt) {
                this.onmouseup = null;
                this.onmousemove = null;
            }
        };

posted @ 2013-03-25 21:48  程序有Bug  阅读(160)  评论(0编辑  收藏  举报