项目需求是使用原生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
核心代码如图: