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

VMware Workstation 14 Pro installation and activation graphic tutorial

This article shares the installation and activati...

JavaScript Advanced Programming: Variables and Scope

Table of contents 1. Original value and reference...

In-depth explanation of slots and filters in Vue

Table of contents Slots What are slots? Slot Cont...

React Diff Principle In-depth Analysis

Table of contents Diffing Algorithm Layer-by-laye...

Several commonly used single-page application website sharing

CSS3Please Take a look at this website yourself, ...

Dealing with the problem of notes details turning gray on web pages

1. In IE, if relative positioning is used, that is...

Docker data management and network communication usage

You can install Docker and perform simple operati...

Detailed explanation of various join summaries of SQL

SQL Left Join, Right Join, Inner Join, and Natura...

MySQL scheduled full database backup

Table of contents 1. MySQL data backup 1.1, mysql...

Detailed explanation of transactions and indexes in MySQL database

Table of contents 1. Affairs: Four major characte...

Steps for encapsulating element-ui pop-up components

Encapsulate el-dialog as a component When we use ...

Vue.js implements the code of clicking the icon to zoom in and leaving

The previous article introduced how Vue can reali...

Javascript operation mechanism Event Loop

Table of contents 1. Four concepts 1. JavaScript ...