search
HomeWeb Front-endJS TutorialDetailed explanation of how to use native JS to achieve mobile web carousel effect

When doing mobile development, carousel images are indispensable. The following article mainly introduces you to the relevant information about using pure JS to implement mobile web carousel images. The important thing is to combine the Tween algorithm Making wheels is introduced in detail through example code in this article. Friends in need can refer to it. Let’s take a look together.

Preface

I believe everyone should know that carousel images on the mobile terminal are our more common needs, and our fastest way to implement them is often We use third-party code, such as swiper, but when we encounter some more complex carousel chart requirements, we are often at a loss and don’t know how to change it. ​

So we have to try to create some ourselves Wheel to adapt to various complex and changing needs; Another point is that if there are bugs in the code you write, it is easy to fix, and it will greatly improve yourself.

Without reading the swiper source code , I tried to implement a simple yet practical mobile carousel chart myself. After several hours of thinking and practice, I finally realized it (as shown in the picture):


Implementing carousel images on the mobile terminal is more complicated than on PC, mainly in the following aspects:

1. Carousel image requirements Adapt to screens of different widths/dpr

2. Need to use touch-related events

3. Different models may support touch events differently. Some compatibility issues

4. Move your finger a part of the distance in the picture, and the remaining distance needs to be completed automatically

5. Automatically completing the distance requires an ease time curve

But the ideas for solving problems with programming are similar.

We can observe carefully when using the carousel chart and see the essence through the phenomenon:

  • When we use the carousel chart, we can observe carefully and see the essence through the phenomenon:

  • Place your finger On the picture, move your finger to the left or right, and the picture will move accordingly;

  • When the finger moves a small distance, the picture will automatically restore its position; when the finger moves a long distance, it will automatically switch to Next picture;

  • When you move your finger to the left or right quickly, it will switch to the next picture;

  • The picture carousel is Infinite loop, we need to use 3 1 2 3 1 method to achieve, that is, N+2 pictures to realize the infinite loop carousel of N pictures

By analyzing the phenomenon, we can propose a basic implementation plan:

1. Finger touch events can be realized through 3 events: touchstart touchmove touchend

2. We need to record the x-coordinate of the finger when the finger touchesstart. You can use the pageX attribute of touch; and at this time point,

3. We also need to record the pageX when the finger touchesmove. , and record the cumulative distance moved move
5. Determine the direction of movement by comparing the movement distance in the x direction and whether it should switch to the next picture; judge whether the user has performed a left or right swipe based on the time


6. Move The picture can be realized using translate3d, and hardware acceleration is turned on


7. Moving a certain distance requires the easeOut effect. We can use the easeOut in the Tween algorithm to realize the distance we move each time; of course, we can also use js Set transition animation


Implementation source code (for reference only):

head style

<head> 
 <meta charset="UTF-8"> 
 <meta name="viewport" content="width=device-width,initial-scale=.5,maximum-scale=.5"> 
 <title>移动端轮播图</title> 
 <style> 
 * { 
 box-sizing: border-box; 
 margin: 0; 
 padding: 0 
 } 
 .banner { 
 overflow: hidden; 
 width: 100%; 
 height: 300px 
 } 
 .banner .img-wrap { 
 position: relative; 
 height: 100% 
 } 
 .banner img { 
 display: block; 
 position: absolute; 
 top: 0; 
 width: 100%; 
 height: 100% 
 } 
 </style> 
</head>

HTML structure


<p class="banner"> 
 <p class="img-wrap" id="imgWrap"> 
 <img  src="/static/imghwm/default1.png"  data-src="images/banner_3.jpg"  class="lazy"   data-index="-1" alt="Detailed explanation of how to use native JS to achieve mobile web carousel effect" > 
 <img  src="/static/imghwm/default1.png"  data-src="images/banner_1.jpg"  class="lazy"   data-index="0" alt="Detailed explanation of how to use native JS to achieve mobile web carousel effect" > 
 <img  src="/static/imghwm/default1.png"  data-src="images/banner_2.jpg"  class="lazy"   data-index="1" alt="Detailed explanation of how to use native JS to achieve mobile web carousel effect" > 
 <img  src="/static/imghwm/default1.png"  data-src="images/banner_3.jpg"  class="lazy"   data-index="2" alt="Detailed explanation of how to use native JS to achieve mobile web carousel effect" > 
 <img  src="/static/imghwm/default1.png"  data-src="images/banner_1.jpg"  class="lazy"   data-index="3" alt="Detailed explanation of how to use native JS to achieve mobile web carousel effect" > 
 </p> 
</p>

JS code 1, easeOut animation Style move,


Here HTMLElement.prototype.tweenTranslateXAnimate is the tweenTranslateXAnimate method extended to all HTML element classes


To move a certain distance we need to use a timer to help We complete this repeated operation


<script> 
 HTMLElement.prototype.tweenTranslateXAnimate = function (start, end, callback) { 
 var duration = 50; 
 var t = 0; 
 var vv = end - start; 
 var Tween = { 
 Quad: { 
 easeOut: function (t, b, c, d) { 
  return -c * (t /= d) * (t - 2) + b; 
 } 
 } 
 }; 
 
 this.timer = setInterval(function () { 
 var dis = start + Tween.Quad.easeOut(++t, 0, vv, duration); 
 this.style.transform = &#39;translate3d(&#39; + dis + &#39;px, 0, 0)&#39;; 
 if (vv > 0 && parseInt(this.style.transform.slice(12)) >= end) { 
 this.style.transform = &#39;translate3d(&#39; + parseInt(dis) + &#39;px, 0, 0)&#39;; 
 clearInterval(this.timer); 
 callback && callback(); 
 } 
 if (vv < 0 && parseInt(this.style.transform.slice(12)) <= end) { 
 this.style.transform = &#39;translate3d(&#39; + parseInt(dis) + &#39;px, 0, 0)&#39;; 
 clearInterval(this.timer); 
 callback && callback(); 
 } 
 }.bind(this), 4); 
 } 
</script>

touch event part


<script> 
 ~function () { 
 var lastPX = 0; // 上一次触摸的位置x坐标, 需要计算出手指每次移动的一点点距离 
 var movex = 0; // 记录手指move的x方向值 
 var imgWrap = document.getElementById(&#39;imgWrap&#39;); 
 var startX = 0; // 开始触摸时手指所在x坐标 
 var endX = 0; // 触摸结束时手指所在的x坐标位置 
 var imgSize = imgWrap.children.length - 2; // 图片个数 
 var t1 = 0; // 记录开始触摸的时刻 
 var t2 = 0; // 记录结束触摸的时刻 
 var width = window.innerWidth; // 当前窗口宽度 
 var nodeList = document.querySelectorAll(&#39;#imgWrap img&#39;); // 所有轮播图节点数组 NodeList 
 
 // 给图片设置合适的left值, 注意 querySelectorAll返回 NodeList, 具有 forEach方法 
 nodeList.forEach(function (node, index) { 
 node.style.left = (index - 1) * width + &#39;px&#39;; 
 }); 
 
 /** 
 * 移动图片到当前的 tIndex索引所在位置 
 * @param {number} tIndex 要显示的图片的索引 
 * */ 
 function toIndex(tIndex) { 
 var dis = -(tIndex * width); 
 var start = parseInt(imgWrap.style.transform.slice(12)); 
 // 动画移动 
 imgWrap.tweenTranslateXAnimate(start, dis, function () { 
 setTimeout(function () { 
  movex = dis; 
  if (tIndex === imgSize) { 
  imgWrap.style.transform = &#39;translate3d(0, 0, 0)&#39;; 
  movex = 0; 
  } 
  if (tIndex === -1) { 
  imgWrap.style.transform = &#39;translate3d(&#39; + width * (1 - imgSize) + &#39;px, 0, 0)&#39;; 
  movex = -width * (imgSize - 1); 
  } 
 }, 0); 
 }); 
 } 
 
 /** 
 * 处理各种触摸事件 ,包括 touchstart, touchend, touchmove, touchcancel 
 * @param {Event} evt 回调函数中系统传回的 js 事件对象 
 * */ 
 function touch(evt) { 
 var touch = evt.targetTouches[0]; 
 var tar = evt.target; 
 var index = parseInt(tar.getAttribute(&#39;data-index&#39;)); 
 if (evt.type === &#39;touchmove&#39;) { 
 var di = parseInt(touch.pageX - lastPX); 
 endX = touch.pageX; 
 movex += di; 
 imgWrap.style.webkitTransform = &#39;translate3d(&#39; + movex + &#39;px, 0, 0)&#39;; 
 lastPX = touch.pageX; 
 } 
 if (evt.type === &#39;touchend&#39;) { 
 var minus = endX - startX; 
 t2 = new Date().getTime() - t1; 
 if (Math.abs(minus) > 0) { // 有拖动操作 
  if (Math.abs(minus) < width * 0.4 && t2 > 500) { // 拖动距离不够,返回! 
  toIndex(index); 
  } else { // 超过一半,看方向 
  console.log(minus); 
  if (Math.abs(minus) < 20) { 
  console.log(&#39;距离很短&#39; + minus); 
  toIndex(index); 
  return; 
  } 
  if (minus < 0) { // endX < startX,向左滑动,是下一张 
  toIndex(index + 1) 
  } else { // endX > startX ,向右滑动, 是上一张 
  toIndex(index - 1) 
  } 
  } 
 } else { //没有拖动操作 
 
 } 
 } 
 if (evt.type === &#39;touchstart&#39;) { 
 lastPX = touch.pageX; 
 startX = lastPX; 
 endX = startX; 
 t1 = new Date().getTime(); 
 } 
 return false; 
 } 
 
 imgWrap.addEventListener(&#39;touchstart&#39;, touch, false); 
 imgWrap.addEventListener(&#39;touchmove&#39;, touch, false); 
 imgWrap.addEventListener(&#39;touchend&#39;, touch, false); 
 imgWrap.addEventListener(&#39;touchcancel&#39;, touch, false); 
 
 }(); 
 
</script>

The most critical parameter in the touch event is the pageX parameter, which records the position of x.

Of course this is just a demo and needs further optimization and encapsulation so that we can use it in real projects.

This demo only provides an idea to solve the problem. With this idea, I believe that various complex needs can also be solved...

The above is the detailed content of Detailed explanation of how to use native JS to achieve mobile web carousel effect. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

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: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

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.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

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

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

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.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

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.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

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.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists

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.