问题描述
我尝试防止在dragstart中浏览器默认图像拖动,但是不知何故它也禁用了drag和dragend事件.无论如何,我可以禁用浏览器的默认图像拖动功能,但它仍将运行拖动和拖动结束事件?
I try to prevent the browser default image drag in dragstart, but somehow it disable the drag and dragend event also. Anyway i can disable the browser default image drag , but it will still run the drag and drag end event?
还是唯一的选择是使用背景图像?我不想这样做,因为因此我需要更改很多代码.现在我知道为什么每个人都使用背景图像了.
Or the only option for be is to use background image? I dont want to do this because I need to change a lot of code because of this. Now I know why everyone using background-image.
$("#addObjectBarContainer a img")
.bind("dragstart", function(event){
var tmpObjImg = $("<img></img>");
tmpObjImg
.attr({
id: "temp-img-object-drag"
, src: $(this).attr("src")
})
.css({
position:"absolute"
});
tmpObjImg.appendTo("body");
event.preventDefault();
})
.bind("drag" , function(event){
$("#temp-img-object-drag").css({
top: event.pageY,
left: event.pageX
});
})
.bind("dragend",function(event){
var ObjectTop = event.pageY - $("#canvas").offset().top;
var ObjectLeft = event.pageX -$("#canvas").offset().left
$("#temp-img-object-drag").remove();
$(this).parent().trigger("click");
$(activeObject).css({
top: ObjectTop,
left: ObjectLeft
});
$(activeObject,"div:first-child").css({
top: ObjectTop,
left: ObjectLeft
});
dragresize.elmY = ObjectTop;
dragresize.elmX = ObjectLeft;
inspector_Update(activeObject, "select");
});
推荐答案
如今,您可以在dragstart事件侦听器中使用event.dataTransfer.clearData()
禁用默认行为,但仍执行所需的侦听器.
Nowadays you can use event.dataTransfer.clearData()
in the dragstart eventlistener to disable the default behaviour but still execute your desired listener.
这篇关于使用dragstart preventDefault禁用浏览器默认图像拖动,但它也禁用我的拖动事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!