This article shares the specific code for JavaScript to achieve the effect of the carousel for your reference. The specific content is as follows 1. Function:1. Automatically switch to the next slideshow every 2.5 seconds; 2. The bottom button switches the corresponding carousel image; 3. Move the mouse in to pause the automatic switching, move it out to start; 4. Move the mouse, and the left and right switch buttons will appear, and you can switch the carousel left and right. 2. Effect (GIF):3. Code:Structure layer (HTML) <div class="box"> <img src="./image/banner1.jpg" /> <div class="arrows left"> <img src="./image/left.png" /> </div> <div class="arrows right"> <img src="./image/right.png" /> </div> <ul class="buttom"></ul> </div> Presentation layer (CSS) .box { width: 300px; height: 200px; background: #333; border-radius: 5px; overflow: hidden; margin: 0 auto; font-size: 0; position: relative; display: flex; align-items: center; } .box:hover .arrows{ display: block; } .box img{ width: 100%; } .arrows { width: 20px; text-align: center; position: absolute; top: 50%; transform: translateY(-50%); z-index: 9; font-size: 30px; display: none; } .left{ left: 10px; } .right{ right: 10px; } .button{ list-style: none; margin: 0; padding: 0; display: flex; justify-content: center; position: absolute; bottom: 10px; left: 50%; transform: translateX(-50%); } .button li { width: 20px; height: 5px; border-radius: 1px; background: #fff; margin: 0 2px; } .active { background: red !important; } Behavior layer (JavaScript) let count = 0 // Create the current carousel index // Get the DOM element let box = document.querySelector('.box') let img = document.querySelector('img') let left = document.querySelector('.left') let right = document.querySelector('.right') let ul = document.querySelector('ul') // Carousel image array let imgArr = [ './image/banner1.jpg', './image/banner2.jpg', './image/banner3.jpg', './image/banner4.jpg' ] // Traverse the image array and add the corresponding bottom switch li tag imgArr.forEach(() => { let li = document.createElement('li') ul.appendChild(li) }) let lis = document.querySelectorAll('li') // Get all li tags lis[0].className = 'active' // Add selected state to the first li tag // Switch the slideshow function switchImg (type) { return function() { if(type == 1) { if(count - 1 < 0) { count = imgArr.length - 1 } else { count += -1 } } else { if(count + 1 >= imgArr.length) { count = 0 } else { count += 1 } } img.src = imgArr[count] lis.forEach((v,i) => { lis[i].className = '' if(i == count) { lis[i].className = 'active' } }) } } left.addEventListener('click', switchImg(1)) // Previous slideshow right.addEventListener('click', switchImg(2)) // Next slideshow // Click the bottom li tag to switch slideshow lis.forEach((value,index) => { lis[index].addEventListener('click', () => { lis.forEach((v,i) => { lis[i].className = '' }) count = index img.src = imgArr[count] lis[count].className = 'active' }) }) // Create a timer to automatically switch to the next slide every 2.5 seconds let swiper = setInterval(() => { right.click() },2500) //Mouse enters and pauses automatic switching box.onmouseenter = () => { clearInterval(swiper) } //Mouse out to start automatic switching box.onmouseleave = () => { swiper = setInterval(() => { right.click() },1500) } 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:
|
<<: Detailed explanation of the difference between Mysql temporary table and partition table
>>: Linux installation MongoDB startup and common problem solving
1. Unzip nginx-1.8.1.tar.gz 2. Unzip fastdfs-ngin...
Table of contents 8. CSS3 click button circular p...
First look at the effect diagram: The complete co...
This article introduces an example of how CSS3 ca...
Preface: Jenkins' Master-Slave distributed ar...
Preface 1. This article uses MySQL 8.0 version Co...
Installation environment: CentOS7 64-bit MINI ver...
Due to the limitation of CPU permissions, communi...
In the horizontal direction, you can set the cell...
Table of contents How to create a Pod? kubectl to...
Table of contents 1. What is an Operating System ...
Deploy nginx with docker, it's so simple Just...
Before introducing the GROUP BY and HAVING clause...
html , address , blockquote , body , dd , div , d...
Table of contents 1. Modify the my.cnf file of se...