简体   繁体   English

在Internet Explorer中使用mousemove的问题

[英]Issue with mousemove in Internet Explorer

Internet explorer does not fire onmousemove events when the event target is positioned over an <img> element and lacks a background. 当事件目标位于<img>元素上且缺少背景时,Internet Explorer不会触发onmousemove事件。

But it does register the event when the target has a background. 但它确实在目标具有背景时注册事件。 Does anyone have an explanation for this? 有没有人对此有解释? I had the same behavior in IE10, IE9, and IE8. 我在IE10,IE9和IE8中有相同的行为。

Fiddle here: http://jsfiddle.net/xSpqE/2/ 在这里小提琴: http//jsfiddle.net/xSpqE/2/

As OP asked for a reason, here's my breakthrough: OP问起一个原因,这是我的突破:

It's easier to explain visually, so first off let's add a click handler to the img s: 这在视觉上更容易解释,所以首先让我们为img添加一个点击处理程序:

$('img').click(function() {
  $('.pageX').text('img clicked!');
});

Fiddle 小提琴

Fair enough. 很公平。 You click the image in Chrome/Firefox/not-IE browsers and nothing will happen as the absolutely positioned div covers it. 您单击Chrome / Firefox /非IE浏览器中的图像,因为绝对定位的div覆盖它,所以不会发生任何事情。

Now try it on IE. 现在在IE上试试吧。 The img 's click handler is fired! img的点击处理程序被触发了! Therefore it shows that IE pushes elements through absolutely positioned "transparent" (no content or background) elements. 因此,它表明IE通过绝对定位的“透明”(无内容或背景)元素推送元素。 More interestingly, relatively positioned elements suffer the same issue and setting z-index on either/both elements won't solve it either. 更有趣的是,相对定位的元素遇到相同的问题,并且在任一/两个元素上设置z-index也不会解决它。 Of course, as the image is above the overlay, it won't trigger the overlay's mousemove event. 当然,由于图像位于叠加层上方,因此不会触发叠加层的mousemove事件。

An workaround is to provide some "filling" to the overlay, say background:rgba(0,0,0,0) will force IE to keep the absolutely positioned element at the top. 解决方法是为叠加层提供一些“填充”,比如background:rgba(0,0,0,0)将强制IE将绝对定位的元素保持在顶部。 Fiddle . 小提琴 If you need to support browsers without rgba support, use a 1x1px transparent gif as background. 如果您需要支持没有rgba支持的浏览器,请使用1x1px透明gif作为背景。

I've never seen this defined in any spec nor officially reported as a bug. 我从来没有在任何规范中看到这个定义,也没有正式报告为bug。 There's no logic or reason for absolutely positioned elements without content nor background to suffer this z-index issue, hence I'll call it yet another IE bug. 没有内容或背景的绝对定位元素没有逻辑或理由遭受这个z索引问题,因此我将其称为另一个IE错误。 Maybe reporting it on the Microsoft forums would be of use. 也许在微软论坛上报道它会有用。

Also, related question: IE bug: absolutely-positioned element with a non-transparent background colour 另外,相关问题: IE bug:绝对定位元素,背景颜色不透明

Yet another IE fail! 又一个IE失败了! This is more of a workaround than an answer, but it seems to work alright: 这是一个解决方法而不是一个答案,但它似乎工作正常:

var is_ie = $.browser.msie;
if(is_ie){
    $('.main img').mousemove(function(e){
        $('.pageX').text('pageX: '+e.pageX);
    }
}

That is in addition to the $('.overlay').mousemove function, so keep that too, and of course the is_ie check is optional. 除了$('.overlay').mousemove函数之外,所以也要保留它,当然is_ie检查是可选的。

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

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