js实现tab选项卡切换功能
程序员文章站
2023-11-16 08:04:46
话不多说,请看代码:
话不多说,请看代码:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>document</title> <style> *{ margin:0; padding:0; list-style: none; box-sizing: border-box; } .menu{ position: relative; display: flex; height: 20px; justify-content:space-around; } .menu span{ display: block; width: 100%; height: 100%; text-align: center; } .menu .line{ position: absolute; bottom:0; left: 0; width: 33.33%; height: 1px; background: red; -webkit-transition: all .2s; transition: all .2s; } .main{ position: relative; width: 100%; } .main li{ position: absolute; top:0; left:0; } .hide{ display: none; } .show{ display: block; } </style> </head> <body> <div class="menu"> <span>menu1</span> <span>menu2</span> <span>menu3</span> <div class="line"></div> </div> <ul class="main"> <li>menu1</li> <li class="hide">menu2</li> <li class="hide">menu3</li> </ul> <script> window.onload=function(){ var omenu=document.queryselectorall(".menu span"); var omain=document.queryselectorall(".main li"); var oline=document.queryselector(".line"); for(var i=0;i<omenu.length;i++){ omenu[i].index=i; omenu[i].onclick=function(){ var thisindex=this.index; for(var j=0;j<omain.length;j++){ omain[j].style.display="none"; omain[thisindex].style.display="block"; } oline.style.left=thisindex*33.33+"%"; } } } </script> </body> </html>
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!