js面向对象(三)组件开发实现弹窗效果
2018-03-21 15:29
776 查看
组件开发:多组对象,像兄弟之间的关系(也是代码复用的一种形式)
组件开发需要解决的问题:1、参数的顺序问题 2、参数不写报错的问题
解决办法:主要就是通过设置默认参数和配置参数
话不多说,直接上代码<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>面向对象之组件开发之弹窗练习</title>
<style>
*{margin:0; padding:0;}
.btn{width:50px; height:30px; background:yellowgreen; margin-left:30px;}
.div1{border:1px solid black; position:absolute; left:0px; top:100px;z-index:2;}
.title{height:30px; line-height:30px; background:lightgray;}
.close{display:inline-block; width:28px; height:28px;border:1px solid white; float:right; text-align:center;border-radius:50%; line-height:28px; color:red; }
.close:hover{background:red; color:#fff;}
#mark{z-index:1;position:absolute; left:0; top:0; background:gray; filter:alpha(opacity=50); opacity:0.5;}
</style>
</head>
<body>
<input class="btn" type="button" value="按钮1">
<input class="btn" type="button" value="按钮2">
<input class="btn" type="button" value="按钮3">
<!--<div id="div1">
<div class="title">
<span></span><span class="close">X</span>
</div>
<div class="content"></div>
</div>
<div id="mark"></div>
-->
<script>
window.onload = function(){
var aBtn = document.getElementsByClassName('btn');
aBtn[1].onclick = function(){
var win2 = new Wind();
win2.init({//配置参数
iNow:1,
w:200,
h:300,
dir:'right',
title:"公告"
});
}
aBtn[0].onclick = function(){
var win1 = new Wind();
win1.init({//配置参数
iNow:0,
title:"登录"
});
}
aBtn[2].onclick = function(){//配置参数
var win1 = new Wind();
win1.init({
iNow:2,
mark:true
});
}
}
function Wind(){
this.oDiv = null;
this.setting = {//默认参数
w:300,
h:300,
dir:'center',
title:'',
mark:false
}
}
Wind.prototype.json={};
Wind.prototype.init = function(opt){
extend(this.setting,opt);
if(this.json[opt.iNow]==undefined){
this.json[opt.iNow]=true;
}
if(this.json[opt.iNow]){
this.fnCreate();
this.fnClose();
if(this.setting.mark){
this.fnMark();
}
}
this.json[opt.iNow]=false;
}
Wind.prototype.fnCreate = function(){
this.oDiv = document.createElement('div');
this.oDiv.className = 'div1';
this.oDiv.innerHTML = '<div class="title"><span>'+ this.setting.title+'</span><a class="close">X</a></div><div class="content"></div>';
document.body.appendChild(this.oDiv);
this.setStyle();
}
Wind.prototype.fnMark = function(){
var oMark = document.createElement('div');
oMark.id = 'mark';
document.body.appendChild(oMark);
this.oMark = oMark;
oMark.style.width=veiwWidth()+'px';
oMark.style.height=veiwHeight()+'px';
}
Wind.prototype.setStyle = function(){
this.oDiv.style.width = this.setting.w+'px';
this.oDiv.style.height = this.setting.h +'px';
if(this.setting.dir=='center'){
this.oDiv.style.left = (veiwWidth() - this.oDiv.offsetWidth)/2 +'px';
this.oDiv.style.top = (veiwHeight() - this.oDiv.offsetHeight)/2 +'px';
}
if(this.setting.dir=='right'){
this.oDiv.style.left = (veiwWidth() - this.oDiv.offsetWidth)+'px';
this.oDiv.style.top = (veiwHeight() - this.oDiv.offsetHeight) +'px';
}
}
Wind.prototype.fnClose = function(){
var oClose = this.oDiv.getElementsByClassName('close')[0];
var This = this;
oClose.onclick = function(){
document.body.removeChild(This.oDiv);
if(This.setting.mark){
document.body.removeChild(This.oMark);
}
This.json[This.setting.iNow] = true;
};
}
function extend(obj1,obj2){
for(var attr in obj2){
obj1[attr] = obj2[attr];
}
}
function veiwWidth(){
return document.documentElement.clientWidth;
}
function veiwHeight(){
return document.documentElement.clientHeight;
}
</script>
</body>
</html>
组件开发需要解决的问题:1、参数的顺序问题 2、参数不写报错的问题
解决办法:主要就是通过设置默认参数和配置参数
话不多说,直接上代码<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>面向对象之组件开发之弹窗练习</title>
<style>
*{margin:0; padding:0;}
.btn{width:50px; height:30px; background:yellowgreen; margin-left:30px;}
.div1{border:1px solid black; position:absolute; left:0px; top:100px;z-index:2;}
.title{height:30px; line-height:30px; background:lightgray;}
.close{display:inline-block; width:28px; height:28px;border:1px solid white; float:right; text-align:center;border-radius:50%; line-height:28px; color:red; }
.close:hover{background:red; color:#fff;}
#mark{z-index:1;position:absolute; left:0; top:0; background:gray; filter:alpha(opacity=50); opacity:0.5;}
</style>
</head>
<body>
<input class="btn" type="button" value="按钮1">
<input class="btn" type="button" value="按钮2">
<input class="btn" type="button" value="按钮3">
<!--<div id="div1">
<div class="title">
<span></span><span class="close">X</span>
</div>
<div class="content"></div>
</div>
<div id="mark"></div>
-->
<script>
window.onload = function(){
var aBtn = document.getElementsByClassName('btn');
aBtn[1].onclick = function(){
var win2 = new Wind();
win2.init({//配置参数
iNow:1,
w:200,
h:300,
dir:'right',
title:"公告"
});
}
aBtn[0].onclick = function(){
var win1 = new Wind();
win1.init({//配置参数
iNow:0,
title:"登录"
});
}
aBtn[2].onclick = function(){//配置参数
var win1 = new Wind();
win1.init({
iNow:2,
mark:true
});
}
}
function Wind(){
this.oDiv = null;
this.setting = {//默认参数
w:300,
h:300,
dir:'center',
title:'',
mark:false
}
}
Wind.prototype.json={};
Wind.prototype.init = function(opt){
extend(this.setting,opt);
if(this.json[opt.iNow]==undefined){
this.json[opt.iNow]=true;
}
if(this.json[opt.iNow]){
this.fnCreate();
this.fnClose();
if(this.setting.mark){
this.fnMark();
}
}
this.json[opt.iNow]=false;
}
Wind.prototype.fnCreate = function(){
this.oDiv = document.createElement('div');
this.oDiv.className = 'div1';
this.oDiv.innerHTML = '<div class="title"><span>'+ this.setting.title+'</span><a class="close">X</a></div><div class="content"></div>';
document.body.appendChild(this.oDiv);
this.setStyle();
}
Wind.prototype.fnMark = function(){
var oMark = document.createElement('div');
oMark.id = 'mark';
document.body.appendChild(oMark);
this.oMark = oMark;
oMark.style.width=veiwWidth()+'px';
oMark.style.height=veiwHeight()+'px';
}
Wind.prototype.setStyle = function(){
this.oDiv.style.width = this.setting.w+'px';
this.oDiv.style.height = this.setting.h +'px';
if(this.setting.dir=='center'){
this.oDiv.style.left = (veiwWidth() - this.oDiv.offsetWidth)/2 +'px';
this.oDiv.style.top = (veiwHeight() - this.oDiv.offsetHeight)/2 +'px';
}
if(this.setting.dir=='right'){
this.oDiv.style.left = (veiwWidth() - this.oDiv.offsetWidth)+'px';
this.oDiv.style.top = (veiwHeight() - this.oDiv.offsetHeight) +'px';
}
}
Wind.prototype.fnClose = function(){
var oClose = this.oDiv.getElementsByClassName('close')[0];
var This = this;
oClose.onclick = function(){
document.body.removeChild(This.oDiv);
if(This.setting.mark){
document.body.removeChild(This.oMark);
}
This.json[This.setting.iNow] = true;
};
}
function extend(obj1,obj2){
for(var attr in obj2){
obj1[attr] = obj2[attr];
}
}
function veiwWidth(){
return document.documentElement.clientWidth;
}
function veiwHeight(){
return document.documentElement.clientHeight;
}
</script>
</body>
</html>
相关文章推荐
- android UI进阶之弹窗开发--实现通讯录的弹窗效果
- js面向对象 实现 tab选项卡效果
- Cocos2d-js 实现弹窗背景虚化效果
- 【Android UI设计与开发】第14期:顶部标题栏(五)两种方式实现仿微信标题栏弹窗效果
- 【Android UI设计与开发】第14期:顶部标题栏(五)两种方式实现仿微信标题栏弹窗效果
- js点击弹出div层实现可拖曳的弹窗效果
- Tree的实现,js开发组件dtree
- [js开源组件开发]loading加载效果
- JS实现可拖曳、可关闭的弹窗效果
- 【Android UI设计与开发】第14期:顶部标题栏(五)两种方式实现仿微信标题栏弹窗效果
- JS实现可拖曳、可关闭的弹窗效果
- CSS学习笔记-附加篇( CSS+JS实现的选项卡效果(html组件))
- js基于面向对象实现网页TAB选项卡菜单效果代码
- 原生js实现类似弹窗抖动效果
- 【Android UI设计与开发】第14期:顶部标题栏(五)两种方式实现仿微信标题栏弹窗效果
- js基于面向对象实现网页TAB选项卡菜单效果代码
- 用Ext实现js方法的继承,以及扩展Ext组件的开发
- JS基于面向对象实现的放烟花效果
- js点击弹出div层实现可拖曳的弹窗效果
- JS弹窗效果的实现