javascript Demo模態視窗

來源:互聯網
上載者:User

下面這個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;

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"));
}

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>

在modalDialog頁面中使用window.parent.md.call()觸發回呼函數
檔案打包指令碼之家下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.