在模式弹出窗口中打开外部网站

新手上路,请多包涵

我知道 <a href="http://www.example.com" target="_blank">Click here</a> 在新选项卡中 打开链接(Chrome 和 Firefox 中的默认行为)并且

<a href="http://www.example.com" onclick="window.open('http://www.example.com',
        'newwindow', 'width=700,height=500'); return false;">Click here</a>

在新窗口中 打开它。

但是 如何在模式弹出窗口中打开外部页面(以屏幕为中心,原始页面的其余部分“变暗”)?

有什么好的方法吗?

我尝试了以下代码,但它似乎不起作用(单击:弹出窗口打开,重新单击,它关闭,重新单击链接:它不再打开。iframe 也没有加载)。

 document.getElementById("a").onclick = function(e) {
  e.preventDefault();
  document.getElementById("popup").style.display = "block";
  document.getElementById('iframe').src = "http://example.com";
  document.getElementById('page').className = "darken";
  setTimeout(function() {
    document.getElementById('page').onclick = function() {
      document.getElementById("popup").style.display = "none";
      document.getElementById('page').className = "";
    }
  }, 100);
  return false;
}
 #popup {
  display: none;
  border: 1px black solid;
  width: 400px; height: 200px;
  top:20px; left:20px;
  background-color: white;
  z-index: 10;
  padding: 2em;
  position: fixed;
}

.darken { background: rgba(0, 0, 0, 0.7); }

#iframe { border: 0; }

html, body, #page { height: 100%; }
 <div id="page">
  <a href="" id="a">Click me</a><br>
  Hello<br>
  Hello<br>

  <div id="popup">
  External website:
  <iframe id="iframe"></iframe>
  </div>

</div>

原文由 Basj 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 391
2 个回答

根本原因是 page.onclick is registered after popup window is open at the first time, then the following 'open popup' will trigger a.onclick and page.onclick in a row, that caused the popup window is open then is closed directly

简单的解决方案是添加一个参数来控制弹出窗口的状态。如果弹出窗口关闭,则在 page.onclick 中不执行任何操作。

要删除 setTimeout,有两种解决方案:

  1. 添加了另一个参数来控制弹出初始化的状态。

  2. Don’t make <div id="page"> is the parent of <a id="popup"> , so <a id="popup"> will not be triggered when clicked <div id="page"> .

以下是两种解决方案:

我更喜欢 Solution2 ,它比 Solution1 更好地解耦,每个组件都专注于自己的工作。

解决方案 1 :如果弹出窗口已完成初始化,则添加 isInit 作为指示符。

PS :在评论中,您提到只有在单击 <div id="page"> 时才关闭弹出窗口,要在解决方案 1 中实现这一点,我认为您必须绑定事件 = mouseoutmouseenter 判断鼠标点击的位置。

 document.getElementById("a").onclick = function(e) {
  e.preventDefault();
  var isInit = true; // indicates if the popup already been initialized.
  var isClosed = false; // indicates the state of the popup
  document.getElementById("popup").style.display = "block";
  document.getElementById('iframe').src = "http://example.com";
  document.getElementById('page').className = "darken";
  document.getElementById('page').onclick = function() {
    if(isInit){isInit=false;return;}
    if(isClosed){return;} //if the popup is closed, do nothing.
    document.getElementById("popup").style.display = "none";
    document.getElementById('page').className = "";
    isClosed=true;
  }
  return false;
}
 #popup {
  display: none;
  border: 1px black solid;
  width: 400px; height: 200px;
  top:20px; left:20px;
  background-color: white;
  z-index: 10;
  padding: 2em;
  position: fixed;
}

.darken { background: rgba(0, 0, 0, 0.7); }

#iframe { border: 0; }

html, body, #page { height: 100%; }
 <div id="page">
  <a href="" id="a">Click me</a><br>
  Hello<br>
  Hello<br>

  <div id="popup">
  External website:
  <iframe id="iframe"></iframe>
  </div>

</div>

解决方案 2 :使 <div id="page"><a id="popup"> 是堂兄而不是父母关系。

 document.getElementById("popup").showpopup = function() {
  document.getElementById("popup").style.display = "block";
  document.getElementById('iframe').src = "http://example.com";
  document.getElementById('page').className = "darken";
  document.getElementById("page").style.display = "block";
}

document.getElementById("a").onclick = function(e) {
  e.preventDefault();
  document.getElementById("popup").showpopup();
}

document.getElementById('page').onclick = function() {
  if(document.getElementById("popup").style.display == "block") {
    document.getElementById("popup").style.display = "none";
    document.getElementById("page").style.display = "none";
    document.getElementById('page').className = "";
  }
};
 #popup {
  display: none;
  border: 1px black solid;
  width: 400px; height: 200px;
  top:20px; left:20px;
  background-color: white;
  z-index: 10;
  padding: 2em;
  position: fixed;
}

#page {
  display: none;
  width: 100%; height: 100%;
  top:0px; left:0px;
  z-index: 9;
  padding: 2em;
  position: absolute;
}

.darken { background: rgba(0, 0, 0, 0.7); }

#iframe { border: 0; }

html, body, #page { height: 100%; }
 <a href="" id="a">Click me</a><br>
  Hello<br>
  Hello<br>

<div id="page">
</div>
<div id="popup">
  External website:
  <iframe id="iframe"></iframe>
</div>

原文由 Sphinx 发布,翻译遵循 CC BY-SA 3.0 许可协议

基于 Sphinx 的出色回答,我将使用以下方法创建一个 模态弹出窗口,在 iframe 中显示外部网站,当弹出窗口打开时,页面的其余部分为深色背景:

 document.getElementById("link").onclick = function(e) {
  e.preventDefault();
  document.getElementById("popupdarkbg").style.display = "block";
  document.getElementById("popup").style.display = "block";
  document.getElementById('popupiframe').src = "http://example.com";
  document.getElementById('popupdarkbg').onclick = function() {
      document.getElementById("popup").style.display = "none";
      document.getElementById("popupdarkbg").style.display = "none";
  };
  return false;
}

window.onkeydown = function(e) {
    if (e.keyCode == 27) {
      document.getElementById("popup").style.display = "none";
      document.getElementById("popupdarkbg").style.display = "none";
      e.preventDefault();
      return;
    }
}
 #popup { display: none; position: fixed; top: 12%; left: 15%; width: 70%; height: 70%; background-color: white; z-index: 10; }
#popup iframe { width: 100%; height: 100%; border: 0; }
#popupdarkbg { position: fixed; z-index: 5; left: 0; top: 0; width: 100%; height: 100%; overflow: hidden; background-color: rgba(0,0,0,.75); display: none; }
 <div id="main">
<a href="" id="link">Click me</a><br>
</div>

<div id="popup"><iframe id="popupiframe"></iframe></div>
<div id="popupdarkbg"></div>

原文由 Basj 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题