javascript模态窗口Demo
不多介绍了,应该见很多了,见过很多网站用的是Jquery的插件,个人觉得不够灵活
下面这个Demo支持回调,可以直接引用modalDialog.js使用,不存在任何Jquery的影子
global.js

window.js = new myJs(); //为了避免名称重复我们换个名称,附加一个myJs对像到window对象上,然后我们在页面中调用window.js
//js对象
function myJs() {
this.x = 10;
}
//下面我们对myJs进行扩展
myJs.prototype.alert = function (msg) { alert(msg); } //一个alert方法测试调用js.alert('弹出提示');
//获取制定Id的dom对象
myJs.prototype.$ = function (id) { return document.getElementById(id); }
myJs.prototype.bodyWidth = document.documentElement.clientWidth;
myJs.prototype.bodyHeight = document.documentElement.clientHeight;
myJs.prototype.body = document.body;
//js对象
function myJs() {
this.x = 10;
}
//下面我们对myJs进行扩展
myJs.prototype.alert = function (msg) { alert(msg); } //一个alert方法测试调用js.alert('弹出提示');
//获取制定Id的dom对象
myJs.prototype.$ = function (id) { return document.getElementById(id); }
myJs.prototype.bodyWidth = document.documentElement.clientWidth;
myJs.prototype.bodyHeight = document.documentElement.clientHeight;
myJs.prototype.body = document.body;
modalDialog.js 文件代码如下:

//Modaldialog
function modalDialog() {
this.uri ="about:blank"; //地址
this.title = null; //标题
this.width = 400; //默认宽
this.height = 300; //默认高
this.borderColor = "black"; //边框颜色
this.borderWidth = 2; //边框宽度
this.callback = null; //回调方法
this.background = "black";
this.titleBackground = "silver";
}
modalDialog.prototype.url = this.uri; //这样不用扩展也是可以的但是在页面中只能提示找不到这个属性
modalDialog.prototype.title = this.title;
modalDialog.prototype.width = this.width;
modalDialog.prototype.height = this.height;
modalDialog.prototype.background = this.background;
modalDialog.prototype.borderWidth = this.borderWidth;
modalDialog.prototype.borderColor = this.borderColor;
modalDialog.prototype.titleBackground = this.titleBackground;
modalDialog.prototype.callback = this.callback;
//触发回调方法
modalDialog.prototype.call = function (callback) { if (callback != null) callback(this); if (this.callback != null) this.callback(); }
//显示
modalDialog.prototype.show = function () {
var js = window.js;
//在里面实现显示的细节
var x = js.bodyWidth, y = js.bodyHeight;
//先创建一个层遮罩整个body
var zdiv = "zdiv"; //遮罩层id
document.body.innerHTML += "<div id='" + zdiv + "' style='width:" + x + "px;height:" + y + "px;background-color:" +
this.background + ";position:absolute;top:0;left:0;" +
"filter:alpha(opacity=80);opacity:0.8;z-index:'></div>";
var mdiv = "mdiv"; //模态窗口层id
document.body.innerHTML += "<div id='" + mdiv + "' style='width:" + this.width + "px;height:" + this.height + "px;" +
"border:solid " + this.borderWidth + "px " + this.borderColor + ";z-index:20;position:absolute;top:" +
(y - this.height) / 2 + ";left:" + (x - this.width) / 2 + ";'>" +
//加上标题
(this.title != null ? "<div style='background:" + this.titleBackground + ";line-height:30px;padding:0 10px;width:100%'>" + this.title + "</div>" : "") +
"<div style='padding:1px;'><iframe src='" + this.uri + "' frameborder='0' scrolling='no' style='width:" + (this.width) + "px;height:" +
(this.title != null ? this.height - 30 : this.height) + "px;'></iframe></div></div>";
}
modalDialog.prototype.close = function () {
document.body.removeChild(window.js.$("mdiv"));
document.body.removeChild(window.js.$("zdiv"));
}
function modalDialog() {
this.uri ="about:blank"; //地址
this.title = null; //标题
this.width = 400; //默认宽
this.height = 300; //默认高
this.borderColor = "black"; //边框颜色
this.borderWidth = 2; //边框宽度
this.callback = null; //回调方法
this.background = "black";
this.titleBackground = "silver";
}
modalDialog.prototype.url = this.uri; //这样不用扩展也是可以的但是在页面中只能提示找不到这个属性
modalDialog.prototype.title = this.title;
modalDialog.prototype.width = this.width;
modalDialog.prototype.height = this.height;
modalDialog.prototype.background = this.background;
modalDialog.prototype.borderWidth = this.borderWidth;
modalDialog.prototype.borderColor = this.borderColor;
modalDialog.prototype.titleBackground = this.titleBackground;
modalDialog.prototype.callback = this.callback;
//触发回调方法
modalDialog.prototype.call = function (callback) { if (callback != null) callback(this); if (this.callback != null) this.callback(); }
//显示
modalDialog.prototype.show = function () {
var js = window.js;
//在里面实现显示的细节
var x = js.bodyWidth, y = js.bodyHeight;
//先创建一个层遮罩整个body
var zdiv = "zdiv"; //遮罩层id
document.body.innerHTML += "<div id='" + zdiv + "' style='width:" + x + "px;height:" + y + "px;background-color:" +
this.background + ";position:absolute;top:0;left:0;" +
"filter:alpha(opacity=80);opacity:0.8;z-index:'></div>";
var mdiv = "mdiv"; //模态窗口层id
document.body.innerHTML += "<div id='" + mdiv + "' style='width:" + this.width + "px;height:" + this.height + "px;" +
"border:solid " + this.borderWidth + "px " + this.borderColor + ";z-index:20;position:absolute;top:" +
(y - this.height) / 2 + ";left:" + (x - this.width) / 2 + ";'>" +
//加上标题
(this.title != null ? "<div style='background:" + this.titleBackground + ";line-height:30px;padding:0 10px;width:100%'>" + this.title + "</div>" : "") +
"<div style='padding:1px;'><iframe src='" + this.uri + "' frameborder='0' scrolling='no' style='width:" + (this.width) + "px;height:" +
(this.title != null ? this.height - 30 : this.height) + "px;'></iframe></div></div>";
}
modalDialog.prototype.close = function () {
document.body.removeChild(window.js.$("mdiv"));
document.body.removeChild(window.js.$("zdiv"));
}
default.html 页面上创建modalDialog

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>模态窗口Demo</title>
<!--下面这个js文件为我们的公共js文件-->
<script type="text/javascript" src="global.js"></script>
<!--ModalDialog UI js文件-->
<script type="text/javascript" src="modaldialog.js"></script>
<script type="text/javascript">
var md; //用于页面回调
var uri = "/test.html";
function showModalDialog() {
//处理打开模态窗口
var m = new modalDialog();
m.uri = uri;
m.title = "模态窗口";
m.background = "white";
m.borderColor = "orange";
m.borderWidth = 2;
m.titleBackground = "gold";
m.callback = function () { m.close(); }
// m.call(); 这个回调方法在modalDialog的Uri中调用
m.show();
md = m;
}
</script>
</style>
</head>
<body>
<div>
用javascript+css实现ModalDialog<br />
Jquery框架里面有个插件也可以实现这种效果,不过我们说的是自己实现
<br />
<input id="btopenDialog" type="button" value="打点模态窗口!" onclick="showModalDialog()" />
</div>
</body>
</html>
<head>
<title>模态窗口Demo</title>
<!--下面这个js文件为我们的公共js文件-->
<script type="text/javascript" src="global.js"></script>
<!--ModalDialog UI js文件-->
<script type="text/javascript" src="modaldialog.js"></script>
<script type="text/javascript">
var md; //用于页面回调
var uri = "/test.html";
function showModalDialog() {
//处理打开模态窗口
var m = new modalDialog();
m.uri = uri;
m.title = "模态窗口";
m.background = "white";
m.borderColor = "orange";
m.borderWidth = 2;
m.titleBackground = "gold";
m.callback = function () { m.close(); }
// m.call(); 这个回调方法在modalDialog的Uri中调用
m.show();
md = m;
}
</script>
</style>
</head>
<body>
<div>
用javascript+css实现ModalDialog<br />
Jquery框架里面有个插件也可以实现这种效果,不过我们说的是自己实现
<br />
<input id="btopenDialog" type="button" value="打点模态窗口!" onclick="showModalDialog()" />
</div>
</body>
</html>
在modalDialog页面中使用window.parent.md.call()触发回调函数
原创内容请您保留出处及地址 , 主页:展益
标签:
Javascript
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述