js制作Tab选项卡组件

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="css/tab.css">
</head>
<body>
    <div id="Tab" class="Tab">
        <div class="tab_title">
            <div>tab1</div>
            <div>tab2</div>
            <div>tab3</div>
        </div>

        <div class="tab_panel">
            <div class="panel"> panel_1</div>
            <div class="panel"> panel_2</div>
            <div class="panel"> panel_3</div>
        </div>
    </div>

    <div id="Tab2" class="Tab">
        <div class="tab_title">
            <div>tab1</div>
            <div>tab2</div>
            <div>tab3</div>
        </div>

        <div class="tab_panel">
            <div class="panel"> panel_1</div>
            <div class="panel"> panel_2</div>
            <div class="panel"> panel_3</div>
        </div>
    </div>
<script src="js/tab.js"></script>
<script>

    new Tab("#Tab",1);//1
    
    new Tab("#Tab2");

</script>
</body>
</html>

css

*{
    padding: 0;
    margin: 0;
}

.Tab{
    width: 250px;
    height: 140px;
    border:solid 0px #ccc;
    margin-left: 50px;
    margin-top: 50px;
}

.tab_title{
    width: 100%;
    height: 35px;
    border:solid 0px red;
    display: flex;
}

.tab_title div{
    width:50px;
    height:inherit;
    text-align: center;
    border: solid 1px transparent;
    line-height: 35px;
    color: black;
    margin-top: auto;
}

.tab_title div:hover{
    color: #75f2ff;
    cursor: pointer;
}

.tab_title div.active{
    border-color: #ccc #ccc white #ccc;
    background-color: white;
    color:#ff6d1f;
    border-top-left-radius: 5px;
    border-top-right-radius: 5px;
}

.tab_panel div.active{
    display: block;
}

.tab_panel{
    width: 100%;
    height: 115px;
    border: solid 1px #ccc;
}
.tab_panel div{
    width: 100%;
    height: inherit;
    padding: 10px;
    display: none;
}

js



//这里的参数tab是可以自己选择的 诺不选择则为默认值
function Tab(tabId,active,tab){
    this.init(tabId,active,tab);//2
}

Tab.prototype.init =function(tabId,active,tab){
    tab = tab || {};
    this.titles = tab.titles || ".tab_title div";
    this.tabs = tab.tabs || ".tab_panel div";

    active = active || 0;//若用户没有传入值则默认显示下标为0的 否则显示用户自己选择的
    var elem = document.querySelector(tabId);
    console.log(elem);
    this.tabTitle = elem.querySelectorAll(this.titles);
    this.tabPanel = elem.querySelectorAll(this.tabs);
    this.active(active);//3
    this.event();
};

Tab.prototype.active= function(index){
    if(index===this.current){
        return ;
    }

    //4
    this.tabTitle[index].classList.add("active");
    this.tabPanel[index].classList.add("active");
    console.log(this.current)
    if(typeof this.current  === "number"){//5
        //当current等于整型的时候 当前index是不会和current相等的
        //也就是说当current等于1时 index等于0 所以这样就实现了一个选项卡的效果 等于1的隐藏 等于0的显示
        this.tabTitle[this.current].classList.remove("active");
        this.tabPanel[this.current].classList.remove("active");
    }
    this.current = index;//6
};

Tab.prototype.event= function(){
    var len = this.tabTitle.length;
    var that = this;
    for(let i=0;i<len;i++){
        this.tabTitle[i].addEventListener("click",function(){
            that.active.call(that,i);//7
        })
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值