问题
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