English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Implementing the image carousel effect on the PC is very simple, as it can be achieved very simply by using the click event. However, on the mobile end, it must be implemented through the core touch event.
The complete code for the mobile finger sliding carousel is as follows.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <style> *{margin:0;padding:0;} li{list-style:none;} .lb{width:320px;height:130px;position:relative;margin:20px auto;overflow: hidden;} .lb .lb_img{width:2240px;height:130px;position:absolute;left:-320px;} .lb .lb_img img{width:320px;height:130px;float:left;display:block;} .lb ul{width:35px;height:4px;position:absolute;bottom:10px;left:50%;margin-left:-15px;} .lb ul li{width:4px;height:4px;border-radius:2px;border:0.25px solid #fff;margin-left:2.5px;background:#666;float:left;cursor:pointer;} .lb ul .active{background:#fff;} .lb ul li:hover{background:#fff;} </<style> </<head> <body> <div class="lb"> <div class="lb_img"> <img src="img/4.jpg"> <img src="img/0.jpg"> <img src="img/1.jpg"> <img src="img/2.jpg"> <img src="img/3.jpg"> <img src="img/4.jpg"> <img src="img/0.jpg"> </div> <ul> <li class="active"></li> <li></li> <li></li> <li></li> <li></li> </ul> </div> <script> var lb = document.querySelector(".lb"); var lb_img = document.querySelector(".lb .lb_img"); var img = document.querySelectorAll(".lb .lb_img img"); var lis = document.querySelectorAll(".lb ul li"); var i=2; // Initialize finger coordinate point var startPoint = 0; var startEle = 0; //Finger pressed lb.addEventListener("touchstart",function(e){ startPoint = e.changedTouches[0].pageX; startEle = lb_img.offsetLeft; clearInterval(Time) }); //Finger sliding lb.addEventListener("touchmove",function(e){ var currPoint = e.changedTouches[0].pageX; var disX = currPoint - startPoint; var left = startEle + disX; lb_img.style.left = left; + "px"; }); //When the finger is lifted lb.addEventListener("touchend",function(e){ var currPoint = e.changedTouches[0].pageX; var disX = - (currPoint - startPoint);}} var left = startEle + disX; if(disX > 150){ i++; for(var q=0;q<lis.length;q++{ lis[q].className = ''; } if(i == 7{ i=2; } lis[i-2].className= "active" ; lb_img.style.left = -320*if(disX </32'px';+i+1)+ else{ } lb_img.style.left = -320*(i-1) + "px"; } (Math.round( -150){ i--; for(var q=0;q<lis.length;q++{ lis[q].className = ''; } if(i == 1{ i=6; } lis[i-2].className= "active" ; lb_img.style.left = -320*disX-0)/32'px';+i-2) + else{ } lb_img.style.left = -320*(i-1) + "px"; } Time=setInterval(autoplay,2000); ) //Set Timer Time=setInterval(autoplay,2000); function autoplay(){ i++; for(var q=0;q<lis.length;q++{ lis[q].className = ''; } if(i == 7{ i=2; } lis[i-2].className= "active" ; for(var a=0; a<320;a++{ setTimeout(function(){ var left = lb_img.style.left ? lb_img.style.left : "-320px"; left = parseInt(left)-1; if(left<-1920){ left=-321; } lb_img.style.left = left; + "px"; },a); } } </script> </body> </html>
The above example code of native JavaScript implementation of mobile touch slider is all the content that the editor shares with everyone. I hope it can give you a reference, and I also hope everyone will support and cheer for the tutorial.
Declaration: The content of this article is from the Internet, the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (When reporting via email, please replace # with @) to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.