JS拖动(简单)

/*-------------------------鼠标拖动---------------------*/
    var od = document.getElementById("fd");
    var dx, dy, mx, my, mouseD;
    var odrag;
    var isIE = document.all ? true : false;
    document.onmousedown = function(e) {
        var e = e ? e : event;
        if (e.button == (document.all ? 1 : 0)) {
            mouseD = true;
        }
    }
    document.onmouseup = function() {
        mouseD = false;
        odrag = "";
        if (isIE) {
            od.releaseCapture();
            od.filters.alpha.opacity = 100;
        }
        else {
            window.releaseEvents(od.MOUSEMOVE);
            od.style.opacity = 1;
        }
    }


    //function readyMove(e){   
    od.onmousedown = function(e) {
        odrag = this;
        var e = e ? e : event;
        if (e.button == (document.all ? 1 : 0)) {
            mx = e.clientX;
            my = e.clientY;
            od.style.left = od.offsetLeft + "px";
            od.style.top = od.offsetTop + "px";
            if (isIE) {
                od.setCapture();
                od.filters.alpha.opacity = 50;
            }
            else {
                window.captureEvents(Event.MOUSEMOVE);
                od.style.opacity = 0.5;
            }

            //alert(mx);
            //alert(my);

        }
    }
    document.onmousemove = function(e) {
        var e = e ? e : event;

        //alert(mrx);
        //alert(e.button);       
        if (mouseD == true && odrag) {
            var mrx = e.clientX - mx;
            var mry = e.clientY - my;
            od.style.left = parseInt(od.style.left) + mrx + "px";
            od.style.top = parseInt(od.style.top) + mry + "px";
            mx = e.clientX;
            my = e.clientY;

        }
    }
    function showBackground(obj, endInt) {
        obj.filters.alpha.opacity += 1;
        if (obj.filters.alpha.opacity < endInt) {
            setTimeout(function() { showBackground(obj, endInt) }, 8);
        }
    }

posted @ 2009-03-09 11:53  悟〈--觉  阅读(263)  评论(0编辑  收藏  举报