简体   繁体   English

从iframe将div弹出窗口居中显示在屏幕上

[英]center a div popup on the screen from iframe

Have a iframe in my page, I need show a small div popup using javascript codes in iframe. 我的网页上有一个iframe,我需要在iframe中使用JavaScript代码显示一个小的div弹出窗口。 The domain of iframe and parent is NOT same. iframe和父级的域不同。 how to center this div popup in whole screen. 如何在整个屏幕上使此div弹出窗口居中。 I found another similar question, but no answer in it. 我发现了另一个类似的问题,但没有答案。

You can't edit that iframe, but you can make the div with a bigger z-index than of that iframe and that be aligned inside of the iframe. 您无法编辑该iframe,但是可以使div具有比该iframe更大的z索引,并且可以在iframe内对齐。

Steps: 脚步:

  1. Create a div, with an class test (in my steps). 创建一个div,并进行类test (在我的步骤中)。
  2. Change its left position to the iframe offsetLeft plus offsetWidth/2 (of iframe), and subtract with the offsetWidth/2 of the div, you do the same to the top (replacing offsetWidth per offsetHeight and offsetLeft to offsetTop ). 将其左侧位置更改为iframe的offsetLeft加上(iframe的) offsetWidth/2 ,然后减去div的offsetWidth/2 ,在顶部进行相同的操作(将offsetWidth offsetHeightoffsetLeft替换为offsetTop )。

It may be so: 可能是这样的:

div.style.left=(iframe.offsetLeft+(iframe.offsetWidth/2))-div.offsetWidth/2+'px',
div.style.top=(iframe.offsetTop+(iframe.offsetHeight/2))-div.offsetHeight/2+'px'

I recommend to update the top and left CSS properties of the div everytime that the user scroll the page also, else the div will not combine with screen. 我建议每次用户滚动页面时都要更新div的topleft CSS属性,否则div不会与屏幕结合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM