Vue實現(xiàn)簡單選項卡功能
更新時間:2022年03月01日 11:44:41 作者:林飛的夢囈
這篇文章主要為大家詳細介紹了Vue實現(xiàn)簡單選項卡功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Vue實現(xiàn)簡單選項卡的具體代碼,供大家參考,具體內(nèi)容如下
vue-tab-demo
App.vue
<template> ? <div id="app"> ? ? <Tab/> ? </div> </template> <script> import Tab from './components/Tab' export default { ? name: 'App', ? components: { ? ? Tab ? } } </script> <style> ? ul, li { ? ? list-style: none; ? } ? .clearfix { ? ? zoom: 1; ? } ? .clearfix:after { ? ? display: block; ? ? content: ''; ? ? clear: both; ? } </style>
先布局,寫好樣式
Tab.vue
<template> <div id="tab"> ? <div class="tab-bar clearfix"> ? ? <a href="javascript:;">HTML</a> ? ? <a href="javascript:;">CSS</a> ? ? <a href="javascript:;">JavaScript</a> ? ? <a href="javascript:;" class="active">Vue</a> ? </div> ? <div class="tab-con"> ? ? <div>HTML</div> ? ? <div>CSS</div> ? ? <div>JavaScript</div> ? ? <div class="light">Vue</div> ? </div> </div> </template> <script> export default { ? ? data () { ? ? ? return { ? ? ? } ? ? }, ? ? methods: { ? ? } } </script> <style scoped> #tab { ? width: 400px; ? border: 1px solid #ccc; ? margin: 60px auto 0; } .tab-bar { ? width: 400px; ? background-color: #ccc; } .tab-bar a { ? float: left; ? width: 100px; ? height: 40px; ? line-height: 40px; ? text-align: center; ? text-decoration: none; ? color: #000; } .tab-con div { ? text-align: left; ? height: 100px; ? display: none; } .tab-bar .active { ? background-color: #0099ff; } .tab-con .light { ? display: block; } </style>
渲染數(shù)據(jù)后,上面Tab.vue修改后如下:
<template> ? <div id="tab"> ? ? <div class="tab-bar clearfix"> ? ? ? <a href="javascript:;" ? ? ? ? ?@click="tab(index)" ? ? ? ? ?v-for="(item,index) in items" ? ? ? ? ?:class="{active : index===curId}" ? ? ? >{{item.item}}</a> ? ? </div> ? ? <div class="tab-con"> ? ? ? <div ? ? ? ? v-show="index===curId" ? ? ? ? v-for="(content, index) in contents" >{{content.content}}</div> ? ? </div> ? </div> </template> <script> ? export default { ? ? data () { ? ? ? return { ? ? ? ? curId: 0, ? ? ? ? items: [ ? ? ? ? ? {item: 'HTML'}, ? ? ? ? ? {item: 'CSS'}, ? ? ? ? ? {item: 'JavaScript'}, ? ? ? ? ? {item: 'Vue'}, ? ? ? ? ], ? ? ? ? contents: [ ? ? ? ? ? {content: 'HTML'}, ? ? ? ? ? {content: 'CSS'}, ? ? ? ? ? {content: 'JavaScript'}, ? ? ? ? ? {content: 'Vue'}, ? ? ? ? ] ? ? ? } ? ? }, ? ? methods: { ? ? ? tab (index) { ? ? ? ? this.curId = index; ? ? ? } ? ? } ? } </script> <style scoped> ? #tab { ? ? width: 400px; ? ? border: 1px solid #ccc; ? ? margin: 60px auto 0; ? } ? .tab-bar { ? ? width: 400px; ? ? background-color: #ccc; ? } ? .tab-bar a { ? ? float: left; ? ? width: 100px; ? ? height: 40px; ? ? line-height: 40px; ? ? text-align: center; ? ? text-decoration: none; ? ? color: #000; ? } ? .tab-bar .active { ? ? background-color: #0099ff; ? } ? .tab-con div { ? ? text-align: left; ? ? height: 100px; ? } </style>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue項目element-ui級聯(lián)選擇器el-cascader回顯的問題及解決
這篇文章主要介紹了vue項目element-ui級聯(lián)選擇器el-cascader回顯的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07vue的axios請求改變content-type為form-data問題
這篇文章主要介紹了vue的axios請求改變content-type為form-data問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09vue props傳值失敗 輸出undefined的解決方法
今天小編就為大家分享一篇vue props傳值失敗 輸出undefined的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09Vue Router動態(tài)路由使用方法總結(jié)
這篇文章主要介紹了Vue Router動態(tài)路由使用方法總結(jié),需要的朋友可以參考下2023-10-10