不可见元素不接收鼠标事件

发布于 2024-12-05 11:38:47 字数 554 浏览 0 评论 0原文

我正在用 jquery 为一个网站编写一个样本选择器,允许您指定座套的颜色。选择器由缩略图网格组成,当用户将鼠标悬停在每个缩略图上时,顶部会显示更大的图像。

现在的问题是,客户希望被覆盖的更大图像部分或完全遮挡的图像仍然能够响应事件。

我对这个问题的解决方案是添加一个预览元素,以将更大的图片显示到 z 索引为 5 的列表中。然后,我将克隆样本列表中的原始元素集,并将它们覆盖为带有 z 的不可见元素-index 为 10。结果是部分被遮挡的元素似乎仍然响应鼠标事件,但实际上底层元素没有附加事件。这些事件实际上附加到预览元素前面的不可见元素(我希望这是有道理的!)。

我第一次尝试实现这种效果是让克隆元素获得visibility:hidden CSS 样式,但这些元素不响应鼠标事件。我尝试使用背景为透明的空元素,这似乎工作正常,但在 IE9 中测试表明这些元素也不响应鼠标事件!

如果我从覆盖元素中删除背景:透明样式,我就可以让它工作,当然现在它们遮盖了下面的所有内容。

目前看来只有IE9有这个问题。 IE8 似乎可以很好地触发透明项目上的事件。它似乎在 FireFox 和 Chrome 中也能按预期工作。

I'm writing a swatch picker in jquery for a site that allows you to specify a colour for a seat cover. The picker consists of a grid of thumbnail images, when the user mouses over each of these thumbnails, a bigger image is shown over the top.

Now the thing is, the client wants the images that are partially or totally obscured by the overlying bigger image to still respond to events.

My solution to this problem was to add a preview element for showing the bigger picture to the list with a z-index of 5. Then I'd clone the original set of elements in the swatch list and overlay them as invisible elements with a z-index of 10. The result is that the partially obscured elements appear to still respond to mouse events, though in actuality the underlying elements don't have events attached. The events are actually attached to invisible elements in front of the preview element (I hope that makes sense!).

My first attempt to achieve this effect was for the cloned elements to get a visibility: hidden CSS style, but these don't respond to mouse events. I tried using empty elements with background: transparent instead, and this seemed to work fine, but testing in IE9 revealed that these elements don't respond to mouse events either!

I can get it to work if I remove the background:transparent style from the overlay elements, bot of course now they obscure everything underneath.

It only seems to be IE9 that has this issue so far. IE8 appeared to trigger the events on transparent items fine. It also seems to work as intended in FireFox and Chrome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想你的星星会说话 2024-12-12 11:38:47

最终的解决方案简单得令人恼火。所需要的只是为不可见元素提供以下样式:

background-color: white;
opacity: 0;
filter: alpha(opacity=0); /* for old IE versions */

这使得元素不可见,但仍然响应鼠标事件。

The solution in the end was annoyingly simple. All that was needed was to give the invisible elements the following styling:

background-color: white;
opacity: 0;
filter: alpha(opacity=0); /* for old IE versions */

This leaves the elements invisible, but still responsive to mouse events.

浪漫人生路 2024-12-12 11:38:47

我将为此使用双重绑定技术,其中鼠标悬停绑定到后面的图像,而out绑定到前面的图像。这允许您隐藏前面的图像,直到悬停后面的图像。

// use $.fn.each so that each thumb gets its own timer.
$(".thumb-behind").each(function(){
  var timer;
  $(this).hover(function(){
    $(this).next().stop(true,true).fadeIn();
  },function(){
    timer = setTimeout(function(){
      $(this).next().stop(true,true).fadeOut();
    },10);
  });

  $(this).next().hover(function(){
    clearTimeout(timer);
  },function(){
    $(this).stop(true,true).fadeOut();
  });
});

只需确保修改 $(this).next() 以选择相对于当前缩略图较大的缩略图。

I would use a double binding technique for this, where the mouseover is bound to the behind image, and the out is bound to the front image. that allows you to have the front image hidden until the behind image is hovered.

// use $.fn.each so that each thumb gets its own timer.
$(".thumb-behind").each(function(){
  var timer;
  $(this).hover(function(){
    $(this).next().stop(true,true).fadeIn();
  },function(){
    timer = setTimeout(function(){
      $(this).next().stop(true,true).fadeOut();
    },10);
  });

  $(this).next().hover(function(){
    clearTimeout(timer);
  },function(){
    $(this).stop(true,true).fadeOut();
  });
});

just make sure you modify $(this).next() to select the larger thumbnail in relation to the current thumbnail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文