简体   繁体   English

为什么使用 JavaScript 拖动图像在 Firefox 中不起作用?

[英]Why doesn't dragging images using JavaScript work in Firefox?

I'm using the code below for dragging an image ( #StndSoln1 ).我正在使用下面的代码来拖动图像 ( #StndSoln1 )。 It works perfectly in Chrome and all, but not in Firefox.它在 Chrome 和所有浏览器中都能完美运行,但在 Firefox 中却不行。 Here startDrag() is the function which i attached to the mousedown event listener.这里 startDrag() 是我附加到 mousedown 事件侦听器的函数。 Could anybody please help me.谁能帮帮我。

function initialFunction(){


document.getElementById("StndSoln1").addEventListener('mousedown',startDrag,false);
document.getElementById("StndSoln1").addEventListener('mousemove',drag,false);
document.getElementById("StndSoln1").addEventListener('mouseup',stopDrag,false);


}


function startDrag()

{

if (!moveFlag){


currentTraget=document.getElementById("StndSoln1");

offsetX=currentTraget.offsetLeft;
offsetY=currentTraget.offsetTop;
ImgPlaced=false;    
moveFlag=true;

x=window.event.clientX;
y=window.event.clientY; 

event.preventDefault();
}
}

   // Fn for drag the current target object...
  function drag(){

if (moveFlag && !ImgPlaced){    
    currentTraget.style.left=(offsetX+window.event.clientX-x)+"px";
    currentTraget.style.top=(offsetY+window.event.clientY-y)+"px";
}
}

I actually had a similar problem, so I can try to help even without the code you're using.我实际上遇到了类似的问题,所以即使没有您正在使用的代码,我也可以尝试提供帮助。

See, the Firefox developers had this bright idea of making it so that, when you drag an image, you can "move" it around and possibly drop it in an Explorer window to quickly and easily download it, or to the tab bar to open the image in a new tab.瞧,Firefox 开发人员有一个绝妙的主意,当您拖动图像时,您可以四处“移动”它,并可能将它放到资源管理器窗口中以快速轻松地下载它,或者将它放到选项卡栏中以打开新标签中的图像。 The obvious downside of this is that it results in a default behaviour that other browsers don't have.这样做的明显缺点是它会导致其他浏览器没有的默认行为。

The simple solution is to make sure that all your events are properly cancelling the default action ( event.preventDefault , return false , that kind of thing).简单的解决方案是确保您的所有事件都正确取消了默认操作( event.preventDefaultreturn false ,诸如此类)。 Should that fail too, then you should use a <div> element with a background-image instead of an <img> element.如果这也失败了,那么您应该使用带有background-image<div>元素而不是<img>元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM