JavaScript實(shí)現(xiàn)簡(jiǎn)易tab欄切換案例
本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)簡(jiǎn)易tab欄切換效果的具體代碼,供大家參考,具體內(nèi)容如下
1. tab欄-案例1
tab欄分析
li里面的分析
js實(shí)現(xiàn)隱藏與顯示
排他思想:
1)、所有元素全部清除樣式(干掉其他人)
2)、給當(dāng)前元素設(shè)置樣式 (留下我自己)
3)、注意順序不能顛倒,首先干掉其他人,再設(shè)置自己
我的思路:
代碼實(shí)現(xiàn):
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> * { margin: 0; padding: 0; } li { list-style: none; } a { text-decoration: none; color: #666; } .vertical-tab { width: 980px; margin: 100px auto; } .vertical-tab .nav { width: 200px; list-style: none; } .vertical-tab .nav-tabs1 { float: left; border-right: 3px solid #e7e7e7; } .vertical-tab .nav-tabs2 { float: right; border-left: 3px solid #e7e7e7; } .vertical-tab li a { display: block; padding: 10px 20px; text-align: center; text-transform: uppercase; letter-spacing: 1px; font-size: 18px; font-weight: 700; } .vertical-tab .active { color: #198df8; } .vertical-tab .tabs { width: 500px; float: left; } .vertical-tab .tab-content { padding: 10px 20px; text-transform: uppercase; letter-spacing: 1px; } .vertical-tab .tab-content h3 { color: #333; margin: 0 0 10px 0; } .vertical-tab .tab-content p { font-size: 12px; } .vertical-tab .hidden { display: none; } </style> </head> <body> <div class="vertical-tab"> <ul class="nav nav-tabs1"> <li><a href="javascript:;" class="active" index="1">section 1</a></li> <li><a href="javascript:;" index='2'>section 2</a></li> <li><a href="javascript:;" index="3">section 3</a></li> </ul> <div class="tab-content tabs"> <div class="tab-content1"> <h3>section 1</h3> <p>content 1</p> </div> <div class="tab-content1 hidden"> <h3>section 2</h3> <p>content 2</p> </div> <div class="tab-content1 hidden"> <h3>section 3</h3> <p>content 3</p> </div> <div class="tab-content1 hidden"> <h3>section 4</h3> <p>content 4</p> </div> <div class="tab-content1 hidden"> <h3>section 5</h3> <p>content 5</p> </div> <div class="tab-content1 hidden"> <h3>section 6</h3> <p>content 6</p> </div> </div> <ul class="nav nav-tabs2"> <li><a href="javascript:;" index="4">section 4</a></li> <li><a href="javascript:;" index="5">section 5</a></li> <li><a href="javascript:;" index="6">section 6</a></li> </ul> </div> <script> var as = document.querySelectorAll("a") var item = document.querySelectorAll(".tab-content1") console.log(as) // console.log(lis) for (var i = 0; i < as.length; i++) { as[i].addEventListener('click', function() { // 干掉其他人 for (var j = 0; j < as.length; j++) { as[j].className = '' } // 留下自己 this.className = "active" // 顯示內(nèi)容 var index = this.getAttribute('index') console.log(index) // 干掉其他人 for (var i = 0; i < item.length; i++) { item[i].style.display = "none" } // 留下自己 item[index - 1].style.display = "block" }) } </script> </body> </html>
vue實(shí)現(xiàn)
vue實(shí)現(xiàn)起來(lái)相對(duì)簡(jiǎn)單,只需要靈活運(yùn)用v-if和v-for
具體代碼:
<!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>Document</title> <style> * { margin: 0; padding: 0; } .vertical-tab { width: 920px; margin: 100px auto; } .vertical-tab .nav { list-style: none; width: 200px; } .vertical-tab .nav-tabs1 { border-right: 3px solid #e7e7e7; } .vertical-tab .nav-tabs2 { border-left: 3px solid #e7e7e7; } .vertical-tab .nav a { display: block; font-size: 18px; font-weight: 700; text-align: center; letter-spacing: 1px; text-transform: uppercase; padding: 10px 20px; margin: 0 0 1px 0; text-decoration: none; } .vertical-tab .tab-content { color: #555; background-color: #fff; font-size: 15px; letter-spacing: 1px; line-height: 23px; padding: 10px 15px 10px 25px; display: table-cell; position: relative; } .vertical-tab .nav-tabs1 { float: left; } .vertical-tab .tabs { width: 500px; box-sizing: border-box; float: left; } .vertical-tab .tab-content h3 { font-weight: 600; text-transform: uppercase; margin: 0 0 5px 0; } .vertical-tab .nav-tabs2 { float: right; } .tab-content { position: relative; } .tab-content .tab-pane { position: absolute; top: 10px; left: 20px; } .nav li.active a { color: #198df8; background: #fff; border: none; } .fade { opacity: 0; transition: all .3s linear; } .fade.active { opacity: 1; } </style> </head> <body> <div class="vertical-tab" id="app"> <!-- Nav tabs --> <ul class="nav nav-tabs1"> <li v-on:click='change(index,0)' :class='currentIndex==index?"active":""' v-if="index < list.length/2" v-for="(item, index) in list"><a href="#" rel="external nofollow" rel="external nofollow" > {{item.title}} </a></li> </ul> <!-- Tab panes --> <div class="tab-content tabs"> <div class="tab-pane fade" :class='currentIndex==index?"active":""' :key='item.id' v-for='(item, index) in list'> <h3>{{item.title}}</h3> <p>{{item.content}}</p> </div> </div> <!-- Nav tabs --> <ul class="nav nav-tabs2"> <li v-on:click='change(index,1)' :class='currentIndex==index?"active":""' v-if="index >= list.length/2" v-for="(item, index) in list"><a href="#" rel="external nofollow" rel="external nofollow" > {{item.title}} </a></li> </ul> </div> <script type="text/javascript" src="js/vue.js"></script> <script> new Vue({ el: "#app", data: { currentIndex: 0, // 選項(xiàng)卡當(dāng)前的索引 list: [{ id: 1, title: 'Section 1', content: 'content1' }, { id: 2, title: 'Section 2', content: 'content2' }, { id: 3, title: 'Section 3', content: 'content3' }, { id: 4, title: 'Section 4', content: 'content4' }, { id: 5, title: 'Section 5', content: 'content5' }, { id: 6, title: 'Section 6', content: 'content6' }] }, methods: { change(index, flag) { if (flag) { console.log(index) this.currentIndex = index; } else { this.currentIndex = index; } } } }) </script> </body>
如果大家還想深入學(xué)習(xí),可以點(diǎn)擊兩個(gè)精彩的專題:javascript選項(xiàng)卡操作方法匯總 jquery選項(xiàng)卡操作方法匯總
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JavaScript實(shí)現(xiàn)tab欄切換的效果
- JavaScript實(shí)現(xiàn)tab欄切換效果
- JS實(shí)現(xiàn)簡(jiǎn)單Tab欄切換案例
- js tab欄切換代碼實(shí)例解析
- JavaScript TAB欄切換效果的示例
- js實(shí)現(xiàn)tab欄切換效果
- js實(shí)現(xiàn)tab欄切換制作
- JavaScript實(shí)現(xiàn)Tab欄切換特效
- JS實(shí)現(xiàn)Tab欄切換的兩種方式案例詳解
- 如何通過(guò)JavaScript、css、H5實(shí)現(xiàn)簡(jiǎn)單的tab欄切換和復(fù)用功能
相關(guān)文章
js動(dòng)態(tài)添加input按鈕并給按鈕增加onclick的函數(shù)事件(帶參數(shù))完整實(shí)例
這篇文章主要介紹了js動(dòng)態(tài)添加input按鈕并給按鈕增加onclick的函數(shù)事件,結(jié)合完整實(shí)例形式分析了javascript頁(yè)面元素屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2023-07-07JS+canvas五子棋人機(jī)對(duì)戰(zhàn)實(shí)現(xiàn)步驟詳解
這篇文章主要介紹了JS+canvas五子棋人機(jī)對(duì)戰(zhàn)實(shí)現(xiàn)步驟詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06javascript之通用簡(jiǎn)單的table選項(xiàng)卡實(shí)現(xiàn)(二)
上篇中的選項(xiàng)卡存在這樣的問(wèn)題:把邏輯封裝在table.js中,不夠靈活,也就是說(shuō)如果某個(gè)選項(xiàng)卡是實(shí)現(xiàn)異步請(qǐng)求或者跳轉(zhuǎn),而非div的顯隱切換,那么就得修過(guò)table.js來(lái)達(dá)到目的,顯然不是我所需要的。2010-05-05javascript通過(guò)navigator.userAgent識(shí)別各種瀏覽器
識(shí)別各種瀏覽器的實(shí)現(xiàn)原理是根據(jù)navigator.userAgent返回值識(shí)別,下面有個(gè)不錯(cuò)的示例,感興趣的朋友可以參考下2013-10-10JavaScript中Array.from()的超全用法詳解
Array.from方法用于將兩類對(duì)象轉(zhuǎn)為真正的數(shù)組:類似數(shù)組的對(duì)象(array-like?object)和可遍歷(iterable)的對(duì)象(包括?ES6?新增的數(shù)據(jù)結(jié)構(gòu)?Set?和?Map),別忘記就來(lái)講講他的具體用法吧2023-03-03JS實(shí)現(xiàn)Fisheye效果動(dòng)感放大菜單代碼
這篇文章主要介紹了JS實(shí)現(xiàn)Fisheye效果動(dòng)感放大菜單代碼,涉及JavaScript事假監(jiān)聽機(jī)制及定時(shí)函數(shù)等相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10