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)
Apache Superset is a powerful BI tool that provid...
Color contrast and harmony In contrasting conditi...
Table of contents 1. Problematic SQL statements S...
Note: This demo is tested in the mini program env...
What is the purpose of creating your own website u...
1. Set up Chinese input method 2. Set the double ...
The reason is this I wanted to deploy a mocker pl...
nvm nvm is responsible for managing multiple vers...
Drop-shadow and box-shadow are both CSS propertie...
lsof (list open files) is a tool to view files op...
Table of contents Overview Index data structure B...
1. Modify 1 column update student s, city c set s...
Part 1: Basics 1. Unlike pseudo-classes such as :...
1. Download MySQL from the official website: This...
<br />Some web pages may not look large but ...