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

Solution to forgetting the MYSQL database password under MAC

Quick solution for forgetting MYSQL database pass...

Solution for multiple Docker containers not having the same port number

Background In Docker, four containers are created...

How to choose transaction isolation level in MySQL project

introduction Let's start with our content. I ...

Implementation of mysql backup strategy (full backup + incremental backup)

Table of contents Design scenario Technical Point...

MySQL 5.7.21 winx64 installation and configuration method graphic tutorial

This article summarizes the notes for installing ...

Priority analysis of and or queries in MySQL

This may be an issue that is easily overlooked. F...

WeChat applet implements simple calculator function

WeChat applet: Simple calculator, for your refere...

HTML text escape tips

Today I saw a little trick for HTML text escaping ...

Preventing SQL injection in web projects

Table of contents 1. Introduction to SQL Injectio...

A line of CSS code that crashes Chrome

General CSS code will only cause minor issues wit...