js to write the carousel effect

js to write the carousel effect

This article shares the specific code of js to achieve the effect of the carousel map for your reference. The specific content is as follows

1. HTML part

<div id="box">
    <ul>
      <li class="show"><img src="img/1.jpg" alt=""></li>
      <li><img src="img/2.jpg" alt=""></li>
      <li><img src="img/3.jpg" alt=""></li>
      <li><img src="img/4.jpg" alt=""></li>
      <li><img src="img/5.jpg" alt=""></li>
      <li><img src="img/6.jpg" alt=""></li>
    </ul>
    <img src="img/l.png" alt="" id="lef">
    <img src="img/r.png" alt="" id="rig">
    <ol>
      <li class="focus" data-i = "0"></li>
      <li data-i = "1"></li>
      <li data-i = "2"></li>
      <li data-i = "3"></li>
      <li data-i = "4"></li>
      <li data-i = "5"></li>
    </ol>
</div>

2. js part

 <script>
    var liList = document.querySelectorAll("#box ul li")
    var olList = document.querySelectorAll("#box ol li")
    var rig = document.getElementById("rig")
    var lef = document.getElementById("lef")

    var index = 0
    function setLi(){
      for(var i = 0;i <liList.length;i++){
        liList[i].className = ""
        olList[i].className = ""
      }
      liList[index].className = "show"
      olList[index].className = "focus"
    }
    //Rightrig.onclick = function(){
      if(index !== 5){
        index++
      }
      if(index === 5){
        index = 0
    }
    setLi()
  }
  //Left lef.onclick = function(){
      
      if(index !== 0){
        index--
      }
      if(index === 0){
        index = 5
    }
    setLi()
  }
  //Point for(var i = 0;i < olList.length;i++){
    olList[i].onclick = function(){
      index = this.getAttribute("data-i")
      setLi()
    }
  }
  // Timing var autoPlay = setInterval(
    function(){
      rig.click()
    },3000)

  //Move the mouse over to stop playing var box = document.querySelector("div")
  box.onmouseenter = function(){
    clearInterval(autoPlay)
  }

  // Move the mouse away and continue playing box.onmouseleave = function(){
    autoPlay = setInterval(
    function(){
      rig.click()
    },3000)
  }
</script>

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 implements circular carousel
  • Several methods of implementing carousel images in JS
  • Implementing a simple carousel based on JavaScript
  • JavaScript imitates Jingdong carousel effect
  • JavaScript Dom implements the principle and example of carousel
  • Native js to achieve seamless carousel effect
  • js to achieve mobile carousel sliding switch
  • javascript to realize the scroll wheel carousel picture
  • Sample code for a simple seamless scrolling carousel implemented with native Js

<<:  How to implement second-level scheduled tasks with Linux Crontab Shell script

>>:  mysql obtains statistical data within a specified time period

Recommend

Method of iframe adaptation in web responsive layout

Problem <br />In responsive layout, we shou...

Implementation of mysql split function separated by commas

1: Define a stored procedure to separate strings ...

Some common mistakes with MySQL null

According to null-values, the value of null in My...

Design reference WordPress website building success case

Each of these 16 sites is worth reading carefully,...

How to get/calculate the offset of a page element using JavaScript

question By clicking a control, a floating layer ...

Summary of 4 methods of div+css layout to achieve 2-end alignment of css

The div+css layout to achieve 2-end alignment is ...

Detailed explanation of the process of using GPU in Docker

Table of contents Download tf-gpu Build your own ...

A brief discussion on Axios's solution to remove duplicate requests

Table of contents 1. Cancel duplicate requests 2....

Vue implements a movable floating button

This article example shares the specific code of ...

Use of Linux gzip command

1. Command Introduction The gzip (GNU zip) comman...

Example analysis of the usage of the new json field type in mysql5.7

This article uses an example to illustrate the us...

Detailed explanation of viewing and setting SQL Mode in MySQL

Viewing and Setting SQL Mode in MySQL MySQL can r...