js native carousel plug-in production

js native carousel plug-in production

This article shares the specific code for the js native carousel plug-in for your reference. The specific content is as follows

When calling, you only need to write a DIV

Configuration content of the called js part:

Pass in the position (div) where the carousel image needs to be displayed

Incoming images and clickable links

Without further ado, let’s get started with the code.

HTML

<div id="banner"></div>

The <script> in the HTML document contains the configuration of the carousel. There are two parameters. The first one is the DIV that needs to be passed in (the area where the carousel is displayed). The second parameter is an array. The elements in the array are objects. The first attribute imgUrl in the object represents the image, and the second attribute link represents the jump link.

An array element is a picture, unlimited number, adaptive

<script>
        bannerArea(document.getElementById("banner"),[
            {imgUrl:"picture",
            link:"Jump link"
        },
            {imgUrl:"picture",
            link:"Jump link"
        },
            {imgUrl:"picture",
            link:"Jump link"
        }
        ])
</script>

JS plugin content

// Two parameters, the first is the slide area, the second is the configuration function bannerArea(areaDom, options) {
    var imgArea = document.createElement("div");
    var numberArea = document.createElement("div");
    var curIndex = 0; //The first slideshow var changeTimer = null;
    var changeDuration = 1000; //interval var timer = null;
    
    initImg();//Create an area to display the image initNumber();//Create an area to display the badge setStatus();//Set the status autoChange();//Automatically switch //The following is a local function //Create an image and set the style function initImg() {
        imgArea.style.width = "100%";
        imgArea.style.height = "100%";
        imgArea.style.display = "flex";
        imgArea.style.overflow = "hidden";
        for (let i = 0; i < options.length; i++) {
            var obj = options[i];
            var img = document.createElement("img");
            img.src = obj.imgUrl;
            img.style.width = "100%";
            img.style.height = "100%";
            img.style.margin = "0";
            img.addEventListener("click", function () {
                location.href = options[i].link;
            })
            imgArea.appendChild(img);
        }
        imgArea.addEventListener("mouseenter", function () {
            clearInterval(changeTimer);
            changeTimer = null;
        })
        imgArea.addEventListener("mouseleave", function () {
            autoChange();
        })
        areaDom.appendChild(imgArea);
    }
    //Create superscript elements and set styles function initNumber() {
        numberArea.style.textAlign = "center";
        numberArea.style.marginTop = "-25px";
        for (let i = 0; i < options.length; i++) {
            var sp = document.createElement("span");
            sp.style.width = "12px";
            sp.style.height = "12px";
            sp.style.background = "lightgray";
            sp.style.display = "inline-block";
            sp.style.margin = "0 7px";
            sp.style.borderRadius = "50%";
            sp.style.cursor = "pointer";
            sp.addEventListener("click", function () {
                curIndex = i;
                setStatus();
            })
            numberArea.appendChild(sp);
        }
        areaDom.appendChild(numberArea);
    }
 
    //Set the corner area status function setStatus() {
        //Set the background color of the circle for (var i = 0; i < options.length; i++) {
            if (i === curIndex) {
                //Set the background color to the selected numberArea.children[i].style.background = "rgb(222 57 57)";
            } else {
                //Non-select numberArea.children[i].style.background = "lightgray";;
            }
 
        }
        //Display the picture var start = parseInt(imgArea.children[0].style.marginLeft);
        var end = curIndex * -100;
        var dis = end - start;
        var duration = 500;
        var speed = dis / duration;
        if (timer) {
            clearInterval(timer);
        }
        timer = setInterval(function () {
            start += speed * 20;
            imgArea.children[0].style.marginLeft = start + "%";
            if (Math.abs(end - start) < 1) {
                imgArea.children[0].style.marginLeft = end + "%";
                clearInterval(timer);
            }
        }, 20)
    }
    //Automatically switch function autoChange() {
        if (changeTimer) {
            return;
        }
        changeTimer = setInterval(function () {
            if (curIndex === options.length - 1) {
                curIndex = 0;
            } else {
                curIndex++;
            }
            setStatus();
        }, changeDuration)
    }
 
}

The speed of the slideshow (switching time) can be adjusted in the plugin code var changeDuration = 1000; //interval

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:
  • Swiper.js plugin makes it super easy to implement carousel images
  • js to realize seamless carousel plug-in encapsulation
  • Detailed explanation of plug-in encapsulation of js carousel
  • Native JS carousel plug-in
  • js to implement the complete code of the carousel
  • Sample code for implementing carousel with native js
  • 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 implements left and right seamless carousel code

<<:  Navicat for MySQL 11 Registration Code\Activation Code Summary

>>:  Front-end development must learn to understand HTML tags every day (1)

Recommend

How to solve the problem that the project in eclipse cannot be added to tomcat

1. Right-click the project and select properties ...

About Zabbix custom monitoring items and triggers

Table of contents 1. Monitoring port Relationship...

How to modify the contents of an existing Docker container

1. Docker ps lists containers 2. Docker cp copies...

Detailed tutorial on replacing mysql8.0.17 in windows10

This article shares the specific steps of replaci...

Implementation of Vue3 style CSS variable injection

Table of contents summary Basic Example motivatio...

Vue implements a complete process record of a single file component

Table of contents Preface Single file components ...

Slot arrangement and usage analysis in Vue

The operating environment of this tutorial: Windo...

How to modify the master-slave replication options in MySQL online

Preface: The most commonly used architecture of M...

Detailed explanation of MySQL delayed replication library method

Simply put, delayed replication is to set a fixed...

Detailed tutorial on installing and configuring MySql5.7 on Ubuntu 20.04

Table of contents 1. Ubuntu source change 2. Inst...

Detailed graphic explanation of sqlmap injection

Table of contents 1. We found that this website m...

Detailed graphic tutorial on how to enable remote secure access with Docker

1. Edit the docker.service file vi /usr/lib/syste...

Some experience sharing on enabling HTTPS

As the domestic network environment continues to ...