js原生tab标签页(不使用jquery)

项目需求是使用原生js写出如下样式:

先说下实现的思路,

1、关于tab按钮切换,先默认给三个tab按钮一个共有的样式,然后再单独拿一个class写一个当前选中tab的样式。切换的时候,当前的tab按钮添加这个class,同时去掉另外两个tab按钮的class(不管有没有)

2、关于tab内容的切换,一一对应,设置当前tab页的内容为display:block,其他的两个的内容设置为display:none

代码如下

html:

<div class="sellcount_tab">
    <div id="tab">
        <div id="manage" class="public">保理管理费</div>
        <div id="supervise">保理监管费</div>
        <div id="renewal">展期</div>
    </div>
</div>
<div class="content">
    <div id="manage_detail">
        <table id="manage_table">
            <thead>
                <tr>
                    <th style="width: 103px;">月份</th>
                    <th style="width: 156px;">管理费总金额</th>
                    <th style="width: 156px;">广三金额</th>
                    <th style="width: 176px;">进三金额</th>
                    <th style="width: 170px;">状态</th>
                </tr>
            </thead>
            <tr>
                <td>2020-06</td>
                <td>计算中</td>
                <td>79,000.31</td>
                <td>计算中</td>
                <td>
                    <div style="display:inline-block;">
                        <div class="pay">
                            <span class="dot"></span>
                            <span style="margin-left: 5px;">已支付</span>
                        </div>
                    </div>
                </td>
            </tr>
        </table>
    </div>
    <div id="supervise_detail" class="develop">
        <img src="./imgs/插画1@2x.png" alt="">
        <div>此功能正在火速开发中,敬请期待哦~</div>
    </div>
    <div id="renewal_detail" class="develop">
        <img src="./imgs/插画1@2x.png" alt="">
        <div>此功能正在火速开发中,敬请期待哦~</div>
    </div>
</div>

js:

var tab = document.getElementById("tab"); //tab容器(用于事件委托)
var manage = document.getElementById("manage"); //保理管理费
var supervise = document.getElementById("supervise"); //保理监管费
var renewal = document.getElementById("renewal"); //展期
var manage_detail = document.getElementById("manage_detail"); //保理管理费-详情
var supervise_detail = document.getElementById("supervise_detail"); //保理监管费-详情
var renewal_detail = document.getElementById("renewal_detail"); //展期-详情
tab.onclick = function (e) {
    var e = e || window.event;
    var target = e.target || e.srcElement;
    if (target.id == "manage") {
        manage.classList.add("public");
        supervise.classList.remove("public");
        renewal.classList.remove("public");
        manage_detail.style.display = "inline";
        supervise_detail.style.display = "none";
        renewal_detail.style.display = "none";
    }
    if (target.id == "supervise") {
        supervise.classList.add("public");
        manage.classList.remove("public");
        renewal.classList.remove("public");
        supervise_detail.style.display = "block";
        manage_detail.style.display = "none";
        renewal_detail.style.display = "none";
    }
    if (target.id == "renewal") {
        renewal.classList.add("public");
        manage.classList.remove("public");
        supervise.classList.remove("public");
        renewal_detail.style.display = "block";
        manage_detail.style.display = "none";
        supervise_detail.style.display = "none";
    }
};

css:

/* 表格样式 */

table {
    border-collapse: collapse;
    margin: 0 auto;
    text-align: center;
    font-size: 12px;
}

table td,
table th {
    border: 1px solid #e8e8e8;
    color: #666;
    height: 30px;
    margin: 0 auto;
}

table thead tr th {
    background-color: #f5f5f5;
    width: 100px;
}


.sellcount_tab {
    display: flex;
    margin: 10px 0 0 24px;
}

.content {
    height: 200px;
    margin-top: 60px;
}

.content .develop {
    text-align: center;
    display: none;
    color: #BCC3D0;
}

.content .pay {
    display: flex;
    justify-content: center;
    align-items: center;
}

.content .dot {
    width: 4px;
    height: 4px;
    border-radius: 10px;
    display: inline-block;
}

/* tab页 */

#tab {
    display: flex;
    border: 2px solid #efefef;
    border-radius: 4px;
}

#manage,
#supervise,
#renewal {
    width: 80px;
    height: 26px;
    line-height: 26px;
    text-align: center;
    font-size: 12px;
    padding: 1px 3px;
    background: #efefef;
    color: #989898;
    cursor: pointer;
}

#manage:hover,
#supervise:hover,
#renewal:hover {
    color: #666666;
}



#tab .public {
    font-weight: bold;
    color: #666666;
    background: white;
    border-radius: 4px;
    border: none;
    box-shadow: 0 0 0 2px #ededed;
}

 

期间遇到的一个问题就是border-radius背景色超出圆角,如下图:

而我想要的效果是

解决这个问题的关键是使用

box-shadow: 0 0 0 2px #ededed;

然后设置border为none

核心代码如图:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值