代码之家  ›  专栏  ›  技术社区  ›  Maheer Ali

调用onmousedown并在js的自定义类中使用onclick

  •  0
  • Maheer Ali  · 技术社区  · 6 年前

    当我拖动它的时候,我想用移动做一个圆圈(军队),当我点击它的时候,它不应该移动而是做其他的事情。

    fucntion unit() {
        this.circle = document.createElementNS(svgNs, "circle");
        //Some attributes are added to this.circle
        document.getElementById("svg1").appendChild(this.circle);
        this.moving = false;
        this.circle.onclick = function () {
            showForm(this);
        }
        this.circle.onmousedown = function () {
            this.moving = true;
        }
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Random Channel    6 年前

    好吧,我知道你没有在代码中标记jquery,但是如果你看过我的其他文章,我倾向于使用完整的jquery。如果您想这样做,请执行以下操作:

    假设这适用于圆id元素:

    $("#circle").draggable( 'enable' )
    var isDragging = 0;
    var isUp = 1;
    $("#circle").mousedown(function() {
        isDragging = 0;
        isUp = 0;
    })
    $("#circle").mousemove(function() {
        isDragging = 1;
     });
    $("#circle").mouseup(function() {
        isDragging = 0;
        isUp = 1;
    });
    $("#circle").mousedown(function() {
        if (isUp == 1) {
           //click function
    
        } else if (isDragging == 1) {
           //drag function
        }
    });
    

    这可能是辆小车,但我试过了。