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

Use tomcat to deploy SpringBoot war package in centos environment

Prepare war package 1. Prepare the existing Sprin...

Solution to MySQL Chinese garbled characters problem

1. The Chinese garbled characters appear in MySQL...

Docker uses nextcloud to build a private Baidu cloud disk

Suddenly, I needed to build a private service for...

Using Docker+jenkins+python3 environment to build a super detailed tutorial

Preface: After the automation is written, it need...

Several methods of deploying multiple front-end projects with nginx

I have summarized 3 methods to deploy multiple fr...

Tutorial on installing Apache 2.4.41 on Windows 10

1. Apache 2.4.41 installation and configuration T...

Sample code for achieving small triangle border effect with pure CSS3+DIV

The specific code is as follows: The html code is...

Two usages of iFrame tags in HTML

I have been working on a project recently - Budou...

How to successfully retrieve VMware Esxi root password after forgetting it

Prepare a CentOS6 installation disk (any version)...

MySQL 8.0.11 installation and configuration method graphic tutorial (win10)

This article records the installation and configu...

How to create a stored procedure in MySQL and add records in a loop

This article uses an example to describe how to c...

A brief introduction to bionic design in Internet web design

When it comes to bionic design, many people will t...