应用悬停效果后,图像链接无法点击。

3

我正在按照指南为我的图片添加悬停效果,以便轻松地在上面显示一些文本。问题是我的图片也作为链接使用,在应用了CSS后不能再点击。有没有办法让它们再次可点击?

.container {
    position: relative;
    width: 200px;
    height: 200px;
    display: inline-block;
}

.image {
    display: block;
    width: 200px;
    height: 200px;
}

.overlay {
    position: absolute;
    transition: all .3s ease;
    opacity: 0;
    background-color: #eee;
}

.container:hover .overlay{
    opacity: 1;
}

.text {
    color: white;
    font-family: sans-serif;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    font-size: 20px;
}

.overlayFade {
    height: 100%;
    width: 100%;
    top: 0;
    left: 0;
    background-color: #00b1bab3;
}

.container:hover .overlayLeft{
    width: 100%;
}
<div class="container">
            <a href="/carportSpidsTag.jsp"><img src="./IMAGES/fladtTag.png" class="image"></a>
            <div class="overlay overlayFade">
                <div class="text">Fade</div>
            </div>
        </div>

1个回答

2

是的,你可以在 .overlay 规则中添加 pointer-events: none;

.container {
  position: relative;
  width: 200px;
  height: 200px;
  display: inline-block;
}

.image {
  display: block;
  width: 200px;
  height: 200px;
}

.overlay {
  position: absolute;
  transition: all .3s ease;
  opacity: 0;
  background-color: #eee;
  pointer-events: none;
}

.container:hover .overlay {
  opacity: 1;
}

.text {
  color: white;
  font-family: sans-serif;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 20px;
}

.overlayFade {
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  background-color: #00b1bab3;
}

.container:hover .overlayLeft {
  width: 100%;
}
<div class="container">
  <a href="/carportSpidsTag.jsp"><img src="./IMAGES/fladtTag.png" class="image"></a>
  <div class="overlay overlayFade">
    <div class="text">Fade</div>
  </div>
</div>


非常简单的答案。我本来想建议将图像和叠加层包装在锚元素内部。虽然在HTML5中这是技术上有效的,但这样做更加简洁。谢谢@JesperChristensen! - Harvey A. Ramer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接