0

我有以下功能:

div_paper.onmousedown = function(event) {
    var mouseCoords = getCoords(event);
    startX = mouseCoords.x - offset[0];
    startY = mouseCoords.y - offset[1];
    rect = paper.rect(startX, startY, 0, 0);
    document.onmousemove = doDraw;
    // the function below i would like to fire once
    rect.drag(drag_move, drag_start, drag_up);
};

如您所见,它会在每次鼠标按下时触发。我drag只想在第一次鼠标按下后触发该功能一次。我应该.one()为此使用jQuery吗?

提前致谢。

4

1 回答 1

2

这是即兴的,但您可以尝试:

var dragged = false;
div_paper.onmousedown = function(event) {
    var mouseCoords = getCoords(event);
    startX = mouseCoords.x - offset[0];
    startY = mouseCoords.y - offset[1];
    rect = paper.rect(startX, startY, 0, 0);
    document.onmousemove = doDraw;
    // the function below i would like to fire once
    if (!dragged)
    {
       dragged = true;
       rect.drag(drag_move, drag_start, drag_up);
    }
};
于 2013-06-20T19:14:52.473 回答