Native javascript+CSS to achieve the effect of carousel

Native javascript+CSS to achieve the effect of carousel

This article uses javascript+CSS to implement the specific code of the carousel effect for your reference. The specific content is as follows

1.html

<ul id="banner" ></ul>

2.css

ul{
    list-style:none;
    position: absolute;
    padding: 0;
    left: 0;
    right: 0;
    bottom: 0;
    top:0;
    margin:auto;
    width: 800px;
    height:200px;
}

3.js

//Generate a carousel export function generateBanner(){
  let sz = new Array();
  let cur_ul = document.getElementById('banner');
  const recommends = this.recommends;

  let timer = setInterval(getNextLi, 3000);

  //Generate the carousel image li
  for (let i = 0; i < recommends.length; i++) {
    //Generate tags let cur_li = document.createElement("li");
    let cur_img = document.createElement("img");
    //Add attributes cur_img.src = recommends[i].pic;
    //Add style cur_li.style.position = 'absolute';
    cur_li.style.left = '0px';
    cur_li.style.transitionDuration = '0.4s';
    cur_li.style.cursor="pointer";

    //ul total width 800, showing one complete 400px and two incomplete 200px
    cur_img.style.width = '125px';
    cur_img.style.height = "100px";

    //Append child element cur_li.appendChild(cur_img);
    cur_ul.appendChild(cur_li);

    //Put all into array for easy operation sz.push(cur_li);
  }

  //Generate two icons generateAngleIcons();

  //Use the last three pictures to display let len ​​= sz.length - 1;
  //The third from the last showThreeLi();

  //Get the next li display and put the first one at the end of the array function getNextLi() {
    const li = sz[0];
    sz = sz.slice(1);
    sz.push(li);

    //All li are restored for (let i = 0; i < sz.length; i++) {
      //All li are restored to their original size sz[i].style.transform = "scale(1)";
      sz[i].style.left = "0px";
      //li covers from small to large sz[i].style.zIndex = i;

      //Hide all sz[i].style.display = "none";
    }
    //Show the last three pictures showThreeLi();

  }

  //Show the last three pictures function showThreeLi() {
    sz[len - 2].style.left = "0px";
    //The second to last picture sz[len - 1].style.left = "120px";
    sz[len - 1].style.zIndex = 100;
    sz[len - 1].style.transform = "scale(1.3)";
    //The last one sz[len].style.left = "230px";

    //Display sz[len - 2].style.display = "block";
    sz[len - 1].style.display = "block";
    sz[len].style.display = "block";
  }


  function generateAngleIcons(){
    const icons = new Array();

    for (let i = 0; i < 2; i++) {
      //Generate icon li
      let cur_li = document.createElement("li");
      //Add style cur_li.style.position = 'absolute';
      cur_li.style.top = '0px';
      cur_li.style.bottom = '0px';
      cur_li.style.margin = "auto";
      cur_li.style.paddingTop="100px";
      cur_li.style.paddingBottom="100px";
      cur_li.style.zIndex = 20;
      icons.push(cur_li);
    }

    icons[0].style.left = '0px';
    icons[1].style.right = '0px';
    icons[0].innerHTML = '<i class="angle left icon"></i>'
    icons[1].innerHTML = '<i class="angle right icon"></i>'
    cur_ul.appendChild(icons[1]);
    cur_ul.appendChild(icons[0]);
  }
}

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+css+HTML to implement mobile carousel (including source code)
  • JS+css3 to implement slideshow carousel
  • JS+CSS to implement 3D cutting carousel
  • Use html+js+css to achieve page carousel effect (example explanation)
  • Sample code for implementing carousel with native js
  • js to implement the complete code of the carousel
  • 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
  • js+css to achieve card carousel effect

<<:  How to Install Oracle Java 14 on Ubuntu Linux

>>:  MySQL trigger trigger add, delete, modify and query operation example

Recommend

How to check disk usage in Linux

1. Use the df command to view the overall disk us...

Mysql solves the database N+1 query problem

Introduction In orm frameworks, such as hibernate...

Example of how to build a Harbor public repository with Docker

The previous blog post talked about the Registry ...

echars 3D map solution for custom colors of regions

Table of contents question extend Solving the pro...

Basic knowledge of MySQL database

Table of contents 1. Understanding Databases 1.1 ...

Linux Check the installation location of the software simple method

1. Check the software installation path: There is...

Complete steps to configure a static IP address for a Linux virtual machine

Preface In many cases, we will use virtual machin...

Reflection and Proxy in Front-end JavaScript

Table of contents 1. What is reflection? 2. Refle...

5 JavaScript Ways to Flatten Arrays

Table of contents 1. Concept of array flattening ...

CentOS7 uses rpm to install MySQL 5.7 tutorial diagram

1. Download 4 rpm packages mysql-community-client...

Detailed explanation of Linux rpm and yum commands and usage

RPM package management A packaging and installati...

How to set an alias for a custom path in Vue

How to configure custom path aliases in Vue In ou...

XHTML introductory tutorial: text formatting and special characters

<br />This section introduces how to impleme...