<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>移动端Tab栏切换以及横向滚动</title>
<script src="./js/jquery-3.2.1.min.js"></script>
<style>
.tabDiv {
width: 100%;
height: 52px;
border-bottom: 1px solid #e7e1e1;
color: #999;
background-color: #fff;
font-size: 14px;
overflow-x: scroll;
white-space: nowrap;
}
.tabDiv::-webkit-scrollbar {
display: none;
}
.tabDiv::-webkit-scrollbar-thumb {
display: none;
}
.tabDiv div {
text-align: center;
width: 25%;
height:40px;
display: inline-block;
line-height: 40px;
}
.tabDiv .selectTab {
color: #3c83f5;
position: relative;
font-weight: bold;
}
.tabDiv .selectTab::before {
content: ' ';
width: 40px;
height:2px;
position: absolute;
left: 50%;
bottom: 2px;
transform: translate(-50%, 0%);
background-color: #3c83f5;
}
</style>
</head>
<body>
<div class="tabDiv">
<div onclick="changeTab(this,0)" class="selectTab">语文</div>
<div onclick="changeTab(this,1)">英语</div>
<div onclick="changeTab(this,2)">数学</div>
<div onclick="changeTab(this,3)">政治</div>
<div onclick="changeTab(this,4)">历史</div>
<div onclick="changeTab(this,4)">生物</div>
<div onclick="changeTab(this,4)">地理</div>
<div onclick="changeTab(this,4)">化学</div>
</div>
<script>
function changeTab(e, index) {
$(e).addClass('selectTab').siblings().removeClass('selectTab');
if (index === 0) {
$("#beingPublic").show();
$("#myApplication").hide();
$("#myFeedback").hide();
$("#myImgDiv").hide();
$("#removeImg").hide();
}
if (index === 1) {
$("#myApplication").show();
$("#beingPublic").hide();
$("#myFeedback").hide();
$("#myImgDiv").hide();
$("#removeImg").hide();
}
if (index === 2) {
$("#myFeedback").show();
$("#myApplication").hide();
$("#beingPublic").hide();
$("#myImgDiv").hide();
$("#removeImg").hide();
}
if (index === 3) {
$("#myImgDiv").show();
$("#myApplication").hide();
$("#beingPublic").hide();
$("#myFeedback").hide();
$("#removeImg").hide();
}
if (index === 4) {
$("#removeImg").show();
$("#myApplication").hide();
$("#beingPublic").hide();
$("#myFeedback").hide();
$("#myImgDiv").hide();
}
}
</script>
</body>
</html>
效果
image.png
网友评论