js封装——封装一些小例(栗)子

在开发过程中,经常会遇到很多相同的效果和逻辑交互,相同的代码要写很多次,代码重复,这样会导致很多问题,具体问题就不说了,大家应该都懂!今天,通过几个封装效果的几个实例,来实现代码的复用,让你的代码也变的简洁高效起来吧!请往下看:封装,就是把一个零零散散的东西做成一个组件。实现共同调用,但又不相互影响,实现代码复用!网上的关于这类文章的介绍很多,我这里就不多介绍,直接上手写几个小栗子,我想会更直接;
示例1
如图:仿的百度输入法的效果


css:
 <style>
        body,ul, li,
        button {
            margin: 0; padding: 0;
        }
        
        body {
            font: 12px/1.5 Tahoma;
        }
        
        ul {
            list-style-type: none;
        }
        
        button {
            cursor: pointer;
        }
        
        #outer {
            width: 70px;
            margin: 10px auto;
        }
        
        .ime {
            margin-top: 5px;
            background: #fff;
            border: 1px solid #9a99ff;
            display: none;
        }
        
        .ime li {
            width: 100%;
            line-height: 24px;
            display: inline-block;
            vertical-align: top;
        }
        
        .close {
            border-top: 1px solid #9a99ff;
        }
        
        .ime li a {
            color: #00c;
            padding: 0 5px;
            display: block;
            text-decoration: none;
        }
        
        .ime li a:hover {
            background: #d9e1f6;
        }
    </style>

    html:
     <div id="outer">
        <button>输入法</button>
        <ul class="ime">
            <li><a href="javascript:;">手写</a></li>
            <li><a href="javascript:;">拼音</a></li>
            <li class="close"><a href="javascript:;">关闭</a></li>
        </ul>
    </div>

重点js(未经封装的常规写法):

window.onload = function() {
    var oBtn = document.getElementsByTagName("button")[0];
    var oIme = document.getElementById("ime");
    var oClose = document.getElementById("close");
    var style = oIme.style;
    oBtn.onclick = function() {
        style.display = style.display == "block" ? "none" : "block"
    };
    oClose.onclick = function() {
        style.display = "none"
    }
};

封装后的写法:

window.onload = function() {
    new Writes("outer");    //调用方法,直接new一下就行了
}
function Writes(id) {
    var oUter = document.getElementById(id);
    this.oBtn = oUter.getElementsByTagName("button")[0];
    this.oIme = oUter.getElementsByClassName("ime")[0];
    this.oClose = oUter.getElementsByClassName("close")[0];
    var _this = this;
    this.style = this.oIme.style;
    this.oBtn.onclick = function() {
        _this.shows(this);
    };
    this.oClose.onclick = function() {
        _this.hides(this);
    };
};
Writes.prototype.shows = function() {
    this.style.display = this.style.display == "block" ? "none" : "block"
};
Writes.prototype.hides = function() {
    this.style.display = "none"
};

<b>示例2</b>
如图:弹出层效果,很常见的效果


css:
<style>
        html,
        body {
            height: 100%;
            overflow: hidden;
        }
        
        body,
        div,
        h2 {
            margin: 0;
            padding: 0;
        }
        
        body {
            font: 12px/1.5 Tahoma;
        }
        
        center {
            padding-top: 10px;
        }
        
        button {
            cursor: pointer;
        }
        
        .overlay {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: #000;
            opacity: 0.5;
            filter: alpha(opacity=50);
            display: none;
        }
        
        .win {
            position: absolute;
            top: 50%;
            left: 50%;
            width: 400px;
            height: 200px;
            background: #fff;
            border: 4px solid #f90;
            margin: -102px 0 0 -202px;
            display: none;
        }
        
        h2 {
            font-size: 12px;
            text-align: right;
            background: #FC0;
            border-bottom: 3px solid #f90;
            padding: 5px;
        }
        
        h2 span {
            color: #f90;
            cursor: pointer;
            background: #fff;
            border: 1px solid #f90;
            padding: 0 2px;
        }
    </style>
    html:
    <div id="cont">
        <div class="overlay"></div>
        <div class="win">
            <h2><span class="close">×</span></h2>
        </div>
        <button>弹出层</button>
    </div>

未封装的写法:
--------------------------
window.onload = function() {
    var cont = document.getElementById('cont');
    var oWin = document.getElementsByClassName("win")[0];
    var oLay = document.getElementsByClassName("overlay")[0];
    var oBtn = document.getElementsByTagName("button")[0];
    var oClose = document.getElementsByClassName("close")[0];
    oBtn.onclick = function() {
        oLay.style.display = "block";
        oWin.style.display = "block"
    };
    oClose.onclick = function() {
        oLay.style.display = "none";
        oWin.style.display = "none"
    };
};

封装后的写法,对比一下
--------------------------
window.onload = function() {
      new shows('cont'); //调用方法,直接new一下就行了
};
function shows(id) {
    var cont = document.getElementById(id);
    this.oWin = cont.getElementsByClassName("win")[0];
    this.oLay = cont.getElementsByClassName("overlay")[0];
    this.oBtn = cont.getElementsByTagName("button")[0];
    this.oClose = cont.getElementsByClassName("close")[0];
    var _this = this;
    this.oBtn.onclick = function() {
        _this.block();
    };
    this.oClose.onclick = function() {
        _this.none();
    }
};
shows.prototype.block = function() {
    this.oLay.style.display = "block";
    this.oWin.style.display = "block"
};
shows.prototype.none = function() {
    this.oLay.style.display = "none";
    this.oWin.style.display = "none"
};

<b>简单总结:</b>
1.不能存在函数嵌套,全局变量;
2.window.onload改写成构造函数的形式;
3.全局变量改写成属性;
4.函数改写成方法;
6.最后,注意一下this,事件方法,闭包,传参这几点注意一下,就ok了?

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,005评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,446评论 4 61
  • 很久没写英文,突然有一天接到要使用英文写作的工作,犯难了,怎么捡回自己的英文写作? 想要短时间用英文写得文采飞扬难...
    吉小舒阅读 5,192评论 3 23
  • 你会被年龄限制吗?昨天新生大学给本周的讨论题目。算我今天的命题作文 年龄就是一堵正在起着的围墙,小时候没有墙壁的时...
    yichen大刀阅读 1,846评论 0 0

友情链接更多精彩内容