use dragstart preventDefault to disable browser default image drag , but it disable my drag event also

谁说我不能喝 提交于 2019-12-10 21:24:21

问题


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");
        });

回答1:


Nowadays you can use event.dataTransfer.clearData() in the dragstart eventlistener to disable the default behaviour but still execute your desired listener.



来源:https://stackoverflow.com/questions/8712303/use-dragstart-preventdefault-to-disable-browser-default-image-drag-but-it-disa

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