This article shares the implementation method of JavaScript carousel for your reference. The specific content is as follows Effect screenshots:Note: The div container size and image path can be set by yourself, and the browser can adapt after adding img and a tags. Create an image folder to store pictures Write HTML text <body> //The image path can be changed by yourself <div id="outer"> <ul id="imglist"> <li><img src="image/8.jpg" alt=""></li> <li><img src="image/6.jpg" alt=""></li> <li><img src="image/7.jpg" alt=""></li> <li><img src="image/6.jpg" alt=""></li> <li><img src="image/8.jpg" alt=""></li> <li><img src="image/7.jpg" alt=""></li> <li><img src="image/6.jpg" alt=""></li> <li><img src="image/8.jpg" alt=""></li> </ul> <div id="nav"> <a href="JavaScript:;"></a> <a href="JavaScript:;"></a> <a href="JavaScript:;"></a> <a href="JavaScript:;"></a> <a href="JavaScript:;"></a> <a href="JavaScript:;"></a> <a href="JavaScript:;"></a> <a href="JavaScript:;"></a> </div> </div> </body> Add CSS styles <style> * { margin: 0px; padding: 0px; } /* Outer frame container */ #outer { width: 1555px; height: 600px; background-color: #bfa; margin: 100px auto; position: relative; /* Hide */ overflow: hidden; } /* List of pictures */ #imglist { /* Flexbox layout */ display: flex; list-style: none; position: relative; /* Layout direction */ /* flex-direct5on: row; */ /*Move an image pixel by 1552px*/ /* right: 1552px; */ } #imglist li { margin: 10px 10px; } /* Navigation point */ #nav { display: flex; list-style: none; position: absolute; bottom: 50px; /* 1555/2 - 6*(20+25)/2 */ /* left: 642px; */ } #nav a { width: 25px; height: 25px; margin: 0px 10px; background-color: rgb(223, 129, 52); border-radius: 5px; } /* Mouse movement effect*/ #nav a:hover { background-color: rgb(215, 107, 224); } </style> Implementing functionality with JavaScript <script type="text/javascript"> window.onload = function () { // Get the outer frame properties var outer = document.getElementById("outer"); // Get imglist attributes var imglist = document.getElementById("imglist"); // Get img attributes var imgArr = document.getElementsByTagName("img"); // Get the a attribute var allA = document.getElementsByTagName('a'); //Get the navigation point var nav = document.getElementById("nav"); //Set the navigation point to the center nav.style.left = (outer.offsetWidth / 2) - (allA.length * 45 / 2) + "px"; //Default display index var index = 0; allA[index].style.backgroundColor = "rgb(215, 107, 224)"; // Switch navigation point timer var timer = setInterval(function () { //Loop display index = (++index) % allA.length; //Set the background color of the navigation point allA[index].style.backgroundColor = "rgb(215, 107, 224)"; SetA(); //Automatically switch pictures//Modify pictures, one picture moves 1552px to the left imglist.style.transition = "right 1.5s" imglist.style.right = (index * 1552) + "px"; }, 1800); //Click the hyperlink to display the image for (var i = 0; i < allA.length; i++) { //Add index to each hyperlink allA[i].index = i; //Bind click response function for each hyperlink allA[i].onclick = function () { imgIndex = this.index; //Override the current position of the navigation point index = imgIndex; SetA(); //Modify the image, move the pixels of an image to the left by 1552px imglist.style.transition = "right .85s" imglist.style.right = (imgIndex * 1552) + "px"; //Modify the selected a tag allA[imgIndex].style.backgroundColor = "rgb(215, 107, 224)"; }; } //Clear the style of a function SetA() { for (var i = 0; i < allA.length; i++) { allA[i].style.backgroundColor = ""; } //Set allA[index].style.backgroundColor = "rgb(215, 107, 224)" for the current navigation; } }; </script> Complete code<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Carousel Image</title> <style> * { margin: 0px; padding: 0px; } /* Outer frame container */ #outer { width: 1555px; height: 600px; background-color: #bfa; margin: 100px auto; position: relative; /* Hide */ overflow: hidden; } /* List of pictures */ #imglist { /* Flexbox layout */ display: flex; list-style: none; position: relative; /* Layout direction */ /* flex-direct5on: row; */ /*Move an image pixel by 1552px*/ /* right: 1552px; */ } #imglist li { margin: 10px 10px; } /* Navigation point */ #nav { display: flex; list-style: none; position: absolute; bottom: 50px; /* 1555/2 - 6*(20+25)/2 */ /* left: 642px; */ } #nav a { width: 25px; height: 25px; margin: 0px 10px; background-color: rgb(223, 129, 52); border-radius: 5px; } /* Mouse movement effect*/ #nav a:hover { background-color: rgb(215, 107, 224); } </style> <script type="text/javascript"> window.onload = function () { // Get the outer frame properties var outer = document.getElementById("outer"); // Get imglist attributes var imglist = document.getElementById("imglist"); // Get img attributes var imgArr = document.getElementsByTagName("img"); // Get the a attribute var allA = document.getElementsByTagName('a'); //Get the navigation point var nav = document.getElementById("nav"); //Set the navigation point to the center nav.style.left = (outer.offsetWidth / 2) - (allA.length * 45 / 2) + "px"; //Default display index var index = 0; allA[index].style.backgroundColor = "rgb(215, 107, 224)"; // Switch navigation point timer var timer = setInterval(function () { index = (++index) % allA.length; //Set the background color of the navigation point allA[index].style.backgroundColor = "rgb(215, 107, 224)"; SetA(); //Automatically switch pictures//Modify pictures, one picture moves 1552px to the left imglist.style.transition = "right 1.5s" imglist.style.right = (index * 1552) + "px"; //Loop display}, 1800); //Click the hyperlink to display the image for (var i = 0; i < allA.length; i++) { //Add index to each hyperlink allA[i].index = i; //Bind click response function for each hyperlink allA[i].onclick = function () { imgIndex = this.index; //Override the current position of the navigation point index = imgIndex; SetA(); //Modify the image, move the pixels of an image to the left by 1552px imglist.style.transition = "right .85s" imglist.style.right = (imgIndex * 1552) + "px"; //Modify the selected a tag allA[imgIndex].style.backgroundColor = "rgb(215, 107, 224)"; }; } //Clear the style of a function SetA() { for (var i = 0; i < allA.length; i++) { allA[i].style.backgroundColor = ""; } allA[index].style.backgroundColor = "rgb(215, 107, 224)"; } }; </script> </head> <body> <div id="outer"> <ul id="imglist"> <li><img src="image/8.jpg" alt=""></li> <li><img src="image/6.jpg" alt=""></li> <li><img src="image/7.jpg" alt=""></li> <li><img src="image/6.jpg" alt=""></li> <li><img src="image/8.jpg" alt=""></li> <li><img src="image/7.jpg" alt=""></li> <li><img src="image/6.jpg" alt=""></li> <li><img src="image/8.jpg" alt=""></li> </ul> <div id="nav"> <a href="JavaScript:;"></a> <a href="JavaScript:;"></a> <a href="JavaScript:;"></a> <a href="JavaScript:;"></a> <a href="JavaScript:;"></a> <a href="JavaScript:;"></a> <a href="JavaScript:;"></a> <a href="JavaScript:;"></a> </div> </div> </body> </html> Function usage:Create a timer: setInterval(function () {},30) Set the rounded border: border-radius: 5px;
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:
|
<<: HTTPS Principles Explained
>>: In-depth analysis of MySQL indexes
When we package the webpackjs file, we introduce ...
Horizontal Line Use the <hr /> tag to draw ...
MVCC MVCC (Multi-Version Concurrency Control) is ...
Migration is unavoidable in many cases. Hardware ...
Download https://tomcat.apache.org/download-80.cg...
This article shares the specific code of Vue to a...
Today I will introduce how to enable the Linux su...
1. Configure local yum source 1. Mount the ISO im...
Table of contents Preface 1. Environment Configur...
Select the category selection. After testing, IE ...
The pitfalls of MySQL read-write separation The m...
Introduction MySQL slow query log is an important...
In the latest HTML standard, there is a calc CSS e...
Result:Implementation Code html <ul class=&quo...
Purpose of using Nginx Using Alibaba Cloud ECS cl...