javascript Demo模態(tài)窗口
更新時(shí)間:2009年12月06日 00:47:20 作者:
不多介紹了,應(yīng)該見(jiàn)很多了,見(jiàn)過(guò)很多網(wǎng)站用的是Jquery的插件,個(gè)人覺(jué)得不夠靈活。
下面這個(gè)Demo支持回調(diào),可以直接引用modalDialog.js使用,不存在任何Jquery的影子
global.js
window.js = new myJs(); //為了避免名稱(chēng)重復(fù)我們換個(gè)名稱(chēng),附加一個(gè)myJs對(duì)像到window對(duì)象上,然后我們?cè)陧?yè)面中調(diào)用window.js
//js對(duì)象
function myJs() {
this.x = 10;
}
//下面我們對(duì)myJs進(jìn)行擴(kuò)展
myJs.prototype.alert = function (msg) { alert(msg); } //一個(gè)alert方法測(cè)試調(diào)用js.alert('彈出提示');
//獲取制定Id的dom對(duì)象
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; //標(biāo)題
this.width = 400; //默認(rèn)寬
this.height = 300; //默認(rèn)高
this.borderColor = "black"; //邊框顏色
this.borderWidth = 2; //邊框?qū)挾?
this.callback = null; //回調(diào)方法
this.background = "black";
this.titleBackground = "silver";
}
modalDialog.prototype.url = this.uri; //這樣不用擴(kuò)展也是可以的但是在頁(yè)面中只能提示找不到這個(gè)屬性
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;
//觸發(fā)回調(diào)方法
modalDialog.prototype.call = function (callback) { if (callback != null) callback(this); if (this.callback != null) this.callback(); }
//顯示
modalDialog.prototype.show = function () {
var js = window.js;
//在里面實(shí)現(xiàn)顯示的細(xì)節(jié)
var x = js.bodyWidth, y = js.bodyHeight;
//先創(chuàng)建一個(gè)層遮罩整個(gè)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"; //模態(tài)窗口層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 + ";'>" +
//加上標(biāo)題
(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 頁(yè)面上創(chuàng)建modalDialog
代碼
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>模態(tài)窗口Demo</title>
<!--下面這個(gè)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; //用于頁(yè)面回調(diào)
var uri = "/test.html";
function showModalDialog() {
//處理打開(kāi)模態(tài)窗口
var m = new modalDialog();
m.uri = uri;
m.title = "模態(tài)窗口";
m.background = "white";
m.borderColor = "orange";
m.borderWidth = 2;
m.titleBackground = "gold";
m.callback = function () { m.close(); }
// m.call(); 這個(gè)回調(diào)方法在modalDialog的Uri中調(diào)用
m.show();
md = m;
}
</script>
</style>
</head>
<body>
<div>
用javascript+css實(shí)現(xiàn)ModalDialog<br />
Jquery框架里面有個(gè)插件也可以實(shí)現(xiàn)這種效果,不過(guò)我們說(shuō)的是自己實(shí)現(xiàn)
<br />
<input id="btopenDialog" type="button" value="打點(diǎn)模態(tài)窗口!" onclick="showModalDialog()" />
</div>
</body>
</html>
在modalDialog頁(yè)面中使用window.parent.md.call()觸發(fā)回調(diào)函數(shù)
文件打包腳本之家下載
global.js
復(fù)制代碼 代碼如下:
window.js = new myJs(); //為了避免名稱(chēng)重復(fù)我們換個(gè)名稱(chēng),附加一個(gè)myJs對(duì)像到window對(duì)象上,然后我們?cè)陧?yè)面中調(diào)用window.js
//js對(duì)象
function myJs() {
this.x = 10;
}
//下面我們對(duì)myJs進(jìn)行擴(kuò)展
myJs.prototype.alert = function (msg) { alert(msg); } //一個(gè)alert方法測(cè)試調(diào)用js.alert('彈出提示');
//獲取制定Id的dom對(duì)象
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 文件代碼如下:
代碼
復(fù)制代碼 代碼如下:
//Modaldialog
function modalDialog() {
this.uri ="about:blank"; //地址
this.title = null; //標(biāo)題
this.width = 400; //默認(rèn)寬
this.height = 300; //默認(rèn)高
this.borderColor = "black"; //邊框顏色
this.borderWidth = 2; //邊框?qū)挾?
this.callback = null; //回調(diào)方法
this.background = "black";
this.titleBackground = "silver";
}
modalDialog.prototype.url = this.uri; //這樣不用擴(kuò)展也是可以的但是在頁(yè)面中只能提示找不到這個(gè)屬性
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;
//觸發(fā)回調(diào)方法
modalDialog.prototype.call = function (callback) { if (callback != null) callback(this); if (this.callback != null) this.callback(); }
//顯示
modalDialog.prototype.show = function () {
var js = window.js;
//在里面實(shí)現(xiàn)顯示的細(xì)節(jié)
var x = js.bodyWidth, y = js.bodyHeight;
//先創(chuàng)建一個(gè)層遮罩整個(gè)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"; //模態(tài)窗口層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 + ";'>" +
//加上標(biāo)題
(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 頁(yè)面上創(chuàng)建modalDialog
代碼
復(fù)制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>模態(tài)窗口Demo</title>
<!--下面這個(gè)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; //用于頁(yè)面回調(diào)
var uri = "/test.html";
function showModalDialog() {
//處理打開(kāi)模態(tài)窗口
var m = new modalDialog();
m.uri = uri;
m.title = "模態(tài)窗口";
m.background = "white";
m.borderColor = "orange";
m.borderWidth = 2;
m.titleBackground = "gold";
m.callback = function () { m.close(); }
// m.call(); 這個(gè)回調(diào)方法在modalDialog的Uri中調(diào)用
m.show();
md = m;
}
</script>
</style>
</head>
<body>
<div>
用javascript+css實(shí)現(xiàn)ModalDialog<br />
Jquery框架里面有個(gè)插件也可以實(shí)現(xiàn)這種效果,不過(guò)我們說(shuō)的是自己實(shí)現(xiàn)
<br />
<input id="btopenDialog" type="button" value="打點(diǎn)模態(tài)窗口!" onclick="showModalDialog()" />
</div>
</body>
</html>
在modalDialog頁(yè)面中使用window.parent.md.call()觸發(fā)回調(diào)函數(shù)
文件打包腳本之家下載
您可能感興趣的文章:
- JS模態(tài)窗口返回值兼容問(wèn)題的完美解決方法
- js操作模態(tài)窗口及父子窗口間相互傳值示例
- javascript 獲取模態(tài)窗口的滾動(dòng)位置代碼
- js關(guān)閉模態(tài)窗口刷新父頁(yè)面或跳轉(zhuǎn)頁(yè)面
- JQuery Dialog(JS 模態(tài)窗口,可拖拽的DIV)
- javascript showModalDialog 多層模態(tài)窗口實(shí)現(xiàn)頁(yè)面提交及刷新的代碼
- js實(shí)現(xiàn)簡(jiǎn)單模態(tài)窗口,背景灰顯
- js實(shí)現(xiàn)模態(tài)窗口增加與刪除
相關(guān)文章
JavaScript簡(jiǎn)單驗(yàn)證表單空值及郵箱格式的方法
這篇文章主要介紹了JavaScript簡(jiǎn)單驗(yàn)證表單空值及郵箱格式的方法,涉及javascript基本的表單與字符串操作相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01JavaScript代碼輕松實(shí)現(xiàn)網(wǎng)頁(yè)內(nèi)容禁止復(fù)制(代碼簡(jiǎn)單)
有些時(shí)候我們寫(xiě)的內(nèi)容不想被別人復(fù)制,在代碼中怎么實(shí)現(xiàn)的呢?下面小編給大家介紹javascript代碼輕松實(shí)現(xiàn)網(wǎng)頁(yè)內(nèi)容禁止復(fù)制,感興趣的童鞋一起看看吧2015-10-10利用Echarts如何實(shí)現(xiàn)多段圓環(huán)圖
這篇文章主要給大家介紹了關(guān)于利用Echarts如何實(shí)現(xiàn)多段圓環(huán)圖的相關(guān)資料,文中通過(guò)實(shí)例代碼代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03javascript 響應(yīng)鍵盤(pán)特定按鍵(只響應(yīng)數(shù)字鍵)
響應(yīng)鍵盤(pán)特定按鍵(只響應(yīng)數(shù)字鍵),大家可以看看思路。2009-03-03JavaScript必知必會(huì)(九)function 說(shuō)起 閉包問(wèn)題
這篇文章主要介紹了JavaScript必知必會(huì)(九)function 說(shuō)起 閉包問(wèn)題的相關(guān)資料,需要的朋友可以參考下2016-06-06