问题
I've got issue with the not-allowed cursor. While dragging the "drag" element, not-allowed cursor appears and I can't drag it anymore. How can i prevent that? I want to make my "drag" element always be "absolute" while mouse is down.
Note: I know that it can happens becouse "pointer-events" but I need it to be contained into this code.
Some code:
$("#drag").bind({
mousedown : function (e) {
var dragged = $(this);
dragged.css({
left : e.pageX - (50 / 2),
top : e.pageY - (50 / 2)
});
dragged.addClass("absolute");
dragged.css({
'pointer-events' : 'none'
})
var upHandler = function () {
dragged.removeClass("absolute");
dragged.css({
'pointer-events' : 'all'
})
$("body").off('mouseup', upHandler);
$("body").off('mousemove', moveHandler);
}
var moveHandler = function (e) {
dragged.css({
left : e.pageX - (50 / 2),
top : e.pageY - (50 / 2)
});
}
$("body").bind({
mouseup : upHandler,
mousemove : moveHandler
})
}
});
$("body").mousemove(function (event) {
$("#log").text("pageX: " + event.pageX + ", pageY: " + event.pageY);
});
https://jsfiddle.net/38zecoL1/1/
Thanks for any help.
回答1:
Before handling your mouse events, make a call to
e.preventDefault();
It cancels the event which prevents the browser from performing the default behavior. Normally it would show a "not allowed" cursor on elements that typically are not draggable.
You can see this in action: https://jsfiddle.net/38zecoL1/4/
来源:https://stackoverflow.com/questions/45534302/jquery-how-to-disable-not-allowed-cursor-while-dragging