Firefox拖放重定向停止JQUERY

时间:2013-09-27 12:13:24

标签: javascript jquery html5 html5-canvas fabricjs

我正在使用fabric.js库来创建一个拖放情绪板创建者。一切都很完美,除了Firefox,我将图像拖放到画布上。它会添加图像,但会重定向到img url。重定向后,当我点击后面的btn时,它返回上一页,图像在画布上。

我一直在寻找和阅读几个小时,尝试各种各样的事情。似乎我的代码中的某些东西阻止了“e.stopPropagation();”或者“e.preventDefault()”无法正常运行。我似乎无法找到gremlin。

任何帮助都会很棒。谢谢!

更新:添加了JSFiddle。
这是我的代码jsfiddle:
http://jsfiddle.net/bQxMu/

更新2:
修复了问题,这是修复它的部分:

function handleDrop(e) {
// this / e.target is current target element.

/*
    if (e.stopPropagation) {
        e.stopPropagation(); // stops the browser from redirecting.
    }
*/

    e.stopPropagation(); // Stops some browsers from redirecting.
    e.preventDefault(); // Stops some browsers from redirecting.

    var img = document.querySelector('#images img.img_dragging');

    console.log('event: ', e);

    var newImage = new fabric.Image(img, {
        width: img.width,
        height: img.height,
    // Set the center of the new object based on the event coordinates relative
    // to the canvas container.
        left: e.layerX,
        top: e.layerY
    });
    canvas.add(newImage);

    return false;
}

感谢所有帮助:)

window.onload=function(){

var canvas = new fabric.Canvas('canvas');

canvas.backgroundColor = '#ffffff';


function handleDragStart(e) {
    [].forEach.call(images, function (img) {
        img.classList.remove('img_dragging');
    });
    this.classList.add('img_dragging');

}

function handleDragOver(e) {
    if (e.preventDefault) {
        e.preventDefault(); // Necessary. Allows us to drop.

    }

    e.dataTransfer.dropEffect = 'copy';

    return false;
}

function handleDragEnter(e) {
    // this / e.target is the current hover target.

    this.classList.add('over');

}

function handleDragLeave(e) {
    e.stopPropagation();

    this.classList.remove('over'); // this / e.target is previous target element.

}

function handleDrop(e) {
    // this / e.target is current target element.

    if (e.stopPropagation) {
        e.stopPropagation(); // stops the browser from redirecting.
    }

    var img = document.querySelector('#images img.img_dragging');

    console.log('event: ', e);

    var newImage = new fabric.Image(img, {
        width: img.width,
        height: img.height,
        // Set the center of the new object based on the event coordinates relative
        // to the canvas container.
        left: e.layerX,
        top: e.layerY
    });
    canvas.add(newImage);
    return false;
}

function handleDragEnd(e) {
    // this/e.target is the source node.
    [].forEach.call(images, function (img) {
        img.classList.remove('img_dragging');

    });
}

  var removeSelectedEl = document.getElementById('remove-selected');
  removeSelectedEl.onclick = function() {
    var activeObject = canvas.getActiveObject(),
        activeGroup = canvas.getActiveGroup();
    if (activeGroup) {
      var objectsInGroup = activeGroup.getObjects();
      canvas.discardActiveGroup();
      objectsInGroup.forEach(function(object) {
        canvas.remove(object);
      });
    }
    else if (activeObject) {
      canvas.remove(activeObject);
        $("#imageControl").fadeOut('slow');

    }
  };

  var sendBackwardsEl = document.getElementById('send-backwards');
  sendBackwardsEl.onclick = function() {
    var activeObject = canvas.getActiveObject();
    if (activeObject) {
      canvas.sendBackwards(activeObject);
    }
  };

  var sendToBackEl = document.getElementById('send-to-back');
  sendToBackEl.onclick = function() {
    var activeObject = canvas.getActiveObject();
    if (activeObject) {
      canvas.sendToBack(activeObject);
    }
  };

  var bringForwardEl = document.getElementById('bring-forward');
  bringForwardEl.onclick = function() {
    var activeObject = canvas.getActiveObject();
    if (activeObject) {
      canvas.bringForward(activeObject);
    }
  };

  var bringToFrontEl = document.getElementById('bring-to-front');
  bringToFrontEl.onclick = function() {
    var activeObject = canvas.getActiveObject();
    if (activeObject) {
      canvas.bringToFront(activeObject);
    }
  };

 document.getElementById('saveImg').onclick = function() {
    if (!fabric.Canvas.supports('toDataURL')) {
      alert('This browser doesn\'t provide means to serialize canvas to an image');
    }
    else {
    canvas.deactivateAll().renderAll();
      window.open(canvas.toDataURL('png'));
    }
  };



canvas.on('selection:cleared', function(options) {
    var activeObject = canvas.getActiveObject();
        if (activeObject === null) {
                $("#imageControl").fadeOut('slow');
            }
});

canvas.on('object:selected', function(options) {
    var activeObject = canvas.getActiveObject();
         if (options.target && activeObject !== null) {
                     $("#imageControl").show('slow');
                }
});

canvas.on('selection:created', function(options) {
         if (options.target) {
                    $("#imageControl").show('slow');
                 }
});


if (Modernizr.draganddrop) {
    // Browser supports HTML5 DnD.

    // Bind the event listeners for the image elements
    var images = document.querySelectorAll('#images img');
    [].forEach.call(images, function (img) {
        img.addEventListener('dragstart', handleDragStart, false);
        img.addEventListener('dragend', handleDragEnd, false);
    });
    // Bind the event listeners for the canvas
    var canvasContainer = document.getElementById('canvas-container');
    canvasContainer.addEventListener('dragenter', handleDragEnter, false);
    canvasContainer.addEventListener('dragover', handleDragOver, false);
    canvasContainer.addEventListener('dragleave', handleDragLeave, false);
    canvasContainer.addEventListener('drop', handleDrop, false);

} else {
    // Replace with a fallback to a library solution.
    alert("This browser doesn't support the HTML5 Drag and Drop API.");
}

}

HTML:

<div id="images">
<img draggable="true" src="http://i.imgur.com/q9aLMza.png" width="70" height="90"></img>    
</div>

<div id="canvas-container-controls">
<div id="imageControlWrapper">
<div id="imageControl">
<button class= "imgBtn" id="saveImg">Save Canvas</button>
<button class="imgBtn" id="remove-selected">Remove selected object/group</button>
<button id="send-backwards" class="imgBtn">Send backwards</button>
<button id="send-to-back" class="imgBtn">Send to back</button>
<button id="bring-forward" class="imgBtn">Bring forwards</button>
<button id="bring-to-front" class="imgBtn">Bring to front</button>
</div>
</div>

<div id="canvas-container">
    <canvas id="canvas" width="400" height="400"></canvas>
</div>
</div>

2 个答案:

答案 0 :(得分:6)

在handleDrop中添加e.preventDefault():http://jsfiddle.net/wN29y/

function handleDrop(e) {
// this / e.target is current target element.
e.preventDefault();
if (e.stopPropagation) {
    e.stopPropagation(); // stops the browser from redirecting.
}

var img = document.querySelector('#images img.img_dragging');

console.log('event: ', e);

var newImage = new fabric.Image(img, {
    width: img.width,
    height: img.height,
    // Set the center of the new object based on the event coordinates relative
    // to the canvas container.
    left: e.layerX,
    top: e.layerY
});
canvas.add(newImage);

return false;
}

答案 1 :(得分:0)

我的理解是您应该e.stopPropagation中有handleDragOver,否则浏览器仍会接受该事件,并打开图片。

(类似于我的回答:Why can't I get file dropping to work with jquery?