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: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Apr 11, 2025 am 08:23 AM

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)Apr 11, 2025 am 08:22 AM

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript: Exploring the Versatility of a Web LanguageJavaScript: Exploring the Versatility of a Web LanguageApr 11, 2025 am 12:01 AM

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The Evolution of JavaScript: Current Trends and Future ProspectsThe Evolution of JavaScript: Current Trends and Future ProspectsApr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version