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
Introduction to MySQL logical architecture Overvi...
【1】Know the width and height of the centered elem...
Usage of MySQL memory tables and temporary tables...
Table of contents 1. Data Type 1.1 Why do we need...
The code demonstrates horizontal merging: <!DO...
Use the --all-database parameter when performing ...
1. Download the MySQL 5.7 installation package fr...
1 Requirements Overview The data of multiple tabl...
Countdown function needs to be implemented in man...
Application scenario 1: Domain name-based redirec...
calc is a function in CSS that is used to calcula...
Simply put, distinct is used to remove duplicates...
MySQL DECIMAL data type is used to store exact nu...
April 23, 2020, Today, Ubuntu 20.04 on Windows al...
JPQL stands for Java Persistence Query Language. ...