原生js+css實(shí)現(xiàn)tab切換功能
本文實(shí)例為大家分享了原生css+js實(shí)現(xiàn)tab切換功能的具體代碼,供大家參考,具體內(nèi)容如下
現(xiàn)在很多的ui框架都集成了tab功能,使用過程中只需按照他們的api套用即可,但在有時(shí)jquery項(xiàng)目中會(huì)覺得為了一個(gè)tab功能再單獨(dú)調(diào)用一個(gè)ui庫(kù)有些小題大做,所以博主這里推薦了原生tab的實(shí)現(xiàn)
分析:通過display屬性控制每一個(gè)tab的顯示
以下是圖片示例:
以下是代碼部分:
<!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'> <script src='http://www.cgvv.com.cn/bcjs/jquery-3.4.1.min.js'></script> <title>tab功能實(shí)現(xiàn)</title> <style> body { margin: 0 auto; } .tab-menu { width: 100%; background: #F5F6F8; display: flex; } .tab-menu-span { width: 150px; height: 50px; line-height: 50px; cursor: pointer; text-align: center; } .tab-menu-span-hover { background: #0054A7; color: #ffffff; } .tab-list { position: relative; } .tab-list-span { padding: 20px 10px; height: 200px; position: absolute; top: 0; display: none; } </style></head> <body> <div class='tab-class'> <!-- 菜單 --> <div class='tab-menu'> <div class='tab-menu-span'> 菜單一 </div> <div class='tab-menu-span'> 菜單二 </div> <div class='tab-menu-span'> 菜單三 </div> </div> <!-- tab list --> <div class='tab-list'> <div class='tab-list-span'> 1、金子啊,你是多么神奇。你可以使老的變成少的,丑的變成美的,黑的變成白的,錯(cuò)的變成對(duì)的……</br> 2、玫瑰是美的,但更美的是它包含的香味。</br> 3、戀愛的人去赴他情人的約會(huì),像一個(gè)放學(xué)歸來的兒童;可是當(dāng)他和情人分別的時(shí)候,卻上學(xué)去一般滿臉懊喪。</br> </div> <div class='tab-list-span'> 4、時(shí)間會(huì)刺破青春表面的彩飾,會(huì)在美人的額上掘深溝淺槽;會(huì)吃掉稀世之珍!天生麗質(zhì),什么都逃不過他那橫掃的鐮刀。</br> 5、笨蛋自以為聰明,聰明人才知道自己是笨蛋。</br> 6、對(duì)眾人一視同仁,對(duì)少數(shù)人推心置腹,對(duì)任何人不要虧負(fù)。 </br> </div> <div class='tab-list-span'> 7、希望者,思想之母也。</br> 8、草率的婚姻少美滿。</br> 9、無數(shù)人事的變化孕育在時(shí)間的胚胎里。</br> 10、要是不能把握時(shí)機(jī),就要終身蹉跎,一事無成。</br> </div> </div> </div> <script> $(’.tab-menu-span:first’).addClass(’tab-menu-span-hover’); $(’.tab-list-span:first’).css(’display’, ’block’); $(’.tab-menu-span’).hover(function () { $(this).addClass(’tab-menu-span-hover’); $(this).siblings().removeClass(’tab-menu-span-hover’); $(’.tab-list-span’).css(’display’, ’none’); $(’.tab-list-span’).eq($(this).index()).css(’display’, ’block’); }) </script></body> </html>
如果大家還想深入學(xué)習(xí),可以點(diǎn)擊兩個(gè)精彩的專題:
javascript選項(xiàng)卡操作方法匯總
jquery選項(xiàng)卡操作方法匯總
以上就是為大家總結(jié)的tab切換實(shí)現(xiàn)方法,希望對(duì)大家的學(xué)習(xí)有所幫助,順著這個(gè)思路動(dòng)手制作自己tab切換特效。
相關(guān)文章:
1. html中的form不提交(排除)某些input 原創(chuàng)2. ASP動(dòng)態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗(yàn)分享3. ASP常用日期格式化函數(shù) FormatDate()4. CSS3實(shí)現(xiàn)動(dòng)態(tài)翻牌效果 仿百度貼吧3D翻牌一次動(dòng)畫特效5. asp.net core項(xiàng)目授權(quán)流程詳解6. XMLHTTP資料7. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式8. CSS3中Transition屬性詳解以及示例分享9. jsp文件下載功能實(shí)現(xiàn)代碼10. 開發(fā)效率翻倍的Web API使用技巧
