2013-03-27 60 views
1

解雇我有一些代码归档这样的功能:jQuery的鼠标松开不能在IE8

在浏览器中,我可以拖动图像,这样的代码:

function activateMove() { 
     isMoving = false; 
     $thumb.bind('mousedown', startMove); 
    } 

    function startMove() { 
     isMoving = true; 
     $(document).bind('mousemove', moveUpdate); 
     $(document).bind('mouseup', endMove); 
     return false; 
    } 

    function endMove() { 
     isMoving = false; 
     $(document).unbind('mousemove', moveUpdate); 
     $(document).unbind('mouseup', endMove); 
     return false; 
    } 

我只抄一部分功能,但应足够清楚......

的问题是鼠标松开事件,它的正常工作在所有浏览器,除了IE8

当您拖动拇指并将鼠标释放到浏览器外部时,任何鼠标移动(不按下鼠标)都会导致图像移动。要停止此自动移动,必须再次单击。

因此,这意味着:

  • 点击图片
  • 拖动,然后松开鼠标的页面的可视区域外(如松开鼠标的地址栏中)
  • 现在,如果您移动鼠标向上和向下,用鼠标将图像移动(这是不是一个理想的行为)在

任何可能的解决方案IE8?我花了很多时间在这...

我会在线寻求答案,非常感谢!

+0

[此问题](http://stackoverflow.com/q/6019190/778118)看起来有关。所以[这一个](http://stackoverflow.com/a/11367687/778118)。 – jahroy 2013-03-27 06:17:17

+0

谢谢,但已经尝试谷歌地图之一,不为我工作:( – simon 2013-03-27 06:32:38

+0

可能重复的[链接](http://stackoverflow.com/q/4595606/467853) – 2013-03-27 06:35:27

回答

2

这是由于IE8中的一个错误,我认为是由于执行不当的安全修复。

在IE8中,鼠标一旦离开文档就没有鼠标事件触发,包括document.mouseup - 我想这是由于IE中的一个早期安全错误,您可以在浏览器窗口之外获得mouseclicks的坐标。

为了解决这个问题,您需要在鼠标离开内容区域时触发另一个事件。幸运的是IE这里有分寸的事件,我们可以使用:

function startMove() { 
    isMoving = true; 
    $(document).bind('mousemove', moveUpdate); 
    return false; 
} 

function endMove() { 
    isMoving = false; 
    $(document).unbind('mousemove', moveUpdate); 
    return false; 
} 

function activateMove() { 
    isMoving = false; 
    $thumb.bind('mousedown', startMove); 
    $(document).bind('mouseup', endMove); 

    //IE8 hack - also end the move when the mouse leaves the document 
    $(document).bind('mouseleave', endMove); 
} 

注意mouseleave是必要的行为 - 一旦当鼠标离开文档区域它激发。 IE以外的浏览器支持mouseout,但每次鼠标穿过子内容时都会触发。这很有用,所以jQuery spoofs it in other browsers