通过 JavaScript实现选项卡的切换
代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>实践题 - 选项卡</title> <style type="text/css"> /* 标签和文本结合区域效果制作: 1.用ul做分类标签 2.把ul的display定义为block使ul区域和下面div区域结合在一起 3.给ul定义一个下边框或者给div内容区域定义一个上边框 4.所有li标签默认样式不设下边框,作为激活显示的li标签加宽其#fff(白色) 区域的下边框,覆盖。 */ *{ margin:0px; padding:0px; font:12px normal; font-family:微软雅黑; } #tabs{ width:290px; height:150px; padding:5px; margin:20px; } #tabs ul{ list-style:none; display:block; height:30px; line-height:30px; } #tabs ul li{ float:left; width:60px; height:28px; line-height:28px; text-align:center; display:inline-block;margin:0px 3px; border:1px solid #aaa; border-bottom:none; cursor:pointer; background:#fff; list-style:none; } #tabs ul li.on{ border-top:2px solid saddlebrown; border-bottom:2px solid #fff; } #tabs div{ height:120px; line-height:25px; border:1px solid #336699; border-top:2px saddlebrown solid;padding:5px; } .hide{display:none;} </style> <script type="text/javascript">
window.onload=function(){ var oTab = document.getElementById("tabs") var li = document.getElementsByTagName("li"); var div = oTab.getElementsByTagName("div"); 是为了定义className作用对象为id为tabs下的所有文本div for(var i=0;i<li.length;i++){ li[i].index = i; li[i].onclick = function(){ 当点击的时候所有标签都恢复最初状态 for(var n=0;n<li.length;n++){ li[n].className = ""; div[n].className = "hide"; } this.className = "on"; div[this.index].className = ""; } } } </script> </head> <body>
<div id="tabs"> <ul> <li class="on">房产</li> <li>家居</li> <li>二手房</li> </ul> <div> 275万购昌平邻铁三居 总价20万买一居<br> 200万内购五环三居 140万安家东三环<br> 北京首现零首付楼盘 53万购东5环50平<br> 京楼盘直降5000 中信府 公园楼王现房<br> </div> <div class="hide"> 40平出租屋大改造 美少女的混搭小窝<br> 经典清新简欧爱家 90平老房焕发新生<br> 新中式的酷色温情 66平撞色活泼家居<br> 瓷砖就像选好老婆 卫生间烟道的设计<br> </div> <div class="hide"> 通州豪华3居260万 二环稀缺2居250w甩<br> 西3环通透2居290万 130万2居限量抢购<br> 黄城根小学学区仅260万 121平70万抛!<br> 独家别墅280万 苏州桥2居优惠价248万<br> </div> </div>
</body> </html>
|
更新
在B站看到的另一种,代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>选项卡切换</title> <style> *{ margin:0; padding:0; } body{ background-image: url(https://i.loli.net/2020/05/02/dgPwHx76CLalENS.jpg); } .tab{ width:800px; height:500px; background-color: rgba(0, 0, 0, 0.4); margin:60px auto; } .tab_list ul{ width:800px; height:60px; } .tab_list ul li{ width:200px; height:60px; background-color:rgba(122,133,122,0.5); list-style:none; float:left; text-align:center; line-height:60px; font-size:20px; color:white; } .tab_content{ width:800px; height:440px; position:absolute; } .tab_content .item{ width:800px; height:440px; position:absolute; color:white; font-size: 50px; text-align: center; } .tab_list .bgc{ background-color:rgb(61, 9, 182); } </style> </head> <body> <div class="tab"> <div class="tab_list"> <ul> <li class="bgc">张凯</li> <li>蔡徐坤</li> <li>胡康进</li> <li>舔狗</li> </ul> </div> <div class="tab_content"> <div class="item" style="display:block;">测试</div> <div class="item">篮球少年</div> <div class="item">弟弟吧你!</div> <div class="item">舔到最后一无所有</div> </div> </div> <script> window.onload=function(){ var lis=document.getElementsByTagName("li"); var items=document.getElementsByClassName("item"); for(var i=0;i<lis.length;i++){ lis[i].index=i; lis[i].onmousemove=function(){ for(var j=0;j<items.length;j++){ lis[j].className=''; items[j].style.display="none"; } this.className="bgc" items[this.index].style.display="block";
} } } </script> </body> </html>
|
相比之下,第二种可能更好理解,但还是有些不明白的地方,以上代码是JS进阶篇结课任务,copy了dalao的代码,虽然现在有些还不太明白,打算过段时间回头继续看。