JavaScript implements Tab bar switching effects

JavaScript implements Tab bar switching effects

Here is a case that front-end developers must know, which is the switching effect of a Tab bar. In addition to the Tab bar itself being switched by clicking, the Tab itself also determines the display of the content section below it.

Operation effect display:

As shown in the picture above, actually, just click on the Tab bar above and the style of the Tab bar itself will be modified, and then the content block below it will also display the corresponding content. Such effects and functions are widely used in the front-end, so it can be said that this is a must for the front-end. Without further ado, here's the code:

(I won’t post the CSS style code here, you can set it according to your needs, and just use float layout)

The structure of HTML:

<div class="tab">
        <div class="tab_list">
            <ul>
                <li class="current">Product Introduction</li><!-- The first li is selected by default, current determines the style of red background and white text-->
                <li>Specifications and packaging</li>
                <li>After-sales guarantee</li>
                <li>Product Reviews (50,000)</li>
                <li>Mobile Community</li>
            </ul>
        </div>
        <div class="tab_con">
            <div class="item" style="display: block;"><!-- This item is displayed by default, because the display value of all items is none -->
                Product introduction module content</div>
            <div class="item">
                Specifications and packaging module content</div>
            <div class="item">
                After-sales guarantee module content</div>
            <div class="item">
                Product reviews (50000) module content</div>
            <div class="item">
                Mobile community module content</div>
 
        </div>
</div>

Here is the JS code:

<script>
        // Business requirement: Click on the tab bar. The clicked tab bar has a different style, and the div below it should also be switched to achieve the effect of changing the content var tab_list = document.querySelector('.tab_list');
        var lis = tab_list.querySelectorAll('li');
        var tabs = document.querySelectorAll('.item');
        for (var i = 0; i < lis.length; i++) {
            lis[i].setAttribute('data-index', i); // Add a data-index custom attribute to each li, the value is its own subscript in lis, this value mainly allows us to determine which li we are currently clicking on, so as to help us modify the display value of the corresponding div later lis[i].onclick = function () {
                // 1. The first step is to use the exclusive idea to assign a class attribute to the user who clicks it. Note that the class values ​​of other unclicked items need to be left empty. This is the exclusive idea of ​​killing others and leaving me alone for (var i = 0; i <lis.length; i++) {
                    lis[i].className = ''; // Use a loop to empty the class name of everyone (including yourself)}
                // Then modify the class name for yourself and leave it to myself this.className = 'current'; // The CSS style of the current class name has been written// Then the tab bar style above needs to be processed. The second step is to display the corresponding subordinate div of whoever is clicked, and hide the others that have not been clicked. Here it is mainly necessary to first know who the user clicked, and then set the display of the object div to block. Still need to use exclusive thinking to do it // Step 2: Modify the display attribute value of div according to the click var index = this.getAttribute('index'); // Get the index of the currently clicked li
                for (var i = 0; i < lis.length; i++) { // Use a loop to set the display of each item to none, thus killing everyone tabs[i].style.display = 'none';
                }
                // In tabs, lock the item corresponding to the index of li and change its display value to block, leaving me alone tabs[index].style.display = 'block';
            }
        }
</script>

Here are the implementation steps and ideas (detailed analysis has been given in the JS code): First of all, the functions are roughly divided into two steps: first, the style of the li tag is modified, that is, the style of the li clicked by the user will change to white text on a red background, while other li tags will use the default style of black text on a gray background; second, the content of the text module below must also change if the above style is modified. In fact, divs corresponding to the li tags are placed below to hold the text content, but their display is determined by the li tags in the Tab bar. Therefore, to realize that the text of the div changes with the li tags, we need to know which li tag the current user has clicked. The method used here is lis[i].setAttribute('index', i), that is, use a loop to add a custom attribute index value equal to the index of lis (the array of all li tags) to each li tag. Based on the value of index, we can know which li the user has clicked, so we can decide which div to display. (It is recommended to read the code and combine it with comments for better understanding)

Note: Let me talk about the " exclusive idea " here, that is, element objects such as lists or tables can generally be uniformly obtained and stored in an array. When we want to control the "difference" between the currently selected element and other unselected elements, we often design the "exclusive idea", which is mainly implemented in two steps: 1. Use a loop to traverse all these elements (including the selected element). This step is mainly to make all the elements "the same" and no one is different, and then add the style or function we want to the element we selected (that is, leave myself). These two steps combined achieve the effect of making whoever is selected "different".

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • JavaScript to achieve simple tab bar switching case
  • JavaScript to implement simple tab bar switching content bar
  • Example of JavaScript TAB bar switching effect
  • js to achieve tab bar switching effect
  • JavaScript to achieve tab bar switching effect
  • js tab bar switching code example analysis
  • JavaScript to achieve the effect of tab bar switching

<<:  Linux CentOS 6.5 Uninstall, tar and install MySQL tutorial

>>:  Detailed explanation of MySQL master-slave inconsistency and solutions

Recommend

Why is it not recommended to use an empty string as a className in Vue?

Table of contents Compare the empty string '&...

MySQL 8.0.12 Installation and Configuration Tutorial

This article records the detailed tutorial for in...

Analysis of Linux kernel scheduler source code initialization

Table of contents 1. Introduction 2. Basic Concep...

Summary of important mysql log files

Author: Ding Yi Source: https://chengxuzhixin.com...

Three ways to create a gray effect on website images

I’ve always preferred grayscale images because I t...

Use of Docker image storage overlayfs

1. Overview The image in Docker is designed in la...

js implements mouse switching pictures (without timer)

This article example shares the specific code of ...

Detailed explanation of MySQL redo log (redo log) and rollback log (undo logo)

Preface: The previous article described several c...

Docker container log analysis

View container logs First, use docker run -it --r...

Solution to many line breaks and carriage returns in MySQL data

Table of contents Find the problem 1. How to remo...

A brief discussion on tags in HTML

0. What is a tag? XML/HTML CodeCopy content to cl...

How to use cursor triggers in MySQL

cursor The set of rows returned by the select que...