Vue two-choice tab bar switching new approach

Vue two-choice tab bar switching new approach

Problem Description

When we are working on a project, sometimes we need to make some tab bar switching effects. Some have two tabs, some have three tabs, and some even have five, six, seven or eight tabs. Normally we just use the tab component of Ele.me, but sometimes when we are free, we write one to switch between two tabs, that is, a two-choice effect. Enough of small talk, here are the dynamic renderings

This case is suitable for two tabs (three tabs can be written in the same way as mine. If there are four or five tabs, it will be faster to use the Ele.me component)

The code is as follows

HTML Part

<template>
 <div id="app">
  <div class="tabWrap">
   <!-- This structure is a tab navigation, and the corresponding click event is bound to it. In the callback of the click event, the display and hiding of the corresponding content and the modification of the style are controlled, that is, the switching of the tab-->
   <div class="tabNav">
    <div class="navOne" @click="tabOne">tab1</div>
    <div class="navTwo" @click="tabTwo">tab2</div>
   </div>
   <!-- This structure is the content corresponding to the tab navigation-->
   <div class="tabContent">
    <!-- Use v-show to control hiding, hiding one and showing one at the same time, and the tab bar switching effect is achieved-->
    <div class="navOneBox" v-show="showTabOne">I am switching 1</div>
    <div class="navTwoBox" v-show="showTabTwo">i am tab2</div>
   </div>
  </div>
 </div>
</template>

js part

<script>
export default {
 name: "app",
 data() {
  return {
   showTabOne: true, // Choose one of the two tabs to switch showTabTwo: false, // Choose one of the two tabs to switch };
 },
 methods: {
  //Choose one of the two tab bar switches tabOne() {
   /*
    When you click tab1, make tab1 visible and tab2 hidden, that is, showTabOne is true and showTabTwo is false
    At the same time, modify the style of tab1 to make it "highlighted", and be careful not to forget to modify the style of tab2 to make it "unhighlighted".
    The same applies when you click 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";
  },
  //Choose one of the two tab bar switching 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 part

<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>

This is the end of this article about the new implementation of Vue's two-choice tab bar switching. For more relevant Vue tab bar switching content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue realizes the highlight effect of tab bar click
  • Realize the function of switching content in the tab bar and refreshing data in real time based on Vue

<<:  Detailed explanation of the "/" problem when proxy_pass forwards according to the path path

>>:  MySQL Daemon failed to start error solution

Recommend

Detailed explanation of the this pointing problem in JavaScript

Summarize Global environment ➡️ window Normal fun...

Introduction to user management under Linux system

Table of contents 1. The significance of users an...

MySQL learning to create and operate databases and table DDL for beginners

Table of contents 1. Operate the database 1.1 Cre...

How to modify server uuid in Mysql

Source of the problem: If the slave server is the...

MySQL 5.7.17 installation and configuration method graphic tutorial under win7

I would like to share with you the graphic tutori...

Vue uses better-scroll to achieve horizontal scrolling method example

1. Implementation principle of scrolling The scro...

How to call a piece of HTML code together on multiple HTML pages

Method 1: Use script method: Create a common head...

Implementing custom radio and check box functions with pure CSS

1. Achieve the effect 2 Knowledge Points 2.1 <...

js to implement verification code interference (dynamic)

This article example shares the specific code of ...

Get / delete method to pass array parameters in Vue

When the front-end and back-end interact, sometim...

js canvas realizes slider verification

This article example shares the specific code of ...

Linux file and user management practice

1. Display the files or directories in the /etc d...

Analysis of the advantages and disadvantages of MySQL stored procedures

MySQL version 5.0 began to support stored procedu...

Linux hardware configuration command example

Hardware View Commands system # uname -a # View k...

Detailed explanation of the principles of Vue's responsive system

Table of contents The basic principles of Vue'...