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:
|
<<: Navicat for MySQL 11 Registration Code\Activation Code Summary
>>: Front-end development must learn to understand HTML tags every day (1)
Table of contents 1. Pull the Redis image 2. Crea...
This article records the detailed tutorial of MyS...
Preface 1. Benchmarking is a type of performance ...
This article example shares the specific code of ...
Recently, several data anomalies have occurred in...
This is a very important topic, not only for Linu...
This article shares the specific code for impleme...
During normal project development, if the MySQL v...
In the Docker system learning tutorial, we learne...
Table of contents Preface Create a Vite project R...
<br />For some time, I found that many peopl...
Use the following terminal command to install the...
If you want to change your host name, you can fol...
Here are the detailed steps: 1. Check the disk sp...
This article describes the MySQL integrity constr...