Javascript Window.open 作为模态弹出窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3937591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Window.open as modal popup?
提问by James123
I want open window.open
as modal popup.
我想window.open
作为模态弹出窗口打开。
var features = 'resizable= yes; status= no; scroll= no; help= no; center= yes;
width=460;height=140;menubar=no;directories=no;location=no;modal=yes';
window.open(href, 'name', features, false);
I can use Window.ShowModelDialog()
, but in my child window I am calling parent javascript method. That is not happening with ShowModelDialog().
我可以使用Window.ShowModelDialog()
,但在我的子窗口中,我正在调用父 javascript 方法。ShowModelDialog() 不会发生这种情况。
function CallParentScript(weburl) {
alert(weburl);
if (weburl != null) {
var url = weburl;
window.opener.SelectUserImageCallback(url);
window.close();
return false;
}
}
If I use window.open()
. I can call Parent javascript. But window is not modal.
如果我使用window.open()
. 我可以调用 Parent javascript。但是窗口不是模态的。
How to solve this? Can I write something in child popup to always top?
如何解决这个问题?我可以在子弹出窗口中写一些东西总是在顶部吗?
回答by Diodeus - James MacFarlane
A pop-up is a child of the parent window, but it is not a child of the parent DOCUMENT. It is its own independent browser window and is not contained by the parent.
弹出窗口是父窗口的子窗口,但不是父文档的子窗口。它是自己独立的浏览器窗口,不包含在父窗口中。
Use an absolutely-positioned DIV and a translucent overlay instead.
改用绝对定位的 DIV 和半透明覆盖。
EDIT - example
编辑 - 示例
You need jQuery for this:
为此,您需要 jQuery:
<style>
html, body {
height:100%
}
#overlay {
position:absolute;
z-index:10;
width:100%;
height:100%;
top:0;
left:0;
background-color:#f00;
filter:alpha(opacity=10);
-moz-opacity:0.1;
opacity:0.1;
cursor:pointer;
}
.dialog {
position:absolute;
border:2px solid #3366CC;
width:250px;
height:120px;
background-color:#ffffff;
z-index:12;
}
</style>
<script type="text/javascript">
$(document).ready(function() { init() })
function init() {
$('#overlay').click(function() { closeDialog(); })
}
function openDialog(element) {
//this is the general dialog handler.
//pass the element name and this will copy
//the contents of the element to the dialog box
$('#overlay').css('height', $(document.body).height() + 'px')
$('#overlay').show()
$('#dialog').html($(element).html())
centerMe('#dialog')
$('#dialog').show();
}
function closeDialog() {
$('#overlay').hide();
$('#dialog').hide().html('');
}
function centerMe(element) {
//pass element name to be centered on screen
var pWidth = $(window).width();
var pTop = $(window).scrollTop()
var eWidth = $(element).width()
var height = $(element).height()
$(element).css('top', '130px')
//$(element).css('top',pTop+100+'px')
$(element).css('left', parseInt((pWidth / 2) - (eWidth / 2)) + 'px')
}
</script>
<a href="javascript:;//close me" onclick="openDialog($('#content'))">show dialog A</a>
<a href="javascript:;//close me" onclick="openDialog($('#contentB'))">show dialog B</a>
<div id="dialog" class="dialog" style="display:none"></div>
<div id="overlay" style="display:none"></div>
<div id="content" style="display:none">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin nisl felis, placerat in sollicitudin quis, hendrerit vitae diam. Nunc ornare iaculis urna.
</div>
<div id="contentB" style="display:none">
Moooo mooo moo moo moo!!!
</div>
回答by nacho
You can try open a modal dialog with html5 and css3, try this code:
你可以尝试用 html5 和 css3 打开一个模态对话框,试试这个代码:
.windowModal {
position: fixed;
font-family: Arial, Helvetica, sans-serif;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: rgba(0,0,0,0.8);
z-index: 99999;
opacity:0;
-webkit-transition: opacity 400ms ease-in;
-moz-transition: opacity 400ms ease-in;
transition: opacity 400ms ease-in;
pointer-events: none;
}
.windowModal:target {
opacity:1;
pointer-events: auto;
}
.windowModal > div {
width: 400px;
position: relative;
margin: 10% auto;
padding: 5px 20px 13px 20px;
border-radius: 10px;
background: #fff;
background: -moz-linear-gradient(#fff, #999);
background: -webkit-linear-gradient(#fff, #999);
background: -o-linear-gradient(#fff, #999);
}
.close {
background: #606061;
color: #FFFFFF;
line-height: 25px;
position: absolute;
right: -12px;
text-align: center;
top: -10px;
width: 24px;
text-decoration: none;
font-weight: bold;
-webkit-border-radius: 12px;
-moz-border-radius: 12px;
border-radius: 12px;
-moz-box-shadow: 1px 1px 3px #000;
-webkit-box-shadow: 1px 1px 3px #000;
box-shadow: 1px 1px 3px #000;
}
.close:hover { background: #00d9ff; }
<a href="#divModal">Open Modal Window</a>
<div id="divModal" class="windowModal">
<div>
<a href="#close" title="Close" class="close">X</a>
<h2>Modal Dialog</h2>
<p>This example shows a modal window without using javascript only using html5 and css3, I try it it?</p>
<p>Using javascript, with new versions of html5 and css3 is not necessary can do whatever we want without using js libraries.</p>
</div>
</div>
回答by Martin S Ek
That solution will open up a new browser window without the normal features such as address bar and similar.
该解决方案将打开一个新的浏览器窗口,而没有地址栏等常规功能。
To implement a modal popup, I suggest you to take a look at jQuery and SimpleModal, which is really slick.
要实现模态弹出窗口,我建议您查看 jQuery 和SimpleModal,它们真的很漂亮。
(Here are some simple demos using SimpleModal: http://www.ericmmartin.com/projects/simplemodal-demos/)
(这里有一些使用 SimpleModal 的简单演示:http://www.ericmmartin.com/projects/simplemodal-demos/ )
回答by Jonathan Nesbitt
I agree with both previous answers. Basically, you want to use what is known as a "lightbox" - http://en.wikipedia.org/wiki/Lightbox_(JavaScript)
我同意之前的两个答案。基本上,您想使用所谓的“灯箱” - http://en.wikipedia.org/wiki/Lightbox_(JavaScript)
It is essentially a div than is created within the DOM of your current window/tab. In addition to the div that contains your dialog, a transparent overlay blocks the user from engaging all underlying elements. This can effectively create a modal dialog (i.e. user MUST make some kind of decision before moving on).
它本质上是一个 div,而不是在当前窗口/选项卡的 DOM 中创建的。除了包含对话框的 div 之外,透明的覆盖层会阻止用户参与所有底层元素。这可以有效地创建一个模态对话框(即用户必须在继续之前做出某种决定)。