js面向对象(三)组件开发实现弹窗效果

news/2024/7/19 14:19:41 标签: js, 面向对象之组件开发

组件开发:多组对象,像兄弟之间的关系(也是代码复用的一种形式)

组件开发需要解决的问题: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>


http://www.niftyadmin.cn/n/952190.html

相关文章

转: 技巧和诀窍:防范SQL注入攻击

【原文地址】Tip/Trick: Guard Against SQL Injection Attacks 【原文发表日期】 Saturday, September 30, 2006 9:11 AM SQL注入攻击是非常令人讨厌的安全漏洞&#xff0c;是所有的web开发人员&#xff0c;不管是什么平台&#xff0c;技术&#xff0c;还是数据层&#xff0c;需…

pythonflask项目实战_Flask框架的学习与实战(二):实战小项目

昨天写了一篇flask开发环境搭建&#xff0c;今天继续&#xff0c;进行一个实战小项目-blog系统。blog系统很简单&#xff0c;只有一个页面&#xff0c;然而麻雀虽小五脏俱全。这里目的不是为了做项目而做项目&#xff0c;这篇文章本意是通过这次练习传达以下几个知识点&#xf…

深入彻底理解原生js的作用域、作用域链(以及浏览器是怎样解析js代码的)

在学习闭包的过程中&#xff0c;我发现&#xff0c;要想真正的理解闭包&#xff0c;光知道什么闭包是不行的&#xff0c;我们还需要知道什么是作用域以及作用域链、函数声明和函数表达式&#xff0c;当然还牵扯到的知识有&#xff0c;垃圾回收机制和内存泄漏等&#xff0c;所以…

最近比较烦,哪位大哥指点一二!

最近比较烦 &#xff0c;现在感觉自己不知道学点什么&#xff01;现在感觉就会点数据库操作&#xff0c;很菜&#xff01;.net的具体哪个方向比较好啊&#xff1f;发展前途光明啊&#xff1f;哪位大哥指点一二 转载于:https://www.cnblogs.com/zhangzheny/archive/2007/03/07/…

状态机设计的一般步骤_PLC编程状态机如何实现

在PLC程序的编写过程中&#xff0c;可以使用状态机的控制思路&#xff0c;将一些复杂的控制过程使用状态机的方法处理。这里简单给大家介绍一下什么是状态机?如下图所示&#xff0c;为一个状态机的状态图。从以上图中可以看到&#xff0c;其是将动作执行的各个状态进行了一个划…

深入理解函数声明和函数表达式、深入理解立即执行函数(自执行函数)

在学习函数声明和函数表达式之前如果你对作用域和作用域链掌握的不是特别的好&#xff0c;建议您先看完js深入理解函数作用域和作用域链&#xff0c;再进行接下来的学习 函数声明&#xff1a;function 函数名(){} 函数表达式&#xff1a;function 函数名(){},函数名&#xff0…

高兴!

积分快到1W了&#xff01;&#xff01;转载于:https://www.cnblogs.com/zhangzheny/archive/2007/03/14/674789.html

弱引用什么时候被回收_Java进阶:搞定 JVM 垃圾回收就是这么简单

原创&#xff1a; Snailclimb来源&#xff1a; JavaGuide写在前面本节常见面试题&#xff1a;问题答案在文中都有提到如何判断对象是否死亡(两种方法)。简单的介绍一下强引用、软引用、弱引用、虚引用(虚引用与软引用和弱引用的区别、使用软引用能带来的好处)。如何判断一个常量…