This article shares the specific code for JavaScript to achieve the effect of the carousel for your reference. The specific content is as follows A simple carousel chart written using a timer, look directly at the code, as follows: 1.css code <style> *{ margin: 0; padding: 0; box-sizing: border-box; } body{ font-size: 14px; font-family: Arial, Helvetica, sans-serif; } .slider-box{ width: 1226px; height: 460px; margin: 10px auto; overflow: hidden; position: relative; } .slider-box .images a{ width: 100%; height: 460px; position: absolute; left: 0; top: 0; opacity: 0; transition: all 2s; } .slider-box .images a.active{ opacity: 1; } .slider-box .images a img{ width: 100%; height: 100%; display: block; } .slider-box .nav{ position: absolute; left: 0; top: 195px; width: 100%; /* background-color: red; */ padding: 0 10px; /* height: 100px; */ } .slider-box .nav a{ background-image: url(img/icons.png); width: 41px; height: 69px; display: inline-block; background-repeat: no-repeat; } .slider-box .nav .prev{ background-position: -84px 0; } .slider-box .nav .prev:hover{ background-position: 0 0; } .slider-box .nav .next{ background-position: -125px 0; float: right; } .slider-box .nav .next:hover{ background-position: -42px 0; } .slider-box .pages{ position: absolute; right: 20px; bottom: 25px; font-size: 0; width: 1186px; text-align: center; /* background-color: rebeccapurple; */ } .slider-box .pages .dot{ display: inline-block; width: 10px; height: 10px; border-radius: 50%; background-color: rgba(0,0,0,0.4); margin-right: 10px; } .slider-box .pages .dot:hover{ background-color: yellow; } .slider-box .pages .dot.active{ background-color: yellow; } </style> 2.html code <div class="slider-box"> <div class="images"> <!-- If you want to display any picture in the future, just add an active class --> <a href="#" class="active"> <img src="img/1.jpg" alt=""> </a> <a href="#" > <img src="img/2.jpg" alt=""> </a> <a href="#" > <img src="img/3.jpg" alt=""> </a> <a href="#" > <img src="img/4.jpg" alt=""> </a> <a href="#" > <img src="img/5.jpg" alt=""> </a> </div> <div class="nav"> <a href="javascript:;" class="prev" title="Previous"></a> <a href="javascript:;" class="next" title="Next"></a> </div> <div class="pages"> <a href="javascript:;" class="dot active"></a> <a href="javascript:;" class="dot"></a> <a href="javascript:;" class="dot"></a> <a href="javascript:;" class="dot"></a> <a href="javascript:;" class="dot"></a> </div> </div> 3.js code <script> // Timer switching image function var images = document.querySelectorAll('.images a') var index = 0 //Define the index of the picture to be played var pages = document.querySelectorAll(".dot") var prev = document.querySelector(".prev") var next = document.querySelector(".next") // Switch images according to index value // Find the a tag of images and add the active class function showImage(idx){ images.forEach(function(v,i){ // idx = 1 // i = 0 1 2 3 4 if(i===idx){ v.classList.add('active') //Control the corresponding point highlight pages[i].classList.add("active") }else{ v.classList.remove('active') pages[i].classList.remove("active") } }) } // showImage(3) prev.onclick = function(){ if(index===0){ index = images.length - 1 // 4 }else{ index = index - 1 } showImage(index) } next.onclick = function(){ if(index===images.length-1){ index = 0 }else{ index+=1 } showImage(index) } var timer = setInterval(function(){ // The timer controls the switching of pictures and has the same function as clicking the next picture // Call the click operation of the next picture next.click() // Simulate the click operation of next },3000) </script> 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:
|
<<: How to enter and exit the Docker container
>>: The principle and application of MySQL connection query
Preface When I was studying the front end before,...
Pull the image root@EricZhou-MateBookProX: docker...
Element form and code display For details, please...
Preface There are many open source monitoring too...
Using Nginx's proxy_cache to build a cache se...
Table of contents Prototype chain We can implemen...
It is very easy to delete data and tables in MySQ...
Table of contents Zabbix monitors Nginx Zabbix mo...
Table of contents Preface 1. Use for...of to iter...
Error message: user: 'root' host: `localh...
1. Usage scenarios There is such a requirement, s...
Preface I have been summarizing my front-end know...
The following introduces the commonly used head s...
The action of the form is different from the URL j...
1. Write the Dockerfile (1) Right-click the proje...