JS implements multiple tab switching carousel

JS implements multiple tab switching carousel

Carousel animation can improve the appearance and interactive performance of the page. Next, we will learn to use the basic knowledge of front-end development such as HTML, CSS and Javascript to make a simple carousel.

Introduction to carousel: In a specific module of a website, different pictures can be displayed by clicking the mouse on a computer or moving the mouse in, or sliding the finger on a mobile phone. This module is called a carousel module.

(Welcome to criticize and correct me if I have done anything wrong. If you think it is helpful, please give me a star~)

HTML layout part:

<div id="box">
    <div class="scenery pic">
      <img class="show" src="imgs/s2.jpg" alt="scenery">
      <img src="imgs/s3.jpg" alt="scenery">
      <img src="imgs/s1.jpg" alt="scenery">
      <img src="imgs/s5.jpg" alt="scenery">
      <img src="imgs/s4.jpg" alt="scenery">
    </div>
    <div class="car pic">
      <img src="imgs/animal4.jpg" alt="animal">
      <img src="imgs/animal3.jpg" alt="animal">
      <img src="imgs/animal2.jpg" alt="animal">
      <img src="imgs/animal1.jpg" alt="animal">
    </div>
    <div class="entertainment pic">
      <img src="imgs/entertainment1.jpg" alt="entertainment">
      <img src="imgs/entertainment2.jpg" alt="entertainment">
      <img src="imgs/entertainment3.jpg" alt="entertainment">
      <img src="imgs/entertainment4.jpg" alt="entertainment">
      <img src="imgs/entertainment5.jpg" alt="entertainment">
    </div>
    <div class="leftbar">
      <div class="checked">Landscape</div>
      <div>Famous car</div>
      <div>Entertainment</div>
    </div>
    <div class="bottombar">
 
    </div>
</div>

CSS style part:

/* For the convenience of layout, flex is mostly used in containers */
#box {
      position: relative;
      width: 460px;
      height: 300px;
      margin: 40px auto;
      border: 1px solid rgb(109, 98, 98);
      user-select: none;
    }
    /* Sidebar layout */
    .leftbar {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      position: absolute;
      top: -1px;
      left: -80px;
      width: 80px;
      height: 100%;
      text-align: center;
      font-size: 20px;
      cursor: pointer;
    }
 
    .leftbar div {
      flex: 1;
      line-height: 100px;
      background-color: cadetblue;
      letter-spacing: 5px;
    }
 
    .leftbar div:nth-child(2) {
      border-top: 1px solid #fff;
      border-bottom: 1px solid #fff;
    }
 
    /* Bottom switch button style design*/
    .bottombar {
      display: flex;
      justify-content: space-between;
      position: absolute;
      bottom: -1px;
      right: -1px;
      z-index: 10;
      width: 200px;
      height: 30px;
      cursor: pointer;
    }
 
    .bottombar div {
      flex: 1;
      line-height: 30px;
      background-color: rgb(232, 233, 235, .5);
      text-align: center;
      font-weight: 700;
    }
 
    .bottombar div~div {
      border-left: 1px solid grey;
    }
 
    img {
      position: absolute;
      display: block;
      width: 460px;
      height: 300px;
    }
 
    .show {
      z-index: 5;
    }
 
    .leftbar .checked,
    .bottombar .checked {
      background-color: rgb(241, 5, 5);
    }

JavaScript logic implementation part:

var Box = document.querySelector('#box'), pic = Box.querySelectorAll('.pic'),
    Idx = 0, index = 0, timer = null,
    ltDiv = Box.querySelector('.leftbar'), btDiv = Box.querySelector('.bottombar'),
    Img = Box.querySelectorAll('img');
 
    function createBtBar(len) {//Dynamically create the bottom switch button var str = '';
      for (var i = 0; i < len; i++) {
        str += `<div>${i + 1}</div>`;
      }
      btDiv.innerHTML = str;
      btDiv.children[0].classList.add('checked');
    }
 
    function initialize() {//Page initial status createBtBar(pic[0].children.length);
    }
    initialize();
 
    function clearZindex() {//Reset the positioning level of all images for (var k = 0; k < Img.length; k++) {
        Img[k].classList.remove('show');
      }
    }
 
    ltDiv.addEventListener('click', (e) => {//Sidebar item switchif (e.target.tagName.toLowerCase() === 'div') {
        for (var j = 0; j < ltDiv.children.length; j++) {
          ltDiv.children[j].classList.remove('checked');
        }
 
        clearZindex();
        Idx = 0;
        index = getEleIdx(e.target);
        ltDiv.children[index].classList.add('checked');
        pic[index].children[0].classList.add('show');
        createBtBar(pic[index].children.length);
      }
    });
 
    btDiv.addEventListener('click', (e) => {//Delegate listens to the bottom switch button if (e.target.tagName.toLowerCase() === 'div') {
        function changePic(callback) {
          btDiv.children[Idx].classList.remove('checked');
 
          clearZindex();
          callback && callback();
          btDiv.children[Idx].classList.add('checked');
          pic[index].children[Idx].classList.add('show');
        }
        changePic(function () {
          Idx = getEleIdx(e.target);
        });
      }
    });
 
    function getEleIdx(item) {//Get DOM element index var elements = item.parentNode.children;
      for (var i = 0, len = elements.length; i < len; i++) {
        if (item === elements[i]) {
          return i;
        }
      }
    }
 
    function loopShow() {//loop automatic display clearInterval(timer);
      timer = setInterval(function () {
        pic[index].children[Idx].classList.remove('show');
        btDiv.children[Idx].classList.remove('checked');
        Idx += 1;
        if (Idx < 0 || Idx > pic[index].children.length - 1) {
          Idx = 0;
        }
        pic[index].children[Idx].classList.add('show');
        btDiv.children[Idx].classList.add('checked');
      }, 1000);
    }
 
    loopShow();
 
    Box.addEventListener('mouseover', function () {
      clearInterval(timer); //Move the mouse into the display area to stop the carousel });
    Box.addEventListener('mouseout', function () {
      loopShow(); //Move the mouse out of the display area to automatically rotate });

The specific display effects are as follows:

(Tip: Please prepare the pictures and put them in your own folder~)

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:
  • Native JavaScript to achieve the effect of carousel
  • js to achieve a simple carousel effect
  • JavaScript to implement the most complete code analysis of simple carousel (ES6 object-oriented)
  • JavaScript to implement simple carousel chart most complete code analysis (ES5)
  • js to implement the complete code of the carousel
  • Sample code for implementing carousel with native js
  • js to achieve the effect of supporting mobile phone sliding switch carousel picture example
  • JS carousel diagram implementation simple code
  • js to achieve click left and right buttons to play pictures
  • JavaScript implementation of carousel example

<<:  Solution to the Chinese garbled code problem in the decompressed version of MYSQL

>>:  MySQL loop inserts tens of millions of data

Recommend

Server concurrency estimation formula and calculation method

Recently, I need to stress test the server again....

JavaScript object-oriented implementation of magnifying glass case

This article shares the specific code of JavaScri...

Detailed explanation of angular parent-child component communication

Table of contents APIs used Simple Example person...

Detailed explanation of Vue login and logout

Table of contents Login business process Login fu...

Five ways to traverse JavaScript arrays

Table of contents 1. for loop: basic and simple 2...

How to use dynamic parameters and calculated properties in Vue

1. Dynamic parameters Starting from 2.6.0, you ca...

Installation process of CentOS8 Linux 8.0.1905 (illustration)

As of now, the latest version of CentOS is CentOS...

Introduction to install method in Vue

Table of contents 1. Globally registered componen...

MySql COALESCE function usage code example

COALESCE is a function that refers to each parame...

How to implement h5 input box prompt + normal text box prompt

XML/HTML CodeCopy content to clipboard < input...

jQuery achieves the shutter effect (using li positioning)

This article shares the specific code of jQuery t...

Let's talk about the Vue life cycle in detail

Table of contents Preface 1. Life cycle in Vue2 I...