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

Vue implements time countdown function

This article example shares the specific code of ...

How to dynamically add a volume to a running Docker container

Someone asked me before whether it is possible to...

CSS3 clear float method example

1. Purpose Through this article, everyone can und...

A brief discussion on Nginx10m+ high concurrency kernel optimization

What is high concurrency? The default Linux kerne...

A record of the pitfalls of the WeChat applet component life cycle

The component lifecycle is usually where our busi...

18 common commands in MySQL command line

In daily website maintenance and management, a lo...

ReactRouter implementation

ReactRouter implementation ReactRouter is the cor...

In-depth understanding of the implementation principle of require loader

Preface We often say that node is not a new progr...

Detailed explanation of angular two-way binding

Table of contents Bidirectional binding principle...

An enhanced screenshot and sharing tool for Linux: ScreenCloud

ScreenCloud is a great little app you didn’t even...

Introduction to Docker Architecture

Docker includes three basic concepts: Image: A Do...

Implementation example of uploading multiple attachments in Vue

Table of contents Preface Core code File shows pa...