本来应该在上周末写出来的,可上周事太多,而且在一周中,又发现了N多种解决方案,一一吃透,一周过去了。
一定还会有别的方法,这里只列出我目前实现了的和搞懂的。
DEMO地址
1,利用a标签锚点定位,配合overflow:hidden
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
*{
margin: 0;
padding: 0;
}
ul{
list-style: none;
}
.tabDiv{
width: 200px;
height: 70px;
background:#ccc;
overflow: hidden;
}
</style>
</head>
<body>
<center>
<ul class="head">
<label for="ul1">
<li>Tab1</li>
</label>
<a href="#ul2">
<li>Tab2</li>
</a>
<a href="#ul3">
<li>Tab3</li>
</a>
<a href="#ul4">
<li>Tab4</li>
</a>
</ul>
<div class="tabDiv">
<ul id="ul1">
<li>1</li>
<li>1</li>
<li>1</li>
<li>1</li>
</ul>
<ul id="ul2">
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
</ul>
<ul id="ul3">
<li>3</li>
<li>3</li>
<li>3</li>
<li>3</li>
</ul>
<ul id="ul4">
<li>4</li>
<li>4</li>
<li>4</li>
<li>4</li>
</ul>
</div>
</center>
</body>
</html>
2,利用a标签,配合css:target选择器和z-index
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>选项卡</title>
<style>
*{
margin: 0px;
border: 0px;
}
.all{
background-color: #ffffff;
}
body{
margin-left: 40%;
}
.mes{
height: 150px;
width: 300px;
}
#id1{
background-color: red;
position: absolute;
z-index: 3;
}
#id2{
background-color: blue;
position: absolute;
z-index: 2;
}
#id3{
position: absolute;
z-index: 1;
background-color: green;
}
#id1:target,#id2:target,#id3:target
{
position: absolute;
z-index: 4;
}
</style>
</head>
<body>
<a href="#id1">第一个选项卡</a>
<a href="#id2">第二个选项卡</a>
<a href="#id3">第三个选项卡</a>
<div class="mes" id="id1">
<p>这里是第一个选项卡</p>
</div>
<div class="mes" id="id2">
<p>这里是第二个选项卡</p>
</div>
<div id="id3" class="mes" >
<p>这里是第三个选项卡</p>
</div>
</body>
</html>
3,利用单选框label for,配合z-index
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.tabs{
position: absolute;
}
.tab{
float: left;
margin-left: 10px;
}
.content {
position: absolute;
background: red;
top: 30px;
left: 20px;
width: 300px;
height: 260px;
z-index: 1;
}
input{
display: none;
}
.tab input:checked + .content {
z-index: 2;
}
</style>
</head>
<body>
<div class="tabs">
<div class="tab">
<label for="check1">11111111111111</label>
<input id="check1" type="radio" name="tabs" checked="checked" />
<div class="content">
选项卡1
</div>
</div>
<div class="tab">
<label for="check2">22222222222222</label>
<input id="check2" type="radio" name="tabs" />
<div class="content">
选项卡2
</div>
</div>
<div class="tab">
<label for="check3">33333333333333</label>
<input id="check3" type="radio" name="tabs" />
<div class="content">
选项卡3
</div>
</div>
</div>
</body>
</html>
4,利用单选框label for,通过:checked 来判断是否被选中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
padding: 20px;
text-align: left;
font-family: Lato;
color: #fff;
background: #9b59b6;
}
.tabs {
width: 650px;
float: none;
list-style: none;
position: relative;
margin: 80px 0 0 10px;
text-align: left;
}
.tabs li {
float: left;
display: block;
}
.tabs input[type="radio"] {
position: absolute;
top: -9999px;
left: -9999px;
}
.tabs label {
display: block;
padding: 14px 21px;
border-radius: 2px 2px 0 0;
font-size: 20px;
font-weight: normal;
text-transform: uppercase;
background: #8e44ad;
cursor: pointer;
position: relative;
top: 4px;
transition: all 0.2s ease-in-out;
}
.tabs label:hover {
background: #703688;
}
.tabs .tab-content {
z-index: 2;
display: none;
overflow: hidden;
width: 100%;
font-size: 17px;
line-height: 25px;
padding: 25px;
position: absolute;
top: 53px;
left: 0;
background: #612e76;
}
.tabs [id^="tab"]:checked + label {
top: 0;
padding-top: 17px;
background: #612e76;
}
.tabs [id^="tab"]:checked ~ [id^="tab-content"] {
display: inline;
}
</style>
</head>
<body>
<ul class="tabs">
<li>
<input type="radio" name="tabs" id="tab1" checked />
<label for="tab1">选项卡 1</label>
<div id="tab-content1" class="tab-content">
<p>选项卡内容 1</p>
</div>
</li>
<li>
<input type="radio" name="tabs" id="tab2" />
<label for="tab2">选项卡 2</label>
<div id="tab-content2" class="tab-content">
<p>选项卡内容 2</p>
</div>
</li>
</ul>
</body>
</html>