这篇文章将为大家详细讲解有关利用vue怎么实现一个tab栏切换二选一功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
<template>
<div id="app">
<div class="tabWrap">
<!-- 这个结构是tab导航,并给其绑定对应的点击事件,在点击事件的回调中
去控制对应内容的显示隐藏和样式的修改即:tab的切换-->
<div class="tabNav">
<div class="navOne" @click="tabOne">tab1</div>
<div class="navTwo" @click="tabTwo">tab2</div>
</div>
<!-- 这个结构是tab导航对应的内容 -->
<div class="tabContent">
<!-- 通过v-show控制隐藏,同一时刻隐藏一个显示一个,就实现了tab栏的切换效果了 -->
<div class="navOneBox" v-show="showTabOne">我是切换1</div>
<div class="navTwoBox" v-show="showTabTwo">i am tab2</div>
</div>
</div>
</div>
</template>
js部分
<script>
export default {
name: "app",
data() {
return {
showTabOne: true, // 二选一tab切换
showTabTwo: false, // 二选一tab切换
};
},
methods: {
// 二选一tab栏切换
tabOne() {
/*
点击tab1的时候,让tab1显示,tab2隐藏,即showTabOne为true,showTabTwo为false
同时修改tab1的样式使其"高亮",注意不要忘了修改tab2的样式,使其"不高亮"。
点击tab2的时候,也是同理。
*/
this.showTabOne = true;
this.showTabTwo = false;
document.querySelector(".navOne").style.backgroundColor = "#fff";
document.querySelector(".navTwo").style.backgroundColor = "#e3e3e3";
document.querySelector(".navOne").style.color = "#3985EC";
document.querySelector(".navTwo").style.color = "#80868D";
},
// 二选一tab栏切换
tabTwo() {
this.showTabTwo = true;
this.showTabOne = false;
document.querySelector(".navOne").style.backgroundColor = "#e3e3e3";
document.querySelector(".navTwo").style.backgroundColor = "#fff";
document.querySelector(".navTwo").style.color = "#3985EC";
document.querySelector(".navOne").style.color = "#80868D";
},
},
};
</script>
css部分
<style lang="less">
.tabNav {
width: 126px;
height: 30px;
border-radius: 2px;
background-color: #e3e3e3;
display: flex;
align-items: center;
justify-content: space-evenly;
.navOne {
width: 60px;
height: 26px;
border-radius: 2px;
background-color: #fff;
color: #3985ec;
font-size: 14px;
font-weight: 500;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
}
.navTwo {
width: 60px;
height: 26px;
color: #80868d;
border-radius: 2px;
font-size: 14px;
font-weight: 500;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
}
}
.tabContent {
margin-top: 8px;
.navOneBox {
background-color: #bfa;
}
.navTwoBox {
background-color: #baf;
}
}
</style>
关于利用vue怎么实现一个tab栏切换二选一功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。