HTML5+CSS3+JS 选项卡切换

先上效果图:


1、html页面:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Tab切换</title>
<link rel="stylesheet" href="css/tabswitchover.css" />
<script type="text/javascript" src="js/tabswitchover.js" ></script>
</head>
<body>
<div class="tab" id="tab">
<div id="tab_title" class="tab_title">
<ul>
<li class="selector"><a href="#">公告</a></li>
<li><a href="#">规则</a></li>
<li><a href="#">论坛</a></li>
<li><a href="#">安全</a></li>
<li><a href="#">公益</a></li>
</ul>
</div>
<div class="tab_content" id="tab_content">
<div class="tabct" style="display: block;">
<ul>
<li><a href="#">公告1</a></li>
<li><a href="#">公告2</a></li>
<li><a href="#">公告3</a></li>
<li><a href="#">公告4</a></li>
</ul>
</div>
<div class="tabct" style="display: none;">
<ul>
<li><a href="#">规则1</a></li>
<li><a href="#">规则2</a></li>
<li><a href="#">规则3</a></li>
<li><a href="#">规则4</a></li>
</ul>
</div>
<div class="tabct" style="display: none;">
<ul>
<li><a href="#">论坛1</a></li>
<li><a href="#">论坛2</a></li>
<li><a href="#">论坛3</a></li>
<li><a href="#">论坛4</a></li>
</ul>
</div>
<div class="tabct" style="display: none;">
<ul>
<li><a href="#">安全1</a></li>
<li><a href="#">安全2</a></li>
<li><a href="#">安全3</a></li>
<li><a href="#">安全4</a></li>
</ul>
</div>
<div class="tabct" style="display: none;">
<ul>
<li><a href="#">公益1</a></li>
<li><a href="#">公益2</a></li>
<li><a href="#">公益3</a></li>
<li><a href="#">公益4</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>


2、css ——tabswitchover



*{
margin: 0;
padding: 0;
list-style: none;
font-size: 12px;
}


.tab{
width: 298px;
height: 98px;
border: 1px solid #eee;
margin: 10px;
overflow: hidden;
}


.tab_title{
height: 27px;
position: relative;
background: #f7f7f7;
}


.tab_title ul{
width: 301px;
position: absolute;
left: -1;
}


.tab_title li{
float: left;
width: 58px;
height: 26px;
line-height: 26px;
text-align: center;
padding: 0 1px;
border-bottom: 1px solid #eee;
overflow: hidden;
}


.tab li a:link,.tab li a:visited{
text-decoration: none;
color: #000;
}


.tab li a:hover{
color: #f90;
font-weight: 700; /*字体变粗*/
}


.tab_title li.selector{
background: #fff;
border-bottom-color: #fff;
border-left: 1px solid #eee;
border-right: 1px solid #eee;
padding: 0;
}


.tab .tab_content .tabct {
margin: 10px 10px 10px 19px;
}


.tab .tab_content .tabct ul li{
float: left;
width: 134px;
height: 25px;
overflow: hidden;
}

3、js——tabswitchover



function $(id) {
return typeof id == "string"?document.getElementById(id):id;
}


window.onload = function() {
var titleName = $("tab_title").getElementsByTagName("li");
var tabContent = $("tab_content").getElementsByTagName("div");
if(titleName.length != tabContent.length){
return;
}

for(var i = 0; i < titleName.length; i++) {
titleName[i].id = i;
titleName[i].onmouseover = function() {
for(var j = 0; j < titleName.length; j++){
titleName[j].className = "";
tabContent[j].style.display = "none";
}
this.className = "selector";
tabContent[this.id].style.display = "block";
}
}
}

可以直接运行

<!DOCTYPE html> <html> <head> <title>Tap栏切换</title> <style> /* 设置tap栏样式 */ .tap { display: flex; background-color: #eee; padding: 10px; border-bottom: 1px solid #ccc; } /* 设置tap栏选中样式 */ .tap .active { background-color: #fff; border: 1px solid #ccc; border-bottom: none; } /* 设置tap栏选项样式 */ .tap div { cursor: pointer; padding: 10px; margin-right: 10px; border: 1px solid #ccc; border-bottom: none; border-radius: 5px 5px 0 0; } /* 设置tap栏选项悬停样式 */ .tap div:hover { background-color: #ddd; } /* 设置内容区样式 */ .content { padding: 10px; background-color: #fff; border: 1px solid #ccc; border-top: none; } </style> </head> <body> <div class="tap"> <div class="active" onclick="toggleTab(0)">选项一</div> <div onclick="toggleTab(1)">选项二</div> <div onclick="toggleTab(2)">选项三</div> </div> <div class="content"> <div id="tab1"> <h2>选项一内容</h2> <p>这是选项一的内容,可以根据自己的需求添加任意元素和样式。</p> </div> <div id="tab2" style="display:none;"> <h2>选项二内容</h2> <p>这是选项二的内容,可以根据自己的需求添加任意元素和样式。</p> </div> <div id="tab3" style="display:none;"> <h2>选项三内容</h2> <p>这是选项三的内容,可以根据自己的需求添加任意元素和样式。</p> </div> </div> <script> // 定义toggleTab函数,用于切换选项卡 function toggleTab(tabIndex) { // 获取所有选项卡元素 var tabs = document.querySelectorAll('.content > div'); // 获取所有选项元素 var tapItems = document.querySelectorAll('.tap > div'); // 遍历所有选项卡元素,将选中的选项卡显示,其他卡片隐藏 for (var i = 0; i < tabs.length; i++) { if (i === tabIndex) { tabs[i].style.display = ''; tapItems[i].classList.add('active'); } else { tabs[i].style.display = 'none'; tapItems[i].classList.remove('active'); } } } </script> </body> </html> 注释: 1. 定义tap栏样式 2. 定义tap栏选中样式 3. 定义tap栏选项样式 4. 定义tap栏选项悬停样式 5. 定义内容区样式 6. 定义tap栏选项,其中第一个选项默认选中,并绑定点击事件 7. 定义内容区,其中每个选项卡对应一个div元素,其中第一个选项卡的div元素默认显示 8. 定义toggleTab函数,用于切换选项卡 9. 获取所有选项卡元素和所有选项元素 10. 遍历所有选项卡元素,将选中的选项卡显示,其他卡片隐藏
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沉淀的沙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值