网站首页 文章专栏 es6 class 的 constructor super关键字使用
es6 class 的 constructor super关键字使用
编辑时间:2017-11-21 16:20:00 作者:lizhi 浏览量:814
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .on{ background: rgb(9, 224, 134);}
        .box div{ width: 200px; height: 200px; border: 1px solid #666; display: none;}
        .box div.active{ display: block;}
    </style>
</head>

<body>
    <div id="box" class="box">
        <input type="button" class="on" value="aaa">
        <input type="button" value="bbb">
        <input type="button" value="ccc">
        <div class="active">aaaa</div>
        <div>bbbb</div>
        <div>cccc</div>
    </div>

    <div id="box2" class="box">
        <input type="button" class="on" value="aaa">
        <input type="button" value="bbb">
        <div class="active">aaaa</div>
        <div>bbbb</div>
    </div>

    <script>
        class Tab{
            constructor(config){
                this.config = config || {};
                if(typeof this.config.el == 'undefined') return; //id必填
                this.oBox = document.getElementById(this.config.el);
                this.aInp = this.oBox.getElementsByTagName('input');
                this.aDiv = this.oBox.getElementsByTagName('div');
                this.length = this.aDiv.length;
                this.n = 0;
                this.init();
            };
            init(){
                for(let i=0;i<this.length;i++){
                    this.aInp[i].onclick=function(){
                        document.title = i;
                        this.hide();
                        this.show(i);
                    }.bind(this);
                }
            };
            show(index){
                this.aInp[index].setAttribute('class','on');
                this.aDiv[index].setAttribute('class','active');
                this.n =index;
            };
            hide(){
               
                for(let i=0; i<this.length;i++){
                    this.aInp[i].setAttribute('class','');
                    this.aDiv[i].setAttribute('class','');
                }
            };
        };

        class AutoTab extends Tab{
            constructor(config){
                super(config);
                setInterval(function(){
                    this.next()
                }.bind(this),1000);
                console.log(this);
            };
            next(){
                
               if(this.n >= this.length-1){  
                   this.n = 0
                }else{
                   this.n++  ;
                }
                console.log(this.n);
                this.hide();
                this.show(this.n);
            };
        };


        new Tab({
            el:'box'
        });

        new AutoTab({
            el:'box2'
        });
       
    </script>
</body>

</html>

来说两句吧
最新评论
  • hongsir.top
    hongsir.top 2017-11-28 09:30:17 重庆市--电信
    这个评论插件,是自定义的么