封了个公用拖拽。。。。

 

<!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>D类</title>
<style type="text/css">
html, body {
    margin:0;
}
</style>
<script type="text/javascript">
!!window.__defineGetter__ && !/MSIE/.test(navigator.userAgent) && function () {
    !window.opera && window.__defineGetter__('event', function () {
    //兼容Event对象
        var o = arguments.callee;
        do {
            if (o.arguments[0] instanceof Event) return o.arguments[0];
        } while (o = o.caller);
        return null;
    });
   
    window.attachEvent = Document.prototype.attachEvent = Element.prototype.attachEvent = function (type, listener, capture) {
    //兼容attachEvent方法
        return this.addEventListener(new String(type).substr(2), listener, capture || false);
    };
   
    window.detachEvent = Document.prototype.detachEvent = Element.prototype.detachEvent = function (type, listener, capture) {
    //兼容detachEvent方法
        return this.removeEventListener(new String(type).substr(2), listener, capture || false);
    };
   
}();
var D = {
//拽补助类
    lock : false
   
    , dom : function () {
    //document相关属性
        return {
            left : document.documentElement.scrollLeft
            , top : document.documentElement.scrollTop
            , width : document.documentElement.clientWidth
            , height : document.documentElement.clientHeight
            , body : document.documentElement
        };
    }

    , mos : function (e) {
    //获取鼠标绝对位置
        var dom = this.dom();
       
        return { 'x' : dom.left + e.clientX, 'y' : dom.top + e.clientY }
    }
   
    , pos : function (o) {
    //获取元素绝对位置
        var x = 0, y = 0;
        do { x += o.offsetLeft, y += o.offsetTop; } while (o = o.offsetParent);
        return { 'x' : x, 'y' : y };
    }
   
    , start : function (element, startEventHandler, moveEventHandler, stopEventHandler) {
    //移动开始
        if (this.lock) return;
        else this.lock = true; //先上锁安全。。。:D
   
        var pos = this.pos(element) //元素位置
            , mos = this.mos(window.event) //鼠标位置
            , eventHandlers = { MF : null, EF : null } //事件记录
            , property = { x : mos.x - pos.x, y : mos.y - pos.y, left : pos.x, top : pos.y } //属性
            , _MF = this.move(moveEventHandler, property) //移动过程事件闭包
            , _EF = this.stop(element, stopEventHandler, eventHandlers); //移动停止事件闭包
  
  try { document.selection && document.selection.empty && (document.selection.empty(), 1) || window.getSelection && window.getSelection().removeAllRanges(); } catch (exp) {}
       
        document.attachEvent('onmousemove', _MF); //鼠标移动
        document.attachEvent('onmouseup', _EF); //鼠标释放
       
        element.setCapture
            ? (element.setCapture(), element.attachEvent('onlosecapture', _EF))
            : window.attachEvent('onblur', _EF); //鼠标捕获丢失则释放
       
        eventHandlers.MF = _MF, eventHandlers.EF = _EF;
       
        startEventHandler && startEventHandler(property); //直接传过去。。。请误污染- -
    }
   
    , move : function (moveEventHandler, property) {
    //移动中
        var wc = this;
       
        return function (e) {
            var mos = wc.mos(e || window.event), dom = wc.dom();
            window.getSelection && window.getSelection().removeAllRanges();
           
            /MSIE/.test(navigator.userAgent) && function () {
            //IE滚屏
                if (mos.x > dom.left + dom.width) dom.body.scrollLeft = mos.x - dom.width;
                else if (mos.x < dom.left) dom.body.scrollLeft = mos.x;

                if (mos.y > dom.top + dom.height) dom.body.scrollTop = mos.y - dom.height;
                else if (mos.y < dom.top) dom.body.scrollTop = mos.y;
            }();
   
   property.left = mos.x - property.x, property.top = mos.y - property.y;
           
            moveEventHandler && moveEventHandler(property); //直接传过去。。。请误污染- -;
        };
    }
   
    , stop : function (element, stopEventHandler, eventHandlers) {
    //移动结束
        var wc = this;
        return function (e) {
            document.detachEvent('onmousemove', eventHandlers.MF);
            document.detachEvent('onmouseup', eventHandlers.EF);
            element.setCapture
                ? (element.detachEvent('onlosecapture', eventHandlers.EF), element.releaseCapture())
                : window.detachEvent('onblur', eventHandlers.EF);
   wc.lock = false; //事件都干掉了可以放心解锁了
   stopEventHandler && stopEventHandler();
        };
    }
   
};
</script>
</head>
<body>
<div style="position:relative; width:100px; height:100px; background-color:#000000;">
    <div style="position:absolute; width:20px; height:20px; line-height:20px; left:50%; top:50%; margin:-10px 0 0 -10px;

background-color:#F00; text-align:center; cursor:move;"
        onmousedown="var wc = this;D.start(wc, null, function (property) {
            var node = wc.parentNode;
            var a = D.pos(node), b = D.pos(wc);
            node.style.left = property.left - b.x + a.x + 'px';
            node.style.top = property.top - b.y + a.y + 'px';
        }, null);"
    >拽</div>
</div>

<div style="width:50px; height:50px; line-height:50px; text-align:center; background-color:#F00; position:absolute;

cursor:move;"
    onmousedown="var wc = this;D.start(wc, null, function (property) {
        wc.style.left = property.left + 'px';
        wc.style.top = property.top + 'px';
    }, null);"
>拽</div>
<div style="height:1000px;"></div>
</body>
</html>

posted @ 2008-07-17 10:31 越兔 阅读(2762) 评论(15) 编辑 收藏

 回复 引用   
#1楼2008-07-23 13:42 | dh20156[未注册用户]
强大是强大,不过这么长的篇幅......
 回复 引用   
#2楼2008-07-25 17:27 | rank_liu[未注册用户]
完成了基本需求。
 回复 引用 查看   
#3楼[楼主]2008-08-26 10:44 | 越兔      
@dh20156
呃。。。
换了种代码风格所以。。。。

 回复 引用 查看   
#4楼2008-10-02 11:47 | 花易落      
/Firefox/.test(navigator.userAgent) && function ()这句我看不懂

还有

var a = function () {
var z = 1;
return function () {
alert(z);
};
}();

这种写法我也看不懂。为什么 var a=function(){}(还要这个括号);??

 回复 引用 查看   
#5楼[楼主]2008-10-08 10:38 | 越兔      
@花易落
那个括号是执行用的。。。
这东西叫表达式声名。
它会把这个函数执行,然后返回给等号左边函数的返回值。。。

 回复 引用 查看   
#6楼2008-10-08 11:28 | 花易落      
/Firefox/.test(navigator.userAgent) && function ()

这句呢?

谢谢指点啊。

 回复 引用 查看   
#7楼[楼主]2008-10-08 15:13 | 越兔      
@花易落
就是判断是否是FF,如果是就执行那个函数。。。
类似
if (ff) func();

 回复 引用 查看   
#8楼2008-10-08 15:32 | 花易落      
嗯嗯。谢谢你啊!!!学到不少东西
 回复 引用 查看   
#9楼2008-10-09 10:20 | 花易落      
$("bgDiv").attachEvent("onclick",function(){
alert('x');
$("bgDiv").detachEvent("onclick",function(){alert('remove');});

});

我想点一次div的时候,弹出alert('x');然后再点,就不能用了。结果他detachEvent,不能移出onlcick这个事件,帮帮我!!!!

 回复 引用 查看   
#10楼2008-10-09 10:21 | 花易落      
可以加你QQ吗?我需要你的指导。。。我的QQ是229176739。加我好吗?


 回复 引用 查看   
#11楼[楼主]2008-10-28 17:21 | 越兔      
有两个低级BUG...改完了...鄙视下自己...飘走...
 回复 引用   
#12楼2008-11-06 16:45 | 痕[未注册用户]
请问楼主
try { document.selection && document.selection.empty && (document.selection.empty(), 1) || window.getSelection && window.getSelection().removeAllRanges(); } catch (exp) {}
这句具体是什么用
因为我发现在FF里需要用这句,拖动才完全正确,不然有bug

 回复 引用 查看   
#13楼[楼主]2008-11-06 20:02 | 越兔      
@痕
这个是清理selection的..
selection就是你选中的内容...
背景变蓝,字变白那种...

因为那种情况会导致ondrag触发,并且出现不可拖拽(禁止的图标)产生bug..
所以要河蟹掉selection

 回复 引用   
#14楼2008-11-12 17:29 | Todd Chang[未注册用户]
好多堪布懂的地方啊,学习颇深啊~,偶像!
 回复 引用 查看   
#15楼2009-01-14 17:46 | 痴情客      
cool