【问题标题】:How can I enable pointer-events to pass through elements without disabling them?如何启用指针事件以通过元素而不禁用它们?
【发布时间】:2018-08-12 10:52:09
【问题描述】:

所以我有一个完整的列表项,我希望它是可点击的,但里面有一些链接也需要是可点击的。事实证明你不能嵌套<a> 标签,所以我创建了列表项和<a> 兄弟姐妹,并将它们定位为重叠。

为了使列表的链接正常工作,我必须禁用指针事件,以便它通过下面的链接。然后我必须在容器中的每个事件上重新启用指针事件,因为当它们的父对象是时它们将被禁用。

现在的问题是,当您将鼠标悬停在列表中的链接上时,背景悬停将停止触发。

我希望这样当您将鼠标悬停在容器内的链接上时,会显示链接悬停(并且 href 显示在底部),但下面的链接仍显示为悬停状态。

这是示例,请注意当您将鼠标悬停在框内的链接上时,框停止悬停。

#listlink {
  height: 100px; width: 100px; top:0; left:0;
  position: absolute;

  background: rgba(255,0,0,0.4);
}

#listlink:hover {  background: rgba(255,0,0,0.8);}

#listitem {
  height: 100px; width: 100px; top:0; left:0;
  position: absolute;
  
  z-index: 10;
  pointer-events: none;
}

#childlink {
  background: rgba(0,0,255,0.4);
  pointer-events: auto;
}
#childlink:hover {  background: rgba(0,0,255,0.8);}
<a id="listlink" href="#listlink"></a>
<div id="listitem">
  Text <a id="childlink" href="#childlink">top link</a> text
</div>

【问题讨论】:

  • This:#listitem { background: rgba(255, 0, 0, 0.4); } #listitem:hover { background: rgba(255, 0, 0, 0.8); } #childlink { background: rgba(0, 0, 255, 0.4); } #childlink:hover { background: rgba(0, 0, 255, 0.8); } 似乎可以在没有你额外的 &lt;a&gt; 的情况下完成这项工作

标签: html css hyperlink anchor pointer-events


【解决方案1】:

为了实现所需的行为,您可以将整体放置在具有固定widthheightrelative 容器中,如图所示。并将hover效果应用于它而不是#listlink

.container {
  position: relative;
  height: 100px;
  width: 100px;
  background: rgba(255, 0, 0, 0.4);
}
.container:hover {
  background: rgba(255, 0, 0, 0.8);
}
#listlink {
 /* height: 100px;
  width: 100px;*/
  top: 0;
  bottom:0;
  left: 0;
  right:0;
  position: absolute;
  /*background: rgba(255, 0, 0, 0.4);*/
}
/*
#listlink:hover {
  background: rgba(255, 0, 0, 0.8);
}*/

#listitem {
  /* height: 100px;
  width: 100px;*/
  top: 0;
  bottom:0;
  left: 0;
  right:0;
  position: absolute;
  z-index: 10;
  pointer-events: none;
}

#childlink {
  background: rgba(0, 0, 255, 0.4);
  pointer-events: auto;
}

#childlink:hover {
  background: rgba(0, 0, 255, 0.8);
}
<div class='container'>
  <a id="listlink" href="#listlink"></a>
  <div id="listitem">
    Text <a id="childlink" href="#childlink">top link</a> text
  </div>
</div>

【讨论】:

  • 是的,这行得通,我什至已经有了一个容器,所以我只是将悬停从背景链接移到容器上,它工作得很好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-03
  • 2021-02-04
  • 1970-01-01
相关资源
最近更新 更多