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

Two ways to visualize ClickHouse data using Apache Superset

Apache Superset is a powerful BI tool that provid...

Sharing of web color contrast and harmony techniques

Color contrast and harmony In contrasting conditi...

A "classic" pitfall of MySQL UPDATE statement

Table of contents 1. Problematic SQL statements S...

CSS mimics remote control buttons

Note: This demo is tested in the mini program env...

How to check and organize website files using Dreamweaver8

What is the purpose of creating your own website u...

Installation steps of Ubuntu 20.04 double pinyin input method

1. Set up Chinese input method 2. Set the double ...

Detailed installation process of nodejs management tool nvm

nvm nvm is responsible for managing multiple vers...

Comparison of CSS shadow effects: drop-Shadow and box-Shadow

Drop-shadow and box-shadow are both CSS propertie...

Detailed explanation of Linux lsof command usage

lsof (list open files) is a tool to view files op...

In-depth analysis of MySQL index data structure

Table of contents Overview Index data structure B...

How to use a field in one table to update a field in another table in MySQL

1. Modify 1 column update student s, city c set s...

Deep understanding of the use of ::before/:before and ::after/:after

Part 1: Basics 1. Unlike pseudo-classes such as :...

How to reduce the memory and CPU usage of web pages

<br />Some web pages may not look large but ...