【问题标题】:Click through an element, affecting what's behind it [duplicate]单击一个元素,影响其背后的内容[重复]
【发布时间】:2014-09-11 14:29:42
【问题描述】:

所以,我打算为我的网站制作一个覆盖物,其中一个元素固定在网站上并放置在所有内容之上。但这意味着元素后面的任何其他内容都不会受到影响。

这里有一个JSFiddle 重现了这个问题。

您会注意到无法突出显示黑色文本,并且 JavaScript 事件不会触发。


这是我用来创建此问题的 CSS + HTML:

HTML

<div id="main-container">
    <div id="container-content">You can't highlight me! D:</div>
    <div id="container-fixed"><div style="margin:90px auto;background:red;width:40px;height:40px;"></div>You cannot highlight the text behind this DIV.  It also doesn't fire click events.</div>
</div>

main-container:这里保存着内容。

container-content:这是固定DIV后面的内容

container-fixed:这是重叠“container-content”的DIV

CSS

#main-container {
    width: 90%;
    margin: 5%;
    position: relative;
    height: 500px;
    overflow: auto;
}

#container-content {
    width: 100%;
    height: auto;
}

#container-fixed {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    color: red;
}

我正在努力实现的目标是否可能?允许使用 CSS 和 JavaScript (jQuery) 答案。

【问题讨论】:

  • 你到底想达到什么目的?
  • 其实我可以在 jsfiddle 中同时选择红色和黑色文本(在 Chrome 36 上)
  • 我在 Linux 上使用 Chromium 34。您可以选择,但不能选择单个位。如果您三次单击它会选择所有文本。否则我无法选择文本。另外请记住,我也需要触发 JavaScript 事件。

标签: javascript jquery html css


【解决方案1】:

使用 CSS pointer-events:

除了指示该元素不是鼠标事件的目标之外,值 none 还指示鼠标事件“穿过”该元素并以该元素“下方”的任何内容为目标。

#container-content {
    width:100%;
    height:auto;
    pointer-events: none;
}

如果您需要 IE 10 及更低版本的支持,请联系polyfill

【讨论】:

  • 啊,为什么是 IE,为什么? D:
  • 感谢您的回答。
  • 正如我们所有的 Web 开发人员都知道的那样,IE 在很多事情上对游戏来说有点慢。从好的方面来说,under consideration for the next version 有不少前沿功能。
【解决方案2】:

这应该可以解决问题:

#container-fixed{
    pointer-events: none;
}

pointer-events: none; 禁用目标元素上的鼠标交互。
请记住,11 之前的 IE 版本不支持此 CSS 属性。

【讨论】:

    【解决方案3】:

    对于 IE10> 浏览器,您必须通过更高级别的元素计算要单击的元素的位置,然后执行您想做的事情。例如http://jsfiddle.net/hm6Js/7/

    topo=$('#container-content').offset().top;
    left=$('#container-content').offset().left;
    right=$('#container-content').offset().left+$('#container-content').width();
    bottom=$('#container-content').offset().top+$('#container-content').height();
    $(document).click(function(e){
        if(e.pageX>=left && e.pageX<=right && e.pageY>=topo && e.pageY<=bottom){
            alert("This doesn't work.");
        }
    });
    

    正如您在此示例中看到的,警报仅在您单击 #container-content 元素时触发。

    注意:

    如果要点击的元素很多,这不是最有效的方法。

    【讨论】:

      【解决方案4】:

      您可以使用 css 的指针事件来做到这一点。 将 #container-content 的 css 更改为此

      #container-content{
      width:100%;
      height:auto;
      }
      
      #container-fixed {
      width:100%;
      height:100%;
      position:absolute;
      top:0; left:0;
      color:red;
      
          pointer-events: none /*the important thing*/
      }
      

      工作小提琴here

      可以找到指向指针事件更多信息的链接here 这可能不是跨浏览器,所以另一个家伙建议如果这是一个问题)使用 javascript,通过层转发鼠标事件。完整答案可以找到here

      【讨论】:

        【解决方案5】:

        你可以通过添加z-index来做到这一点

        #container-content {
         width:100%;
         height:auto;
          z-index:999;
          position:absolute;
        }
        

        【讨论】:

        • 只设置z-index 不允许点击。
        • 用小提琴试了一下,效果很好
        • 在小提琴中它可以工作,但正如问题#container-fixed 中所描述的那样,它是一个覆盖。在#container-content 上设置z-index 会将其置于#container-fixed 之上,因此它不再是叠加层了。
        猜你喜欢
        • 1970-01-01
        • 2016-06-17
        • 1970-01-01
        • 2021-06-30
        • 2013-03-22
        • 2019-06-01
        • 1970-01-01
        • 2014-05-24
        • 2018-07-30
        相关资源
        最近更新 更多