Vue implements tab navigation bar and supports left and right sliding function

Vue implements tab navigation bar and supports left and right sliding function

This article mainly introduces: using Vue to implement the tab navigation bar, and using flex layout to achieve left and right sliding, calculating the position of the button, and automatically sliding to display the undisplayed elements when the last one displayed on the first screen is clicked and there are still elements that have not been displayed.

Tab navigation bar layout:

<section class="theme-list">
  <div class="fixed-nav" ref="fixednav">
    <div class="fixed-nav-content">
      <p
        v-for="(item, index) in theme"
        :key="index"
        :class="['tab-title', activeId === index && 'select-tab']"
        @click="changeTab(index, $event)"
      >
        {{ item }}
      </p>
    </div>
  </div>
</section>
theme: ['CSDN Blog', 'Blog Garden', 'Good luck on the college entrance examination', 'Good luck on the high school entrance examination', 'Little Joy', 'Seventieth Anniversary'],
activeId: 0

Navigation bar style code:

.theme-list {
  margin-top: 12px;
}
.fixed-nav {
  overflow-x: scroll;
  -webkit-overflow-scrolling: touch;
}
.fixed-nav-content {
  display: flex;
}
.tab-title {
  padding: 0 13px;
  margin-right: 12px;
  color: #141414;
  border-radius: 13px;
  font-size: 12px;
  flex-shrink: 0;
  height: 0.52rem;
  line-height: 0.52rem;
}

At this point we can implement the following style, and can slide the tab left and right:

insert image description here

It should be noted that flex-shrink : 0 needs to be added in the style code so that the tab will not shrink when the width is greater than the width of the external container.

In this way, our basic tab navigation bar has been implemented. Now let's implement it: when you click "Good luck on the high school entrance examination", the entire tab slides to the left to display the remaining tab elements.

changeTab(id, event) {
  // If the selected one is different from the currently active one if (id !== this.activeId) {
    this.activeId = id;
    // Calculate the position of the current button to see if it needs to be moved const spanLeft = event.clientX; // The left distance of the currently clicked element const divBox = document.querySelector(".select-tab").clientWidth / 2; // Half the width of the clicked element const totalWidths = document.body.clientWidth; // Total screen width const widths = totalWidths / 2; // Half the screen width const spanRight = totalWidths - spanLeft; // The right distance of the element const scrollBox = document.querySelector(".fixed-nav"); // Get the outermost element const scrollL = scrollBox.scrollLeft; // The distance the scroll bar scrolls // Slide when the left or right distance of the element is less than 100 if (spanRight < 100 || spanLeft < 100) {
      scrollBox.scrollLeft = scrollL + (spanLeft - widths) + divBox;
    }
  }
}

This method can realize automatic scrolling of the tab, but there is still a problem: the scroll bar will be displayed when sliding, which is obviously not very beautiful.

/*Define the scroll bar height and width and the background height and width corresponding to the size of the horizontal and vertical scroll bars respectively*/
::-webkit-scrollbar {
  width: 0.01rem;
  opacity: 0;
  display: none;
}
/*Define the inner shadow + rounded corners of the scroll bar track*/
::-webkit-scrollbar-track {
  background-color: #fff;
  opacity: 0;
}
/*Define the inner shadow + rounded corners of the slider*/
::-webkit-scrollbar-thumb {
  width: 0.01rem;
  border-radius: 0.01rem;
  opacity: 0;
}

In this way, a navigation bar is realized, and the style of the navigation bar can be modified according to the company's business!

This is the end of this article about Vue's implementation of a tab navigation bar that supports left and right sliding. For more relevant Vue left and right sliding navigation bar 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 implements top left and right sliding navigation
  • Vue implements left and right sliding effect example code
  • Vue+swiper implements the test question function of sliding left and right
  • Vue uses swiper to switch pictures by sliding left and right
  • Vue uses better-scroll to achieve sliding and left-right linkage
  • Detailed explanation of left and right sliding events on Vue mobile terminal
  • Vue mobile terminal realizes the mobile phone sliding left and right entry animation
  • How to realize left and right sliding effect on mobile terminal in Vue
  • Implementing left and right sliding effect of page switching based on Vue
  • Detailed explanation of the use of Vue's left and right sliding button group component

<<:  Implementing access control and connection restriction based on Nginx

>>:  Explanation of Truncate Table usage

Recommend

Windows 10 is too difficult to use. How to customize your Ubuntu?

Author | Editor Awen | Produced by Tu Min | CSDN ...

MySQL 8.0.22 decompression version installation tutorial (for beginners only)

Table of contents 1. Resource download 2. Unzip t...

The difference between char and varchar in MYSQL

CHAR and VARCHAR types are similar, differing pri...

On good design

<br />For every ten thousand people who answ...

5 ways to make your JavaScript codebase cleaner

Table of contents 1. Use default parameters inste...

Four ways to switch tab pages in VUE

Table of contents 1. Static implementation method...

MySQL View Principle Analysis

Table of contents Updatable Views Performance of ...

Web page header optimization suggestions

Logo optimization: 1.The logo image should be as ...

Docker configuration Alibaba Cloud Container Service operation

Configuring Alibaba Cloud Docker Container Servic...

How to move a red rectangle with the mouse in Linux character terminal

Everything is a file! UNIX has already said it. E...

About 3 common packages of rem adaptation

Preface I wrote an article about rem adaptation b...

...

A simple and in-depth study of async and await in JavaScript

Table of contents 1. Introduction 2. Detailed exp...

VMware kali virtual machine environment configuration method

1|0 Compile the kernel (1) Run the uname -r comma...