CSS 中的悬停叠加图像
我需要一个带有图片 bg 的 div 来在悬停时覆盖图像(具有一定程度的透明度)。我需要能够拥有一个透明的覆盖层,可以在整个网站的任何图像上使用和重复使用。至少可以说,我的第一次尝试是迂回的。因为我发现你不能滚动一个不可见的div,所以我设计了一个三明治系统,其中原始图像是第一层,覆盖层是第二层,原始图像又是第三层。这样,当您将鼠标悬停时,原始图像就会消失,露出原始图像上的覆盖图像:
http://www.nightlylabs.com/uploads/test.html
所以它有效。有点。由于您无法与可见性:不可见元素进行交互(为什么?!),除非您将光标放在其上,否则滚动会闪烁。
有什么帮助吗?这种方法很糟糕,如果有人有更好的方法,请评论。
I need a div with picture bg to overlay an image (with some amount transparency) when hovered on. I need to be able to have one transparent overlay that can be used and reused throughout the site on any image. My first attempt was round-about to say the least. Because I found out you cannot roll-over an invisible div I devised a sandwhich system in which the original image is the first layer, the overlay is the second layer, and the original image is third layer again. This way, when you roll-over, the original image disappears revealing the overlay image over the original image:
http://www.nightlylabs.com/uploads/test.html
So it works. Kinda. Because of you cannot interact with an visibility:invisible element (why?!) the roll-over flickers unless you rest the cursor on it.
Any help? This method is bad so if anyone has a better one please comment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用了以下 css,效果很好。
I used the following css and its fine.
图像会闪烁,因为您无法将鼠标悬停在不存在的东西上。
如果您将其正常分层(无需 z-index)并使其透明,则它将起作用,以便它仍然显示并可以悬停在其上。
第二个图像不会闪烁,您可以使用 span 标签控制样式。以下是我使用的一些 CSS:
hover 标签确定基本 img(和基本 div)的样式,hover+span 定义仅在悬停时出现的 img 的样式。
这是完整显示 div 的 html:
希望这会有所帮助。
The image flickers because you can't hover over something that isn't there.
It will work if you have it layered normally (no z-index necessary) and make it transparent, so that it is still being displayed and can be hovered over.
The second image won't flicker, and you can control the styling with the span tag. Here's some of the CSS I used:
The hover tag determines the styling of the base img (and base div), and hover+span defines the styling of the img that only appears when hovering.
Here is the html showing the div entirely:
Hope this helps.