开发者

send two array to a function that run with document.attachEvent("onmousemove", dragGo)

I want to pass all_dragable_obj_left and all_dragable_obj_top arrays to dragGo function in:

1. document.attachEvent("onmousemove", dragGo); and

2. document.addEventListener("mousemove", dragGo, true);.

Here is the code:

function dragStart(event, id) {
    var el;
    var x, y;

    if (id)
        dragObj.elNode = document.getElementById(id);
    else {
        if (browser.isIE)
            dragObj.elNode = window.event.srcElement;
        if (browser.isNS)
            dragObj.elNode = event.target;

        i开发者_如何学Gof (dragObj.elNode.nodeType == 3)
            dragObj.elNode = dragObj.elNode.parentNode;
    }

    if (browser.isIE) {
        x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
        y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
    }
    if (browser.isNS) {
        x = event.clientX + window.scrollX;
        y = event.clientY + window.scrollY;
    }

    // Save starting positions of cursor and element.
    $("img[class=obj_mov]").each(function () {                
        all_dragable_obj_left.push(parseInt($(this).css("left"), 10));
        all_dragable_obj_top.push(parseInt($(this).css("top"), 10));
    });
    dragObj.cursorStartX = x;
    dragObj.cursorStartY = y;
    if (browser.isIE) {
        document.attachEvent("onmousemove", dragGo);
        document.attachEvent("onmouseup", dragStop);
        window.event.cancelBubble = true;
        window.event.returnValue = false;
    }
    if (browser.isNS) {
        document.addEventListener("mousemove", dragGo, true);
        document.addEventListener("mouseup", dragStop, true);
        event.preventDefault();
    }
}

function dragGo(event) {
    var x, y;
    if (browser.isIE) {
        x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
        y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
    }
    if (browser.isNS) {
        x = event.clientX + window.scrollX;
        y = event.clientY + window.scrollY;
    }
    var i = 0;
    i = 0;
    $("img[class=obj_mov]").each(function () {
        $(this).css("left",all_dragable_obj_left[i]+ x - dragObj.cursorStartX);
        $(this).css("top",all_dragable_obj_top[i]+ y - dragObj.cursorStartY);
        i++;
    });            

    if (browser.isIE) {
        window.event.cancelBubble = true;
        window.event.returnValue = false;
    }
    if (browser.isNS)
        event.preventDefault();
}

function dragStop(event) {
    dragObj.elNode = null;
    if (browser.isIE) {
        document.detachEvent("onmousemove", dragGo);
        document.detachEvent("onmouseup", dragStop);
    }
    if (browser.isNS) {
        document.removeEventListener("mousemove", dragGo, true);
        document.removeEventListener("mouseup", dragStop, true);
    }
}


1. First

document.attachEvent("onmousemove", function(e){
    dragGo(e, all_dragable_obj_left, all_dragable_obj_top);
});

2. Second

document.addEventListener("mousemove", function(e){
    dragGo(e, all_dragable_obj_left, all_dragable_obj_top);
}, true);

3. dargGo() function

function dragGo(event, all_dragable_obj_left, all_dragable_obj_top) {

    var x, y;
    if (browser.isIE) {
        x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
        y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
    }
    if (browser.isNS) {
        x = event.clientX + window.scrollX;
        y = event.clientY + window.scrollY;
    }
    var i = 0;
    i = 0;
    $("img[class=obj_mov]").each(function () {
        $(this).css("left",all_dragable_obj_left[i]+ x - dragObj.cursorStartX);
        $(this).css("top",all_dragable_obj_top[i]+ y - dragObj.cursorStartY);
        i++;
    });            

    if (browser.isIE) {
        window.event.cancelBubble = true;
        window.event.returnValue = false;
    }
    if (browser.isNS)
        event.preventDefault();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜