javascript面向对象的方式实现的弹出层效果代码


Posted in Javascript onJanuary 28, 2010

说到js的面向对象,就不得不提到prototype这个js内置属性了(注意:这里的prototype可不是prototype.js),它的作用就是可以动态的向一个对象(object)添加某种属性。我现在要做的就是尽可能的让代码达到公用,像继承啦之类的。好了,这些就不多说了,对prototype不了解的可以搜索下相关内容。

今天要做的是点击一个html元素让其弹出一个友好的对话框来,首先要明确两点,一点是我可能会大量的用到这种方式,甚至不希望出现系统的alert或confirm,第二点就是弹出的内容尽量的可以多种化,甚至可以自定义。明确这两点后,我们就可以写js代码了,都是些很初级的东西,如果你要鄙视的话就尽情的鄙视我吧!^.^

首先定义一个简单的对象:

function objDIV() { 
this.bgdiv ; 
this.infodiv ; 
}

首先,我们希望弹出一个遮罩层,我给它命名openBackDiv();
function openBackDiv(txbdiv) { 
txbdiv.bgdiv = document.createElement("div"); 
txbdiv.bgdiv.setAttribute("id", "overDiv"); 
txbdiv.bgdiv.innerHTML = "<iframe frameborder=\"no\" class=\"overPanel\" id=\"ifrover\"></iframe>"; }

再者,把它添加到刚刚定义的对象的prototype里去(openBG()):
objDIV.prototype.openBG = function() { 
openBackDiv(this); 
document.body.appendChild(this.bgdiv); 
this.bgdiv.style.display = "block"; 
this.bgdiv.style.width = document.documentElement.clientWidth + "px"; 
this.bgdiv.style.height = document.documentElement.scrollHeight + "px"; 
}

再就是添加弹出信息层的方法,和上面一样做就行了。所以才说这个是很基础的东西,好像确实没啥好说的,直接上代码吧!

这是一个正在加载的弹出层,有点粗糙.

function openLoadDiv(txbdiv) { 
txbdiv.infodiv = document.createElement("div"); 
txbdiv.infodiv.setAttribute("id", "div_info"); 
txbdiv.infodiv.innerHTML = "<div style=\" line-height:1.5;background:url(../images/tips-top-bg.gif) repeat-x; height:54px; text-align:center;\"><img border=\"0\" src=\"../images/xtts.gif\" /></div><div style='padding:20px; font-size:14px; color:#b44201;'><div style='width:100px; float:left;margin:60px 0 0 60px; height:80px;'><img src='/images/business/loading.gif' width='100px' height='100' border='0'/></div><div style='float:left; width:250px;margin:90px 0 0 20px;'><p>请稍等,正在处理中...</p></div></div></div>"; 
document.body.appendChild(txbdiv.infodiv); 
txbdiv.infodiv.style.width = "550px"; 
txbdiv.infodiv.style.height = "270px"; 
txbdiv.infodiv.style.fontSize = "14px"; 
txbdiv.infodiv.style.position = "absolute"; 
txbdiv.infodiv.style.background = "#fff"; 
txbdiv.infodiv.style.zIndex = "9999"; 
centerobject();//居中的方法 
} 
objDIV.prototype.openLoading = function() { this.openBG(); openLoadDiv(this); }

做完这些后一个简单的弹出加载层就完成了.是不是有点成就感了,那么接着完成其他的工作吧!既然都弹出了,总得在某个时刻把它们移掉吧,下面就是移除这些层的方法。
objDIV.prototype.removeBG = function() { 
if (this.bgdiv || document.getElementById("overDiv")) { 
if (this.bgdiv) { 
document.body.removeChild(this.bgdiv); 
} else { 
document.body.removeChild(document.getElementById("overDiv")); 
} 
} 
} 
objDIV.prototype.removeInfo = function() { 
this.removeBG(); 
if (this.infodiv) { 
document.body.removeChild(this.infodiv); 
} else { 
document.body.removeChild(document.getElementById("div_info")); 
} 
}

如果想弹出不同层信息的话,就可以添加不同的prototype属性。
完整的代码
[code]

//******js弹出层提示txb20100110********//
function objDIV() {
this.bgdiv ;
this.infodiv ;
}
objDIV.prototype.openBG = function() {
openBackDiv(this);
document.body.appendChild(this.bgdiv);
this.bgdiv.style.display = "block";
this.bgdiv.style.width = document.documentElement.clientWidth + "px";
this.bgdiv.style.height = document.documentElement.scrollHeight + "px";
}
objDIV.prototype.openRegInfo = function() {
this.openBG();
openDiv(this);
}
objDIV.prototype.openLoading = function() {
this.openBG();
openLoadDiv(this);
}
objDIV.prototype.openLoad = function() {
openLoadDiv(this);
}
objDIV.prototype.removeBG = function() {
if (this.bgdiv || document.getElementById("overDiv")) {
if (this.bgdiv) {
document.body.removeChild(this.bgdiv);
} else {
document.body.removeChild(document.getElementById("overDiv"));
}
}
}
objDIV.prototype.removeInfo = function() {
this.removeBG();
if (this.infodiv) {
document.body.removeChild(this.infodiv);
} else {
document.body.removeChild(document.getElementById("div_info"));
}
}

function openLoadDiv(txbdiv) {
txbdiv.infodiv = document.createElement("div");
txbdiv.infodiv.setAttribute("id", "div_info");
txbdiv.infodiv.innerHTML = "<div style=\" line-height:1.5;background:url(tips-top-bg.gif) repeat-x; height:54px; text-align:center;\"><img border=\"0\" src=\"xtts.gif\" /></div><div style='padding:20px; font-size:14px; color:#b44201;'><div style='width:100px; float:left;margin:60px 0 0 60px; height:80px;'><img src='loading.gif' width='100px' height='100' border='0'/></div><div style='float:left; width:250px;margin:90px 0 0 20px;'><p>请稍等,正在处理中...</p></div></div></div>";
document.body.appendChild(txbdiv.infodiv);
txbdiv.infodiv.style.width = "550px";
txbdiv.infodiv.style.height = "270px";
txbdiv.infodiv.style.fontSize = "14px";
txbdiv.infodiv.style.position = "absolute";
txbdiv.infodiv.style.background = "#fff";
txbdiv.infodiv.style.zIndex = "9999";

centerobject();
}

function openBackDiv(txbdiv) {
txbdiv.bgdiv = document.createElement("div");
txbdiv.bgdiv.setAttribute("id", "overDiv");
//alert(document.documentElement.clientWidth);
txbdiv.bgdiv.innerHTML = "<iframe frameborder=\"no\" class=\"overPanel\" id=\"ifrover\"></iframe>";
//"<div id=\"overPanel\" > <iframe frameborder=\"no\" class=\"overPanel\" id=\"ifrover\"></iframe></div>";
//txbdiv.openBG();
}
function openDiv(txbdiv) {
//txbdiv.openBG();
txbdiv.infodiv = document.createElement("div");
txbdiv.infodiv.setAttribute("id", "div_info");
txbdiv.infodiv.innerHTML = "<div style=\" line-height:1.5;background:url(tips-top-bg.gif) repeat-x; height:54px; text-align:center;\"><img border=\"0\" src=\"xtts.gif\" /></div><div style=\"padding:20px;\"><div style=\"width:120px; float:left;\"><img src=\"xin.gif\" /></div><div style=\"float:right; width:350px;color:#b44201;\" id=\"showdivinfo\"><p>恭喜您,注册成功!</p><p>请牢记您的账号:<font color=\"#b44201\" id=\"orpai_ID\">5678537</font></p></div><div style=\"margin:0 auto;\"><input type='button' value='确认' onclick='new objDIV().removeInfo();'/></div></div>";
document.body.appendChild(txbdiv.infodiv);
txbdiv.infodiv.style.width = "550px";
txbdiv.infodiv.style.height = "270px";
txbdiv.infodiv.style.fontSize = "14px";
txbdiv.infodiv.style.position = "absolute";
txbdiv.infodiv.style.background = "#fff";
txbdiv.infodiv.style.zIndex = "9999";

centerobject();
}

function centerobject() {
if (document.getElementById("overDiv")) {
var objdiv = document.getElementById("overDiv").style;
objdiv.height = document.documentElement.scrollHeight + "px";
objdiv.left = parseInt((document.documentElement.clientWidth - parseInt(objdiv.width)) / 2) + "px";
//alert(document.documentElement.scrollHeight)
objdiv.top = parseInt((document.documentElement.clientHeight - parseInt(objdiv.height)) / 2) + "px";
}
if (document.getElementById("div_info")) {
var div_info = document.getElementById("div_info").style;
div_info.left = parseInt((document.documentElement.clientWidth - parseInt(div_info.width)) / 2) + "px";
div_info.top = parseInt((document.documentElement.clientHeight - parseInt(div_info.height)) / 2) + "px";
}
}

function centerDIV(objId) {
if (document.getElementById(objId)) {
var objdiv = document.getElementById(objId).style;
objdiv.height = document.getElementById(objId).scrollHeight + "px";
objdiv.width = document.getElementById(objId).scrollWidth + "px";
objdiv.left = parseInt((document.documentElement.clientWidth - parseInt(objdiv.width)) / 2) + "px";
//alert(document.documentElement.scrollHeight)
objdiv.top = parseInt((document.documentElement.clientHeight - parseInt(objdiv.height))/ 2) + "px";

}
}

function centerObj(obj) {
if (obj) {
var objdiv = obj.style;
objdiv.height = obj.scrollHeight + "px";
objdiv.width = obj.scrollWidth + "px";
objdiv.left = parseInt((document.documentElement.clientWidth - parseInt(objdiv.width)) / 2) + "px";
//alert(document.documentElement.scrollHeight)
objdiv.top = parseInt((document.documentElement.clientHeight - parseInt(objdiv.height)) / 2) + "px";
}
}
//window.onresize = centerobject;
[code]
演示地址 http://demo.3water.com/js/opendiv/opendiv.htm

Javascript 相关文章推荐
Extjs学习笔记之九 数据模型(上)
Jan 11 Javascript
jQuery Ajax 仿AjaxPro.Utility.RegisterTypeForAjax辅助方法
Sep 27 Javascript
js过滤HTML标签以及空格的思路及代码
May 24 Javascript
给应用部分的js代码设定一个统一的入口
Jun 15 Javascript
jQuery实现手机号码输入提示功能实例
Apr 30 Javascript
jQuery多级手风琴菜单实例讲解
Oct 22 Javascript
JavaScript入门系列之知识点总结
Mar 24 Javascript
JS生成某个范围的随机数【四种情况详解】
Apr 20 Javascript
Javascript操作表单实例讲解(下)
Jun 20 Javascript
js仿百度切换皮肤功能(html+css)
Jul 10 Javascript
深入理解Vue 组件之间传值
Aug 16 Javascript
在create-react-app中使用sass的方法示例
Oct 01 Javascript
jquery 常用操作方法
Jan 28 #Javascript
jquery 经典动画菜单效果代码
Jan 26 #Javascript
使用JQuery进行跨域请求
Jan 25 #Javascript
javascript 的Document属性和方法集合
Jan 25 #Javascript
起点页面传值js,有空研究学习下
Jan 25 #Javascript
js 巧妙去除数组中的重复项
Jan 25 #Javascript
将函数的实际参数转换成数组的方法
Jan 25 #Javascript
You might like
两个强悍的php 图像处理类1
2009/06/15 PHP
利用phpExcel实现Excel数据的导入导出(全步骤详细解析)
2013/11/26 PHP
浅谈PHP的反射API
2017/02/26 PHP
javascript对象的property和prototype是这样一种关系
2007/03/24 Javascript
javascript 二分法(数组array)
2010/04/24 Javascript
js点击更换背景颜色或图片的实例代码
2013/06/25 Javascript
javascript实现的一个随机点名功能
2014/08/26 Javascript
JS实现动态给图片添加边框的方法
2015/04/01 Javascript
通过node-mysql搭建Windows+Node.js+MySQL环境的教程
2016/03/01 Javascript
JavaScript实现简单Tip提示框效果
2016/04/20 Javascript
JavaScript中使用import 和require打包后实现原理分析
2018/03/07 Javascript
javascript标准库(js的标准内置对象)总结
2018/05/26 Javascript
vue的for循环使用方法
2019/02/12 Javascript
JS中的模糊查询功能
2019/12/08 Javascript
js 计算月/周的第一天和最后一天代码
2020/02/01 Javascript
Vue实现图书管理案例
2021/01/20 Vue.js
[02:15]2014DOTA2国际邀请赛 专访LGD.lin小兔子是大腿
2014/07/14 DOTA
详解tensorflow训练自己的数据集实现CNN图像分类
2018/02/07 Python
python如何通过twisted实现数据库异步插入
2018/03/20 Python
PYTHON实现SIGN签名的过程解析
2019/10/28 Python
python 画条形图(柱状图)实例
2020/04/24 Python
tensorflow2.0的函数签名与图结构(推荐)
2020/04/28 Python
python 装饰器的实际作用有哪些
2020/09/07 Python
python安装mysql的依赖包mysql-python操作
2021/01/01 Python
numpy实现RNN原理实现
2021/03/02 Python
技校教师求职简历的自我评价
2013/10/20 职场文书
岗位竞聘演讲稿
2014/01/10 职场文书
中青班党性分析材料
2014/02/16 职场文书
北体毕业生求职信
2014/02/28 职场文书
支部书记四风对照材料
2014/08/28 职场文书
领导班子个人查摆问题对照检查材料
2014/10/02 职场文书
学生保证书
2015/01/16 职场文书
销售助理岗位职责
2015/02/11 职场文书
音乐教师求职信范文
2015/03/20 职场文书
面试通知短信
2015/04/20 职场文书
table不让td文字溢出操作方法
2022/12/24 HTML / CSS