惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
Hugging Face - Blog
Hugging Face - Blog
V
Visual Studio Blog
博客园 - Franky
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
博客园 - 叶小钗
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Application and Cybersecurity Blog
Application and Cybersecurity Blog
TaoSecurity Blog
TaoSecurity Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
J
Java Code Geeks
爱范儿
爱范儿
宝玉的分享
宝玉的分享
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
量子位
N
News and Events Feed by Topic
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Recent Commits to openclaw:main
Recent Commits to openclaw:main
SecWiki News
SecWiki News
MyScale Blog
MyScale Blog
AI
AI
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 【当耐特】
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
有赞技术团队
有赞技术团队
W
WeLiveSecurity
Project Zero
Project Zero
T
Tor Project blog
Help Net Security
Help Net Security
L
LINUX DO - 最新话题
IT之家
IT之家
The Hacker News
The Hacker News
腾讯CDC
Schneier on Security
Schneier on Security
N
News and Events Feed by Topic
C
Cisco Blogs
博客园 - 聂微东
Webroot Blog
Webroot Blog
Forbes - Security
Forbes - Security
M
MIT News - Artificial intelligence
C
Cyber Attacks, Cyber Crime and Cyber Security
雷峰网
雷峰网
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
A
About on SuperTechFans

博客园 - 孤城浪子

[转].Net 中的许可证机制 [转]获取窗口 高 、宽 的JS代码 PHP连接mysql数据库 - 孤城浪子 - 博客园 PHP文件上传 动态调用Web Service - 孤城浪子 - 博客园 [转]ASP.NET 2.0 下加密解密算法的封装 [转]fckeidtor配置 [转]加解密技术 - 孤城浪子 - 博客园 [转]服务器推技术 [JavaScript]简单跟随鼠标移动的文字 排序算法 [仿照CloudGamer]写的颜色渐变 Asp.Net用SmtpClient发送邮件 项目中遇到的Vss和Sql问题 [JavaScript]拖动对象 - 孤城浪子 - 博客园 [JavaScript]飘浮文字 C#文件打散合并 C#调用Excel的宏 [网上整理]C#合并Excel
[原]一步一步自己制作弹出框
孤城浪子 · 2010-08-10 · via 博客园 - 孤城浪子

说到javascript弹出框的制作,将其实现步骤分开,其实很容易。
下面,将拆分页面弹出框的制作步骤。
首先,准备编辑工具(废话),其实,您可以使用notepad..嘿嘿。
弹出框的组成结构:
  1.一个页面遮罩层。
  2.一个div容器,用来包含整个弹出框的布局。
  3.弹出框的菜单头(在弹出框的顶部,一般用来放置关闭按钮等)
  4.弹出页面的iframe,将其src设置为需要链接的url.

弹出层组成结构讲解完毕,开始实现弹出层对象。
首先,先实现常见的公共函数

代码

//获取页面指定ID的对象
var $$=function(id)
{
return typeof id=="string"?document.getElementById(id):id;
}
//获取页面指定tagName的对象
var $E=function(tagName)
{
return document.getElementsByTagName(tagName)[0];
}
//创建对象 通过原型的继承
function New(aClass,aParams)
{
function new_()
{
aClass.initialize.apply(
this,aParams);
}
new_.prototype
=aClass;
return new new_();
}

好了  公共函数实现完毕,进入最关键的对象的创建了。
还是一步步来吧,首先,实现创建遮罩层的函数

代码

var makeMask=function()
{
var mask=$$("overlay_div");
if(mask==null)
{
mask
=document.createElement("div");//遮罩层div
mask.setAttribute("id","overlay_div");//设置id为overlay_div
mask.className="light_overlay";
//设置高度和宽度
mask.style.height=window.screen.availHeight>document.body.scrollHeight?

window.screen.availHeight:document.body.scrollHeight;
mask.style.width

=window.screen.availWidth>document.body.scrollWidth?

window.screen.availWidth:document.body.scrollWidth;
$E(

"body").appendChild(mask);
}
return mask;
}

遮罩层创建完毕
就开始创建一个div容器 并将设置容器的内部html代码

代码

var makeContent=function(_width,_height,_url)
{
var container=$$("container_div");
if(container==null)
{
container
=document.createElement("div");
container.setAttribute(
"id","container_div");
container.style.height
=_height;
container.style.width
=_width;
var v_left=document.body.clientWidth/2-_width/2;
var v_top=document.body.scrollTop+document.body.clientHeight/2-_height/2;
//设置容器距离顶部和左边的宽度。
container.style.top=v_top;
container.style.left
=v_left;
container.className
="light_container";
//在容器内部添加一个菜单头和一个指向给定url的iframe
container.innerHTML="<div class='light_header'><a class='light_close'

onclick='closeDialog()'>关闭</a></div><iframe frameborder='0' scrolling='no'

style='border:0;width:100%;height:98%;' src='

"+_url+"'></iframe>"
$E(
"body").appendChild(container);
}
return container;
}

给出关闭弹出层的函数,就是将建立的两个层隐藏就可以了。

function closeDialog(result)
{
$$(
"container_div").style.display="none";
$$(
"overlay_div").style.display="none";
}

最后一步 就是显示弹出层对象了。我们需要将前面两函数联合起来

代码

var displayLayer=function(width,height,url,display)
{
this._mask=makeMask();
this._container=makeContent(width,height,url);
this._mask.style.display=display==""?"none":display;
this._container.style.display=display==""?"none":display;
}

好了 基本的框架已经搭建出来了 现在给出css样式代码

代码

.light_overlay{
display
:none;
position
:absolute;
top
:0;
left
:0;
width
:100%;
height
:100%;
z-index
:1000;
background-color
:#333;
-moz-opacity
: 0.8;
opacity
:.80;
filter
: alpha(opacity=80);
}
.light_container
{
position
:absolute;
display
:none;
z-index
:1001;
border
: 1px solid #B8B8B8;
background-color
: white;
}
.light_header
{
background
:#D3F9F0;
text-align
:right;
}
.light_close
{
height
:28px;
cursor
:pointer;
font-size
:12px;
line-height
:28px;
margin-right
:4px;
}

然后我们直接调用
displayLayer(400,300,'show.html',"block");函数就可以显示弹出层了。

其实,我们可以将弹出层看成一个对象,达到javascrpt对象的封装,利于复用.
函数已经有了,我们只需要将函数整合就可以了,代码如下:

代码

var LightBox=
{
initialize:
function(url,width,height)
{
this._url=url;
this._width=width;
this._height=height;
this._mask=this.makeMask();
this._container=this.makeContent();
},
makeMask:
function()
{
var mask=$$("overlay_div");
if(mask==null)
{
mask
=document.createElement("div");
mask.setAttribute(
"id","overlay_div");
mask.className
="light_overlay";
mask.style.height
=window.screen.availHeight>document.body.scrollHeight?

window.screen.availHeight:document.body.scrollHeight;
mask.style.width

=window.screen.availWidth>document.body.scrollWidth?

window.screen.availWidth:document.body.scrollWidth;
$E(

"body").appendChild(mask);
}
return mask;
},
makeContent:
function()
{
var container=$$("container_div");
if(container==null)
{
container
=document.createElement("div");
container.setAttribute(
"id","container_div");
container.style.height
=this._height;
container.style.width
=this._width;
var v_left=document.body.clientWidth/2-this._width/2;
var v_top=document.body.scrollTop+document.body.clientHeight/2-this._height/2;
container.style.top
=v_top;
container.style.left
=v_left;
container.className
="light_container";
container.innerHTML
="<div class='light_header'><a class='light_close'

onclick='closeDialog()'>关闭</a></div><iframe frameborder='0' scrolling='no'

style='border:0;width:100%;height:98%;' src='

"+this._url+"'></iframe>"
$E(
"body").appendChild(container);
}
return container;
},
displayLayer:
function(display)
{
this._mask.style.display=display==""?"none":display;
this._container.style.display=display==""?"none":display;
}
}

当我们需要使用弹出框的时候就可以直接New一个对象
如下:

    //创建对象
    var lightbox_example=New(LightBox,[url,width,height]);

    //显示弹出层
    lightbox_example.displayLayer("block");

您可以点击此处 下载代码