This article describes the method of implementing the tab menu switching effect by js+css. Share it for your reference. The specific implementation method is as follows:
index.css is as follows:
Copy the code as follows:* {
margin: 0px;
padding: 0px;
}
body {
width: 600px;
margin: 0 auto;
background-color: silver;
}
#contanier {
background-color: yellow;
width: 600px;height: 400px;
}
#tabNavi {
width: 600px;height: 30px;
background-color: #00bfff;
text-decoration: none;
list-style-type: none;
}
#tabNavi li {
float: left;
margin-right: 7px;
background-color: #008b8b;
color: white;
cursor: pointer;
width: 60px;
height: 28px;
line-height: 30px;
text-align: center;
}
#content {
width: 600px;height: 370px;
background-color: white;
}
Index.html is as follows:
Copy the code as follows: <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>js realizes dynamic switching effect of tab menu</title>
<link href="css/index.css" rel="stylesheet" />
<script type="text/javascript">
function gel(id) {
return document.getElementById(id);
}
var arr = [
{ "title": "News", "content": "This is a news channel" },
{ "title": "Financial", "content": "This is the financial channel" },
{ "title": "Entertainment", "content": "This is an entertainment channel" },
{ "title": "Sports", "content": "This is a sports channel" },
{ "title": "car", "content": "This is the car channel" },
{ "title": "Video", "content": "This is a video channel" },
{ "title": "Life", "content": "This is a life channel" }
];
window.onload = function() {
for (var i = 0; i < arr.length; i++) {
var liNew = document.createElement("li");
liNew.innerHTML = arr[i].title;
gel("tabNavi").appendChild(liNew);
//Bind click events on these lis, so you need to assign a property to each of them (preferably id)
liNew.setAttribute("id", i);
liNew.onclick = function () {
var navs = gel("tabNavi").childNodes;
//Clear all colors
for (var j = 0; j < navs.length; j++) {
if (navs[j].nodeType == 1) {
navs[j].style.backgroundColor ="#008b8b";
}
}
this.style.backgroundColor = "red";
gel("content").innerHTML = arr[this.id].content;
};
}
};
</script>
</head>
<body>
<div id="contanier">
<ul id="tabNavi"></ul>
<div id="content"></div>
</div>
</body>
</html>
I hope this article will be helpful to everyone's JavaScript programming.