JS实现Tab栏切换的两种方式案例详解

这篇文章主要介绍了JS实现Tab栏切换的两种方式,一种是面向过程的写法,一种是面向对象的写法,本文给大家分享详细案例代码,需要的朋友可以参考下!

JS实现Tab栏切换的两种方式案例详解

面向过程的写法

<!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>Document</title>
<style>
*{
padding: 0;
margin: 0;
}
div{
width: 500px;
border: 1px solid red;
}
ul{
overflow: hidden;
}
ul>li{
list-style: none;
float: left;
width: 50px;
text-align: center;
}
div>div{
height: 300px;
background-color: pink;
text-align: center;
font-size: 50px;
}
#lastBox{
display: none;
}
.selected{
background-color: orange;
}
</style>
</head>
<body>
<div>
<ul>
<li id="first" class="selected">1</li>
<li id="last">2</li>
</ul>
<div id="firstBox">11111</div>
<div id="lastBox">222222</div>
</div>
<script>
   var lis =  document.querySelectorAll('ul>li')
   var content =  document.querySelectorAll('div>div')
   //藏值  通过属性来保存对应的值   通过对应的对象来藏
   //加所有li添加点击事件
   for(var i=0;i<lis.length;i++){
// lis[i].setAttribute('data-index',i)
lis[i].index = i
lis[i].onclick = function(){
//排他思想 先将所有的全部设置一个值 然后再给自己设置一个值
for(var j=0;j<lis.length;j++){
lis[j].className = ""
}
this.className = "selected"
//排他
//先将其他的都隐藏 再给自己显示
for(var j=0;j<content.length;j++){
content[j].style.display = "none"
}
// document.getElementById(this.id+'Box').style.display = 'block'
// content[this.getAttribute('data-index')].style.display = 'block'
content[this.index].style.display = 'block'
}
   }
</script>
</body>
</html>

面向对象的写法

<!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>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
#box{
width: 400px;
height: 400px;
border: 2px solid pink;
margin: 0 auto;
}
ul{
display: flex;
margin: 0 auto;
width: 400px;
position: relative;
}
ul>li{
list-style: none;
width: 100px;
height: 50px;
font-size: 40px;
text-align: center;
border: 1px solid white;
background-color: yellow;

}
ul .showli{
background-color: orange;
}
#box div{
width: 400px;
height: 350px;
position: absolute;
display: none;
}

#box .show{
display: block;
background-color: rgb(0, 183, 255);
font-size: 80px;
text-align: center;
}
</style>
</head>
<body>
<div id="box">
<ul>
<li class="showli">1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>

<div class="show">1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
<script>
//用面向对象的思维写tab栏切换
//属性:选项框,选项框对应要切换的页面
class Tab{
constructor(ele){
this.box = document.getElementById(ele);
this.lis = this.box.querySelectorAll('ul > li');
this.divs = this.box.querySelectorAll('div');
}

change()
{
// console.log(this.box);
// console.log(this.lis);
// console.log(this.divs);

for(var i = 0; i < this.lis.length; i++)
{
//保存当前this指向
var _this = this; 
//保存每一个li的下标,便于切换指定div
this.lis[i].index = i; 
//每一个li添加点击事件
this.lis[i].onclick = function() 
{

//排他,清空所有li与盒子的类名
for(var j = 0; j < _this.lis.length; j++) 
{
_this.lis[j].className = "";
_this.divs[j].className = "";
}
//单独设置点击的li类名,让其样式和其余未被点击的li不同
this.className = "showli";

//检查当前li的下标是否成功保存
// console.log(this.index);

//让li对应的div显示出来
_this.divs[this.index].className = "show";
}

}
}
}

var tab = new Tab("box");
tab.change();

</script>
</body>
</html>