Native js realizes the carousel effect (seamless scrolling) for your reference. The specific content is as follows Effect picture: Code: <!DOCTYPE html> <html lang="en"> <!-- day07 7-10-14 --> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="./images1/20.animate.js"></script> <style> * { margin: 0; padding: 0; } li { list-style: none; } .focus { /*overflow: hidden;*/ position: absolute; top: 100px; left: 200px; width: 721px; height: 455px; background-color: brown; } .prev, .next { display: none; position: absolute; top: 50%; margin-top: -15px; width: 20px; height: 30px; background-color: rgba(0, 0, 0, .3); text-decoration: none; color: #fff; line-height: 30px; text-align: center; font-size: 16px; z-index: 2; } .focus ul { /* The introduction of animation js files requires positioning*/ position: absolute; width: 600%; } .focus ul li { float: left; } .prev { left: 0; border-top-right-radius: 15px; border-bottom-right-radius: 15px; } .next { right: 0; border-top-left-radius: 15px; border-bottom-left-radius: 15px; } .promo-nav { position: absolute; bottom: 10px; left: 60px; width: 200px; height: 18px; border-radius: 9px; } .promo-nav li { float: left; width: 10px; height: 10px; background-color: #fff; margin: 2px; border-radius: 50%; } .promo-nav .current { background-color: orange; } .focus ul li a img { width: 721px; height: 455px; } </style> </head> <body> <div class="focus"> <ul> <li> <a href="#" ><img src="images1/focu01.jpg" alt=""></a> </li> <li> <a href="#" ><img src="images1/focu02.jpg" alt=""></a> </li> <li> <a href="#" ><img src="images1/focu03.jpg" alt=""></a> </li> <li> <a href="#" ><img src="images1/focu04.jpg" alt=""></a> </li> </ul> <!-- Left button --> <a href="javascript:;" class="prev"><</a> <!-- Right button --> <a href="javascript:;" class="next">></a> <ol class="promo-nav"> </ol> </div> <script> window.addEventListener('load', function() { var focus = document.querySelector('.focus'); var prev = document.querySelector('.prev'); var next = document.querySelector('.next'); var focusWidth = focus.offsetWidth; //The mouse passes through focus.addEventListener('mouseenter', function() { prev.style.display = 'block'; next.style.display = 'block'; clearInterval(timer); timer = null; // clear the timer variable }) //The mouse leaves focus.addEventListener('mouseleave', function() { prev.style.display = 'none'; next.style.display = 'none'; timer = setInterval(function() { next.click(); }, 2000) }) //3. Dynamically generate small circles. Generate as many small circles as there are pictures. var ul = focus.querySelector('ul'); var ol = focus.querySelector('.promo-nav'); // console.log(ul.children.length); 4 for (var i = 0; i < ul.children.length; i++) { //Create a li var li = document.createElement('li'); //Record the index number of the current small circle through custom attributes li.setAttribute('index', i); //Insert after ol ol.appendChild(li); //4. Click the small circle with the mouse to change its color (add the current class to the small circle and remove this class from the other small circles) (exclusive idea) //Directly bind the click event while generating the small circle li.addEventListener('click', function() { for (var i = 0; i < ol.children.length; i++) { ol.children[i].className = ''; } this.className = 'current'; //5. Click the small dot to move the picture. The ul is moved. //The distance ul moves is the index number of the small circle multiplied by the width of the image. Note that it is a negative value. //When we click on a small li, we get the index number of the current small li. var index = this.getAttribute('index'); //When we click on a certain li, we give the index number of li to num num = index; //When we click on a certain li, we give the index number of li to index circle = index; console.log(index); animate(ul, -index * focusWidth, ); }) } //Set the color of the first li in ol to white ol.children[0].className = 'current'; //6. Clone the first li and put it behind ul var first = ul.children[0].cloneNode(true); ul.appendChild(first); //7. Click the button on the right to scroll one picture var num = 0; var circle = 0; var flag = true; //Right button next.addEventListener('click', function() { if (flag) { flag = false; //Close the throttle first //5. If you reach the last copied image, ul will quickly restore left to 0 (seamless scrolling) if (num == ul.children.length - 1) { ul.style.left = 0; num = 0; } num++; animate(ul, -num * focusWidth, function() { flag = true; }); //8. Click the button on the right and the small circle will change accordingly. Declare a variable to control the change of the small circle circle++; //If circle is equal to 4, it means that we have finished cloning the last picture and we will restore it if (circle == ol.children.length) { circle = 0; } // //Clear the remaining small circle class names// for (var i = 0; i < ol.children.length; i++) { // ol.children[i].className = ''; // } // //Leave the current small circle current class name// ol.children[circle].className = 'current'; circleChange(); } }) //Left button prev.addEventListener('click', function() { if (flag) { flag = false; //5. If you reach the last copied image, ul will quickly restore left to 0 (seamless scrolling) if (num == 0) { num = ul.children.length - 1; ul.style.left = -num * focusWidth + 'px'; } num--; animate(ul, -num * focusWidth, function() { flag = true; }); //8. Click the button on the right and the small circle will change accordingly. Declare a variable to control the change of the small circle circle--; //If circle is less than 0, the small circle will be changed to the fourth small circle if (circle < 0) { circle = ol.children.length - 1; } // Clear the remaining small circle class names // for (var i = 0; i < ol.children.length; i++) { // ol.children[i].className = ''; // } // Leave the current small circle current class name // ol.children[circle].className = 'current'; circleChange(); } }) function circleChange() { //Clear the rest of the small circle class names for (var i = 0; i < ol.children.length; i++) { ol.children[i].className = ''; } //Leave the current small circle current class name ol.children[circle].className = 'current'; } //10. Automatically play the slideshow var timer = setInterval(function() { next.click(); }, 2000) }) </script> </body> </html> 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:
|
>>: IIS 7.5 uses URL Rewrite module to achieve web page redirection
Table of contents I've been learning React re...
1. Backup source list The default source of Ubunt...
Table of contents We have written about drag and ...
Table of contents 1. HTTP Range Request 1.1 Range...
WeChat Mini Programs are becoming more and more p...
Overview Today I will mainly share how to correct...
The img tag in XHTML should be written like this:...
Table of contents 1. Variable Overview 1.1 Storag...
Table of contents 1. Application and configuratio...
Preface The role of process management: Determine...
Will mysql's IN invalidate the index? Won'...
Operating system: Window10 MySQL version: 8.0.13-...
Several parts of Compose deal with environment va...
MySQL download and installation (version 8.0.20) ...
Apache SkyWalking Apache SkyWalking is an applica...