This article shares the specific code of js to achieve a simple carousel effect for your reference. The specific content is as follows Image switching using transform = translateX() <style> .box { position: relative; overflow: hidden; margin: 200px auto; width: 600px; height: 400px; } .img { width: 3000px; height: 400px; } img { float: left; width: 600px; height: 400px; } .btn { position: absolute; top: 350px; left: 245px; width: 110px; height: 20px; } .dian { float: left; margin: 5px; width: 12px; height: 12px; border-radius: 50%; background-color: rgba(255, 255, 255, 0.3); cursor: pointer; } .left, .right { display: none; box-sizing: border-box; position: absolute; top: 150px; width: 50px; height: 100px; background-color: rgba(0, 0, 0, 0.521); font-size: 40px; line-height: 100px; color: #fff; } .box:hover .left, .box:hover .right { display: block; } .left { padding-left: 10px; left: 0; } .right { padding-right: 10px; text-align: right; right: 0; } </style> <div class="box"> <div class="img"> <img src="./img/1.jpeg" alt=""> <img src="./img/2.jpeg" alt=""> <img src="./img/3.jpeg" alt=""> <img src="./img/4.jpeg" alt=""> <img src="./img/5.jpeg" alt=""> </div> <div class="btn"> <span class="dian"></span> <span class="dian"></span> <span class="dian"></span> <span class="dian"></span> <span class="dian"></span> </div> <div class="left">< </div> <div class="right">></div> </div> <script> var btn = document.getElementsByClassName('btn')[0]; var imgBox = document.getElementsByClassName('img')[0]; var imgs = imgBox.getElementsByTagName('img'); var btnChild = document.getElementsByClassName('dian'); var k = 0; // Add custom properties to the button for (var i = 0; i < btnChild.length; i++) { btnChild[i].dataset.num = i * 600; } // The default is the first picture, and the first button is white btnChild[0].style.background = 'rgba(255, 255, 255, 0.8)'; // Click the button to switch the image and change the button color btn.onclick = function(e) { if (e.target.nodeName === 'SPAN') { // First set all buttons to default color for (var i = 0; i < btnChild.length; i++) { btnChild[i].style.background = 'rgba(255, 255, 255, 0.4)'; } // The button clicked changes color k = +e.target.dataset.num; console.log(k); btnChild[k / 600].style.background = 'rgba(255, 255, 255, 0.8)'; // Switch image imgBox.style.transform = `translateX(-${e.target.dataset.num}px)`; } } // Get the image length // var imgWidth = +getComputedStyle(imgs[0]).width.slice(0, -2); var imgWidth = imgs[0].offsetWidth; // Timer moves the picture var interval1 = setInterval(move, 1000); var interval; // Function moves the image and changes the button function move() { k += imgWidth; if (k === imgWidth * imgs.length) { k = 0; } // Move the image imgBox.style.transform = `translateX(-${k}px)`; // Change button color for (var i = 0; i < btnChild.length; i++) { btnChild[i].style.background = 'rgba(255, 255, 255, 0.4)'; } btnChild[k / 600].style.background = 'rgba(255, 255, 255, 0.8)'; } // Move the mouse to the image to delete the timer var box = document.getElementsByClassName('box')[0]; box.onmouseover = function() { clearInterval(interval1); clearInterval(interval); } // Move the mouse out to start the timer box.onmouseout = function() { interval = setInterval(move, 1000); } var leftBtn = document.getElementsByClassName('left')[0]; var rightBtn = document.getElementsByClassName('right')[0]; rightBtn.onclick = function() { k += imgWidth; if (k === imgWidth * imgs.length) { k = 0; } // Move the image imgBox.style.transform = `translateX(-${k}px)`; // Change button color for (var i = 0; i < btnChild.length; i++) { btnChild[i].style.background = 'rgba(255, 255, 255, 0.4)'; } btnChild[k / 600].style.background = 'rgba(255, 255, 255, 0.8)'; } leftBtn.onclick = function() { k -= imgWidth; if (k < 0) { k = imgWidth * (imgs.length - 1); } imgBox.style.transform = `translateX(-${k}px)`; // Change button color for (var i = 0; i < btnChild.length; i++) { btnChild[i].style.background = 'rgba(255, 255, 255, 0.4)'; } btnChild[k / 600].style.background = 'rgba(255, 255, 255, 0.8)'; } </script> Effect picture: 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:
|
<<: What you need to know about MySQL auto-increment ID
Problem Description MySQL reports an error when s...
1. Demand A picture moves from left to right in a...
text-shadow Add a shadow to the text. You can add...
The W3C standardization process is divided into 7...
.NET SDK Download Link https://dotnet.microsoft.c...
As more and more Docker images are used, there ne...
Preface Vue Router is the official routing manage...
clear:both is used to清除浮動This is the impression I...
This article shares the specific code of Vue usin...
Delete a file by its inode number First use ls -i...
This article records the installation and configu...
1: Differences in speed and loading methods The di...
Table of contents JS obtains the .txt file conten...
Table of contents Same Origin Policy Ajax request...
1. What is positioning? The position attribute in...