mouseup event isn't always triggered

自闭症网瘾萝莉.ら 提交于 2019-12-12 17:06:29

问题


I have a function that allows me to drag a something around. My js:

  $(function() {
        $( svgcanv ).css('cursor', '-moz-grab');

        var leftButtonDown = false;
        $(document).mousedown(function(e){
            console.log('pressed');
            // Left mouse button was pressed, set flag
            if(e.which === 1) leftButtonDown = 1;
        });

        $(document).mouseup(function(e){
            console.log('released');
            // Left mouse button was released, clear flag
            leftButtonDown = 0;
            $( svgcanv ).css('cursor', '-moz-grab');
            firstDragCanvas = 1;
        });

        $( mainGroup ).mousemove(function(e){
            if (leftButtonDown == 1)
            {
                $( svgcanv ).css('cursor', '-moz-grabbing');
                var posX = e.pageX;
                var posY = e.pageY;
                if (firstDragCanvas == 0)
                {
                    posX = e.pageX - canvasPosX;
                    posY = e.pageY - canvasPosY;
                }
                else
                {
                    canvasPosX = e.pageX - translateX;
                    canvasPosY = e.pageY - translateY;
                    posX = translateX;
                    posY = translateY;
                    firstDragCanvas = 0;
                }

                if (posX > 0)
                    posX = 0;

                if (posX < -canvasWidth + $("#holder").width())
                    posX = -canvasWidth + $("#holder").width();

                translateX = posX;
                translateY = posY;

                mainGroup.setAttribute("transform","translate("+posX+","+posY+") scale("+currentZoomLevel+")");                    
            }
        });               
    });

The problem is that mouseup event isn't always triggered. The other 2 works fine. First time I move things around everything works OK. But second if I press and drag immediately the mouseup won't be triggered. If I stop dragging, click somewhere then drag again the script works OK again.

My console (with 2 drags, then click then drag again. notice the 2 consecutive "pressed" strings - logged from the mousedown ):

And no... that is an SVG and need to drag inside it so can't use jQuery draggable.


回答1:


Try adding e.preventDefault(); in each event method to prevent any default UA activity.

E.g.

$(document).mousedown(function(e){
    e.preventDefault();
    console.log('pressed');
    // Left mouse button was pressed, set flag
    if(e.which === 1) leftButtonDown = 1;
});

and so on...



来源:https://stackoverflow.com/questions/9410112/mouseup-event-isnt-always-triggered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!