Div 作为模态 - javascript [英] Div as modal - javascript
问题描述
我需要一个 JavaScript 将隐藏的 div 显示在屏幕中央,作为带有深色背景的模态,如 jquery 对话框!
I need a JavaScript that shows hidden div on the center of the screen as modal with darked background like jquery dialog!
示例:
<div id='divToShow' style='display:none'>
Here is the content of the div that should be shown as modal on the center of the page!
</div>
谁能帮忙?谢谢
推荐答案
一个简单的模态弹出div或者对话框可以通过CSS属性和一点点jQuery来完成.基本思路很简单:
A simple modal pop up div or dialog box can be done by CSS properties and little bit of jQuery.The basic idea is simple:
所以我们需要三个 div:
So we need three divs:
首先让我们定义 CSS:
First let us define the CSS:
#hider
{
position:absolute;
top: 0%;
left: 0%;
width:1600px;
height:2000px;
margin-top: -800px; /*set to a negative number 1/2 of your height*/
margin-left: -500px; /*set to a negative number 1/2 of your width*/
/*
z- index must be lower than pop up box
*/
z-index: 99;
background-color:Black;
//for transparency
opacity:0.6;
}
#popup_box
{
position:absolute;
top: 50%;
left: 50%;
width:10em;
height:10em;
margin-top: -5em; /*set to a negative number 1/2 of your height*/
margin-left: -5em; /*set to a negative number 1/2 of your width*/
border: 1px solid #ccc;
border: 2px solid black;
z-index:100;
}
重要的是我们将隐藏 div 的 z-index 设置为低于 pop_up 框,因为我们希望在顶部显示 popup_box.
java脚本来了:
It is important that we set our hider div's z-index lower than pop_up box as we want to show popup_box on top.
Here comes the java Script:
$(document).ready(function () {
//hide hider and popup_box
$("#hider").hide();
$("#popup_box").hide();
//on click show the hider div and the message
$("#showpopup").click(function () {
$("#hider").fadeIn("slow");
$('#popup_box').fadeIn("slow");
});
//on click hide the message and the
$("#buttonClose").click(function () {
$("#hider").fadeOut("slow");
$('#popup_box').fadeOut("slow");
});
});
最后是 HTML:
<div id="hider"></div>
<div id="popup_box">
Message<br />
<a id="buttonClose">Close</a>
</div>
<div id="content">
Page's main content.<br />
<a id="showpopup">ClickMe</a>
</div>
我使用了 jquery-1.4.1.min.js www.jquery.com/download 并在 Firefox 中测试了代码.希望这会有所帮助.
I have used jquery-1.4.1.min.js www.jquery.com/download and tested the code in Firefox. Hope this helps.
这篇关于Div 作为模态 - javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!