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

Solve the problem of black screen when starting VMware virtual machine

# Adjust VMware hard disk boot priority Step 1: E...

How to implement batch deletion of large amounts of data in MySQL large tables

The question is referenced from: https://www.zhih...

MySQL implements a solution similar to Oracle sequence

MySQL implements Oracle-like sequences Oracle gen...

How to install MySQL for beginners (proven effective)

1. Software Download MySQL download and installat...

Simple implementation method of Linux process monitoring and automatic restart

Purpose: Under Linux, the server program may be d...

Install MySQL5.5 database in CentOS7 environment

Table of contents 1. Check whether MySQL has been...

Detailed explanation of Vue slot

1. Function : Allows the parent component to inse...

Detailed explanation of nginx proxy_cache cache configuration

Preface: Due to my work, I am involved in the fie...

Mini Program to Implement Paging Effect

This article example shares the specific code for...

Detailed explanation of the adaptive adaptation problem of Vue mobile terminal

1. Create a project with vue ui 2. Select basic c...

Detailed explanation of the execution process of mysql update statement

There was an article about the execution process ...