【问题标题】:How can I add buttons over an absolute position iframe while still be able to interact with it?如何在绝对位置 iframe 上添加按钮,同时仍然能够与之交互?
【发布时间】:2024-01-10 01:14:01
【问题描述】:

我的标记完全按照这个顺序:

  <iframeContainer>
    <iframe>
  </iframeContainer>
  <container>
    <button1>
    <button2>
    <sidebar>
    ...
 </container>

container 应该位于 iframe 本身之上。不幸的是,由于我的container 具有 100% w/h 并且显然不允许点击 iframe 本身,因此我无法让它们玩得很好。示例:

const buttonThatIWannaClick = document.getElementById('buttonOverIframe');

buttonThatIWannaClick.addEventListener('click', function() {
  console.log('I am able to click the button!');
});
#videoContainer {
  position: absolute;
  z-index: 9998;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
}

#buttonOverIframe {
  position: absolute;
  color: white;
  top: 50%;
  left: 50%;
}

#videoContainer iframe {
  width: 100%;
  height: 100%;
}

#container {
  display: flex;
  position: absolute;
  z-index: 9998;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
}

#openSidebar {
  display: flex;
  position: absolute;
  z-index: 1;
  top: 50%;
  left: 30px;
  color: white;
  transform: translate3d(0, 50%, 0);
}
<div id="videoContainer">

  <div id="buttonOverIframe">Button Inside Iframe</div>
  <iframe width="560" height="315" src="https://www.youtube.com/embed/5qap5aO4i9A" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
<div id="container">
  <div id="openSidebar">Open Sidebar</div>
</div>

您可以看到我无法与 iframe 交互。 **我知道这是因为container 具有 100% 的宽度/高度,即使肉眼看不到它,浏览器也会看到它在前面。

我怎样才能重新安排这样的事情:

  1. 我可以与 iframe 上的按钮进行交互。
  2. 我可以检测到 iframe 外部/上的点击吗?

【问题讨论】:

    标签: html css iframe css-position z-index


    【解决方案1】:

    在容器中使用pointer-events: none;,在活动元素上使用pointer-events: initial

    const buttonThatIWannaClick = document.getElementById('buttonOverIframe');
    
    buttonThatIWannaClick.addEventListener('click', function() {
      alert('I am able to click the button!');
    });
    #videoContainer {
      position: absolute;
      z-index: 9998;
      top: 0;
      left: 0;
      width: 100vw;
      height: 100vh;
    }
    
    #buttonOverIframe {
      position: absolute;
      color: white;
      top: 50%;
      left: 50%;
    }
    
    #videoContainer iframe {
      width: 100%;
      height: 100%;
    }
    
    #container {
      display: flex;
      position: absolute;
      z-index: 9998;
      top: 0;
      left: 0;
      width: 100vw;
      height: 100vh;
      pointer-events: none;
    }
    
    #openSidebar {
      display: flex;
    pointer-events: initial;
      position: absolute;
      z-index: 1;
      top: 50%;
      left: 30px;
      color: white;
      transform: translate3d(0, 50%, 0);
    }
    <div id="videoContainer">
    
      <div id="buttonOverIframe">Button Over Iframe</div>
      <iframe width="560" height="315" src="https://www.youtube.com/embed/5qap5aO4i9A" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
    </div>
    <div id="container">
      <div id="openSidebar">Open Sidebar</div>
    </div>

    【讨论】:

    • 但是这是如何工作的呢?我觉得这好像是一个 hack,而不是应该如何做的事情。
    • 另外,抱歉,我用 "Button Inside Iframe" 编辑了原始 sn-p,也许这给人的印象是 iframe 的按钮需要覆盖其他所有内容。 iframe 应该位于其他所有内容的后面,但仍可交互。
    • pointer-events 定义元素是否对事件做出反应为click 等并允许您点击。因此,您将对容器禁用它,将其用于按钮。 w3schools.com/cssref/css3_pr_pointer-events.asp
    • 啊哈,所以基本上是对页面说“我不在乎容器的宽度/高度,你可以随心所欲,但对其中的点击事件做出反应,就好像没有任何容器”。
    • 是的。并通过将pointer-events: initial 分配给按钮,您就是在说“但不要忽视这些家伙”,否则他们会继承