<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Drag</title>
<script type="text/javascript">
//var rePos = function () {
//    return !window.opera && document.documentElement.getBoundingClientRect && function (o) {
//        var pos = o.getBoundingClientRect()
// , root = o.ownerDocument
// , x = pos.left + root.documentElement.scrollLeft - 2
// , y = pos.top + root.documentElement.scrollTop - 2;
//
// try {
// var f = root.parentWindow || null;
// if (f) {
// var offset = 2 - (f.frameBorder || 1) * 2;
// x += offset, y += offset;
// }
// } catch (exp) {}
// return { x : x, y : y };
//    } || function (o) {
//        var x = 0, y = 0;
//        do { x += o.offsetLeft, y += o.offsetTop; } while (o = o.offsetParent);
//        return { 'x' : x, 'y' : y };
//    };
//}(); 
var rePos = function () {
    return document.documentElement.getBoundingClientRect && function (o) {//返回一个矩形区域
        var pos = o.getBoundingClientRect(), root = o.ownerDocument || o.document;
        return { x : pos.left + root.documentElement.scrollLeft, y : pos.top + root.documentElement.scrollTop };
    } || function (o) {
        var x = 0, y = 0;
        do { x += o.offsetLeft, y += o.offsetTop; } while (o = o.offsetParent);
        return { 'x' : x, 'y' : y };
    };
}();var Drag = {    X : 0, Y : 0, MF : null, EF : null, BF : null
    
    , AE : function (element, type, listener) {
    //添加
        var wc = this, listeners = function () { listener.apply(wc, arguments); };
        
        if (element.addEventListener) element.addEventListener(new String(type).substr(2), listeners, false);
        else if (element.attachEvent) element.attachEvent(type, listeners);
        
        return listeners;
    }
    
    , DE : function (element, type, listener) {
    //删除
        if (element.addEventListener) element.removeEventListener(new String(type).substr(2), listener, false);
        else if (element.attachEvent) element.detachEvent(type, listener);
    }
    
    , start : function (moveObj) {
    //开始
        if (this.MO) return;
        
        var pos = rePos(this.MO = moveObj), e = window.event || arguments.callee.caller.arguments[0];
        
        this.X = document.documentElement.scrollLeft + e.clientX - pos.x;
        this.Y = document.documentElement.scrollTop + e.clientY - pos.y;
        
        if (moveObj.setCapture) {
            this.BF = this.AE(moveObj, 'onlosecapture', this.end);
            moveObj.setCapture();
        } else {
            this.BF = this.AE(window, 'onblur', this.end);
        }
        this.MF = this.AE(document, 'onmousemove', this.move);
        this.EF = this.AE(document, 'onmouseup', this.end);
    }
    
    , move : function (e) {
    //移动中
        var e = window.event || e;
        
        window.getSelection && window.getSelection().removeAllRanges();
        
        with (this.MO.style) {
            left = document.documentElement.scrollLeft + e.clientX - this.X + 'px';
            top = document.documentElement.scrollTop + e.clientY - this.Y + 'px';
        }
        //IEscroll问题无视^^
    }
    
    , end : function () {
    //结束
        if (this.MO.setCapture) {
            this.DE(this.MO, 'onlosecapture', this.BF);
            this.MO.releaseCapture();
        } else {
            this.DE(window, 'onblur', this.BF);
        }
        this.DE(document, 'onmousemove', this.MF);
        this.DE(document, 'onmouseup', this.EF);
        
        this.MO = this.BF = this.MF = this.EF = null;
    }
    
};
</script>
</head>
<body>
<div style="width:100px;height:100px;background-color:#FF0000;position:absolute;" onmousedown="Drag.start(this)">第一个</div>
<div style="width:100px;height:100px;background-color:#00FF00;position:absolute;" onmousedown="Drag.start(this)">第二个</div>
<div style="width:100px;height:100px;background-color:#0000FF;position:absolute;" onmousedown="Drag.start(this)">第三个</div>
<input type="text" id="s1"/>
<input type="text" id="s2"/>
<input type="button" onclick="javascript:document.getElementById('s1').value=Drag.X" />
<center>字有好多</center>
<!--
frame在FF里会导致丢失捕获的BUG
用alpha:0的XX盖住就可以了
这个就不测试了^^
<iframe style="background-color:#CCCC99; width:700px; height:500px;"></iframe>
-->
</body>
</html>

解决方案 »

  1.   

    支持 
    我已经测试过了 
    支持 ie 6,7,8 ff
      

  2.   

    你没有看到 ff下dom2 的操作吗?
      

  3.   

    ff 测试成功
    ff 最新版本下 没问题
      

  4.   

    syntax error
    iframeAutoFit()ReplyTop...3a56%3a11 (行 38)
    [Break on this error] var pos = o.getBoundingClientRec... root = o.ownerDocument | | o.document;\n
    ReplyTop...3a56%3a11 (行 38)
    Drag is not defined
    onmousedown()但是在FF3里面为什么会这样呢~