仅使用 CSS,是否可以触发元素下方的 :hover 和 click 事件?
我有一个半透明的 PNG 作为 div 的背景图像,我将其放置在一些链接上。结果,链接不可点击。有没有办法可以将鼠标悬停并单击“穿过”顶部的 div? (顺便说一句,为了定位到前景 div,我使用绝对定位和 z-index。)
谢谢! 麦克风
I have a semi-transparent PNG as a background image for a div that that I'm placing over some links. As a result, the links aren't clickable. Is there a way I can hover and click "through" the div that's on top? (BTW, to position to foreground div I'm using absolute positioning and z-index.)
Thanks!
Mike
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么不将图像放在背景中(你知道,就像他们为此构建的背景图像属性)?做到这一点的唯一方法是使用一些非常高级的脚本,这会减慢你的页面速度,不值得。您不应该在链接上放置图像。
Why not put the image in the BACKGROUND (you know, like the background-image property that they built for that reason)? The only way to do that is with some highly advanced scripting which would slow your page down, not worth it. You shouldn't be putting an image over your links.
一种选择是使用
:hover
将链接标签的z-index
移至 PNG 上方。我还没有测试过这个,但我想当容器没有悬停时,它会有阴影后面的链接,但当容器悬停时,它会拉动阴影前面的链接。
One option would be to use
:hover
to bring thez-index
of the link tags forward above the PNG.I haven't tested this but I imagine it will have the links behind the shadow when the container isn't hovering but will pull the links before the shadow when the container is hovering.
首先,没有任何理由不能将前景透明度放在链接本身内,从而避免不可点击的问题。让我们举一个简单的例子:
有了新层,就变成了:(
我添加了类名以帮助编码说明。)
然后,覆盖层将被设计样式并绝对定位在原始链接内容之上。覆盖代码块是内联的,并位于后台代码块之后,因此它会自然地分层在先前的代码之上,而无需额外的编码。
首先要做的是将一些格式化属性应用于锚点,以保持锚点内联,但接受内部绝对定位元素。 (-moz- 命令用于支持 FireFox 2。)
然后将装饰性半透明层放置在按钮上。
_background: 属性是一种破解方法,用于在 Internet Explorer 6 版本上删除显示的半透明图像,因为该浏览器对透明 PNG 图像的支持存在固有问题。如果替代图像可用于 IE6 显示,则也可以替换常规 GIF 图像。
您应该做的另一件事是确保链接中的所有内容都提供正确的光标交互。 (某些浏览器,尤其是某些 Internet Explorer 版本,不为链接内的标记提供预期的光标更改。)
第二个选择可能是使用 JavaScript 库来提供基本 HTML 之外的事件处理。我建议使用 JavaScript 库的原因是大多数浏览器仍然不能正确支持 CSS 版本 2 方法,在这些方法中您可以将伪类 :hover 应用于除锚点之外的 DOM 元素。目前获得这种支持的最佳方法是使用库。
添加此悬停属性可以很简单
:
我特别喜欢 jQuery , ID为button_block的包装器,.layer_object是放置在链接上的独立div。]
First, there shouldn't be any reason why you could not put the foreground transparency within the link itself, and thereby avoid the un-clickable problem. Let's take a simple example:
With the new layer this then becomes:
(I've added class names to aid with coding illustration.)
The overlay would then be styled and positioned absolutely above the original link content. The overlay code piece is inline and follows the piece that belongs in the background, therefore it will naturally get layered above the prior code without extra coding.
The first thing to do is to apply some formatting properties to the anchor to keep the anchor inline but accept internal absolute-positioned elements. (The -moz- command is to support FireFox 2.)
Then position your decorative semi-transparent layer over the button.
The _background: property is a hack to remove the semi-transparent image from display on Internet Explorer 6 versions since there is an inherent problem with this browsers support of transparent PNG images. A regular GIF image could also be substituted if an alternate image is available for IE6 display.
One additional thing you should do is make sure all the content within the link provides the proper cursor interraction. (Some browsers, especially some Internet Explorer versions, do not provide expected cursor changes for markup within links.)
Your second option might be to use a JavaScript library to provide event handling beyond the basic HTML. The reason I suggest a JavaScript library is that most browsers still do not properly support CSS version 2 methods where you can apply the pseudo-class :hover to elements of the DOM other than anchors. The best way to approach this support for now is using libraries.
I particularly like jQuery and adding this hover property can be as easy as:
Alternately, you can make a click on the div layer act as a click on the link with:
[The reference to #button_block is assuming the two objects reside in the same wrapper with an ID of button_block, and .layer_object is the independent div placed over the link.]
这个问题在这里解决了: 点击 DIV 到底层元素< /a>
this problem was solved here: Click through a DIV to underlying elements