小编最近在做项目的时候前端需要tab标签来进行角色分类,小编的公司用的是angular,虽然一般情况下angular挺好用的,但是这次例外了,angular的标签并不能良好的达到我想要的效果,于是小编找了一个html改装的tab,如下:
html内容如下
<div class="tabs">
<div class="tab-2">
<label for="tab2-1">一</label>
<input id="tab2-1" name="tabs-two" type="radio" checked="checked">
<div>
<h4>第一个标签页</h4>
<p></p>
</div>
</div>
<div class="tab-2">
<label for="tab2-2">二</label>
<input id="tab2-2" name="tabs-two" type="radio">
<div>
<h4>第二个标签页</h4>
<p></p>
</div>
</div>
</div>
css样式内容如下
.tabs {
display: block;
display: -webkit-flex;
display: -moz-flex;
display: flex;
-webkit-flex-wrap: wrap;
-moz-flex-wrap: wrap;
flex-wrap: wrap;
margin: 0;
overflow: hidden; }
.tabs [class^="tab"] label,
.tabs [class*=" tab"] label {
/* color: black; */
cursor: pointer;
display: block;
font-size: 1.1em;
font-weight: 300;
line-height: 1em;
padding: 2rem 0;
text-align: center; }
.tabs [class^="tab"] [type="radio"],
.tabs [class*=" tab"] [type="radio"] {
/* border-bottom: 1px solid rgba(239, 237, 239, 0.5); */
cursor: pointer;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
display: block;
width: 100%;
-webkit-transition: all 0.3s ease-in-out;
-moz-transition: all 0.3s ease-in-out;
-o-transition: all 0.3s ease-in-out;
transition: all 0.3s ease-in-out; }
.tabs [class^="tab"] [type="radio"]:hover, .tabs [class^="tab"] [type="radio"]:focus,
.tabs [class*=" tab"] [type="radio"]:hover,
.tabs [class*=" tab"] [type="radio"]:focus {
/* tab标签选中的颜色标志 */
border-bottom: 1px solid #40a9ff;
}
.tabs [class^="tab"] [type="radio"]:checked,
.tabs [class*=" tab"] [type="radio"]:checked {
border-bottom: 2px solid #40a9ff;
}
.tabs [class^="tab"] [type="radio"]:checked + div,
.tabs [class*=" tab"] [type="radio"]:checked + div {
opacity: 1; }
.tabs [class^="tab"] [type="radio"] + div,
.tabs [class*=" tab"] [type="radio"] + div {
display: block;
opacity: 0;
padding: 2rem 0;
width: 90%;
-webkit-transition: all 0.3s ease-in-out;
-moz-transition: all 0.3s ease-in-out;
-o-transition: all 0.3s ease-in-out;
transition: all 0.3s ease-in-out; }
.tabs .tab-2 {
width: 50%; }
.tabs .tab-2 [type="radio"] + div {
width: 200%;
margin-left: 200%; }
.tabs .tab-2 [type="radio"]:checked + div {
margin-left: 0; }
.tabs .tab-2:last-child [type="radio"] + div {
margin-left: 100%; }
.tabs .tab-2:last-child [type="radio"]:checked + div {
margin-left: -100%; }
鼠标交互事件封装在了css中,其中hover代表鼠标挪到标签页上方,focus代表当前选中的标签,checked表示标签点击(样式),点击触发的事件需重新定义click事件。在这里checked不能用来进行其他的事件触发了,具体业务处理逻辑根据项目业务具体实现。
效果图如下,解决了angular的tab不能让标签内的文字居中的问题。
总结
其实原生的,基本的东西也挺好的,哈哈哈哈哈