Below I will share with you a sample code for implementing native js to implement touch carousel on the mobile terminal. It has a good reference value and I hope it will be helpful to everyone.
It is very simple to realize the image carousel effect on the PC side. You can achieve the effect very simply by using the click event, but on the mobile side, it must be achieved through the core touch event.
The following is the complete code for the finger sliding carousel on the mobile terminal.
<!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> <p class="lb"> <p class="lb_img"> <img src="/static/imghwm/default1.png" data-src="img/4.jpg" class="lazy" alt="How to implement mobile touch carousel in js" > <img src="/static/imghwm/default1.png" data-src="img/0.jpg" class="lazy" alt="How to implement mobile touch carousel in js" > <img src="/static/imghwm/default1.png" data-src="img/1.jpg" class="lazy" alt="How to implement mobile touch carousel in js" > <img src="/static/imghwm/default1.png" data-src="img/2.jpg" class="lazy" alt="How to implement mobile touch carousel in js" > <img src="/static/imghwm/default1.png" data-src="img/3.jpg" class="lazy" alt="How to implement mobile touch carousel in js" > <img src="/static/imghwm/default1.png" data-src="img/4.jpg" class="lazy" alt="How to implement mobile touch carousel in js" > <img src="/static/imghwm/default1.png" data-src="img/0.jpg" class="lazy" alt="How to implement mobile touch carousel in js" > </p> <ul> <li class="active"></li> <li></li> <li></li> <li></li> <li></li> </ul> </p> <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; // 初始化手指坐标点 var startPoint = 0; var startEle = 0; //手指按下 lb.addEventListener("touchstart",function(e){ startPoint = e.changedTouches[0].pageX; startEle = lb_img.offsetLeft; clearInterval(Time) }); //手指滑动 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"; }); //当手指抬起的时候, 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*(Math.round(disX/320)+i+1)+ 'px'; }else{ lb_img.style.left = -320*(i-1) + "px"; } if(disX < -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*(Math.round(-disX/320)+i-2) + 'px'; }else{ lb_img.style.left = -320*(i-1) + "px"; } Time=setInterval(autoplay,2000); }) //设置定时器 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 is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
How to query weather forecast in Angular
How to display input content in Angular
How to implement the schedule function in Angular
How to implement file upload in nodejs express
In Vue How to implement a blog management platform in SpringBoot
How to solve the maximum call stack error in nodejs
About asynchronous components in Vue Example
The above is the detailed content of How to implement mobile touch carousel in js. For more information, please follow other related articles on the PHP Chinese website!

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download
The most popular open source editor

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools
