这款选项卡是没有任何JS的,而是通过锚点以及伪类来实现。
HTML:
<div class="news-btn">
<a href="#news1">news1</a>
<a href="#news2">news2</a>
<a href="#news3">news3</a>
</div>
<div class="news-show">
<div id="news1">this is news1</div>
<div id="news2">this is news2</div>
<div id="news3">this is news3</div>
</div>
CSS:
* {
margin: 0;
padding: 0;
}
:root {
padding: 100px 0 0 100px;
}
.news-btn {
width: 300px;
height: 40px;
display: flex;
}
.news-btn > a {
width: 100px;
display: flex;
justify-content: center;
align-items: center;
text-decoration: none;
color: #fff;
}
a:first-of-type {
background-color: yellowgreen;
}
a:nth-of-type(2) {
background-color:violet;
}
a:last-of-type {
background-color: tomato;
}
a:hover {
opacity: 0.8;
}
.news-show {
box-sizing: border-box;
width: 300px;
height: 300px;
border: 2px solid red;
overflow: hidden;
}
.news-show > div {
width: 300px;
height: 300px;
text-align: center;
line-height: 300px;
font-size: 30px;
}
:target {
color: red;
}
实现效果如下(Chrome):