试着用原生JS实现TAB切换效果。看下DEMO,如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>试着用原生JS实现TAB切换效果 - Web前端之家</title> <style> .bgbox{ width: 500px; margin:50px auto; } .tab-top { width: 500px; margin:auto; font-size:0; } ul,li{list-style:none;margin:0;padding:0;} .tab-top li { display:inline-block; font-size:16px; width: 24%; line-height: 40px; border: 1px solid #ccc; text-align:center } .tabbox .boxlist { display: none; width: 500px; height: 200px; border: 1px solid #ccc; background: pink; } .active { color: red; background: #1890ff; } </style> </head> <body> <div class="bgbox"> <ul class="clearfix tab-top" id="tabcilk"> <li class="fl active">1</li> <li class="fl">2</li> <li class="fl">3</li> <li class="fl">4</li> </ul> <div class="tabbox" id="tabbox"> <div class="boxlist " style="display: block;">1</div> <div class="boxlist">2</div> <div class="boxlist">3</div> <div class="boxlist">4</div> </div> </div> </body> <script> var ali = document.getElementById("tabcilk").getElementsByTagName("li"); var adiv = document.getElementById("tabbox").getElementsByTagName("div"); function tab(btn, boxlist) { for (var i = 0; i < btn.length; i++) { btn[i].index = i; btn[i].onclick = function () { //alert(this.index ); for (var j = 0; j < btn.length; j++) { btn[j].classList.remove("active") boxlist[j].style.display = "none"; btn[this.index].classList.add("active"); boxlist[this.index].style.display = "block"; } } } } tab(ali, adiv); </script> </html>
网友评论文明上网理性发言 已有0人参与
发表评论: