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

How to add website icon?

The first step is to prepare an icon making softwa...

Solution to the problem that docker nginx cannot be accessed after running

## 1 I'm learning docker deployment recently,...

How to remount the data disk after initializing the system disk in Linux

Remount the data disk after initializing the syst...

Basic structure of HTML documents (basic knowledge of making web pages)

HTML operation principle: 1. Local operation: ope...

Detailed explanation of how to adjust Linux command history

The bash history command in Linux system helps to...

How to use Typescript to encapsulate local storage

Table of contents Preface Local storage usage sce...

Installation method of MySQL 5.7.18 decompressed version under Win7x64

Related reading: Solve the problem that the servi...

MySql index detailed introduction and correct use method

MySql index detailed introduction and correct use...

VScode Remote SSH remote editing and debugging code

The latest Insider version of Visual Studio Code ...

Vue3 (V) Details of integrating HTTP library axios

Table of contents 1. Install axios 2. Use of axio...

How to solve the mysql error 1033 Incorrect information in file: 'xxx.frm'

Problem Description 1. Database of the collection...