Simple implementation of carousel graph with html + js

Keywords: Javascript

Hey, I just saw how to realize the search box I wrote yesterday and got two likes. I'm really happy. Thank you for your love.

Cough, back to the point. What we want to share today is how to use html + js to implement the carousel graph. Let's first look at the effect:

Figure 1

Figure two

Of course, it's more than that. I'll improve it when I have time

The implementation ideas are as follows:

  1. Set the display property of all pictures to none
  2. Set an index to mark the acquired image
  3. Set a timer to automatically increase the index every other period of time and change the display property of the corresponding picture to block

Attach Code:

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <title>Implementation of the carousel chart</title>
    <style type="text/css">

        /*Title Style*/
            text-align: center;
            font-size: 25px;
            color: cadetblue;
            font-family: fantasy;


            border-top: 2px solid cadetblue;
            width: 50%;
            height: 500px;
            margin: 0 auto;


        .imgBox img{
            width: 60%;
            height: 300px;
            margin: 0 auto;
            padding-top: 30px;


            display: block;

            display: none;

            display: none;
<p>Picture carousel</p>
<div class="imgBox">
    <img class="img-slide img1" src="images/1.jpg" alt="1">
    <img class="img-slide img2" src="images/2.jpg" alt="2">
    <img class="img-slide img3" src="images/3.jpg" alt="3">
<script type="text/javascript">
    var index=0;
    //Change picture
    function ChangeImg() {
        var a=document.getElementsByClassName("img-slide");
        if(index>=a.length) index=0;
        for(var i=0;i<a.length;i++){
    //Set the timer to switch one picture every two seconds

If you think what I wrote is good, please give me a compliment~

Posted by Rebelrebellious on Wed, 04 Dec 2019 01:36:44 -0800