use dragstart preventDefault to disable browser de

2019-07-08 03:45发布

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条回答
趁早两清
2楼-- · 2019-07-08 04:48

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

查看更多
登录 后发表回答