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

Example of using negative margin to achieve average layout in CSS

For evenly distributed layouts, we generally use ...

Detailed explanation of CSS weight value (cascading) examples

•There are many selectors in CSS. What will happe...

Detailed explanation of Nginx configuration file

The main configuration file of Nginx is nginx.con...

A bug fix for Tomcat's automatic shutdown

Preface Recently, a Java EE web project that has ...

In-depth analysis of MySQL lock blocking

In daily maintenance, threads are often blocked, ...

MySql5.7.21 installation points record notes

The downloaded version is the Zip decompression v...

Detailed explanation of the solution to Tomcat's 404 error

The 404 problem occurs in the Tomcat test. The pr...

Detailed explanation of the basic usage of the img image tag in HTML/XHTML

The image tag is used to display an image in a we...

Application and implementation of data cache mechanism for small programs

Mini Program Data Cache Related Knowledge Data ca...

Vue implements anchor positioning function

This article example shares the specific code of ...

How to use Volume to transfer files between host and Docker container

I have previously written an article about file t...

Disable IE Image Toolbar

I just tried it on IE6, and it does show the toolb...

How to install Composer in Linux

1. Download the installation script - composer-se...

How to use Greek letters in HTML pages

Greek letters are a very commonly used series of ...