


Detailed 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
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="images/banner_3.jpg" data-index="-1"> <img src="images/banner_1.jpg" data-index="0"> <img src="images/banner_2.jpg" data-index="1"> <img src="images/banner_3.jpg" data-index="2"> <img src="images/banner_1.jpg" data-index="3"> </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 = 'translate3d(' + dis + 'px, 0, 0)'; if (vv > 0 && parseInt(this.style.transform.slice(12)) >= end) { this.style.transform = 'translate3d(' + parseInt(dis) + 'px, 0, 0)'; clearInterval(this.timer); callback && callback(); } if (vv < 0 && parseInt(this.style.transform.slice(12)) <= end) { this.style.transform = 'translate3d(' + parseInt(dis) + 'px, 0, 0)'; 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('imgWrap'); 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('#imgWrap img'); // 所有轮播图节点数组 NodeList // 给图片设置合适的left值, 注意 querySelectorAll返回 NodeList, 具有 forEach方法 nodeList.forEach(function (node, index) { node.style.left = (index - 1) * width + 'px'; }); /** * 移动图片到当前的 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 = 'translate3d(0, 0, 0)'; movex = 0; } if (tIndex === -1) { imgWrap.style.transform = 'translate3d(' + width * (1 - imgSize) + 'px, 0, 0)'; 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('data-index')); if (evt.type === 'touchmove') { var di = parseInt(touch.pageX - lastPX); endX = touch.pageX; movex += di; imgWrap.style.webkitTransform = 'translate3d(' + movex + 'px, 0, 0)'; lastPX = touch.pageX; } if (evt.type === 'touchend') { 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('距离很短' + minus); toIndex(index); return; } if (minus < 0) { // endX < startX,向左滑动,是下一张 toIndex(index + 1) } else { // endX > startX ,向右滑动, 是上一张 toIndex(index - 1) } } } else { //没有拖动操作 } } if (evt.type === 'touchstart') { lastPX = touch.pageX; startX = lastPX; endX = startX; t1 = new Date().getTime(); } return false; } imgWrap.addEventListener('touchstart', touch, false); imgWrap.addEventListener('touchmove', touch, false); imgWrap.addEventListener('touchend', touch, false); imgWrap.addEventListener('touchcancel', 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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.

PHP Game Requirements Implementation Guide With the popularity and development of the Internet, the web game market is becoming more and more popular. Many developers hope to use the PHP language to develop their own web games, and implementing game requirements is a key step. This article will introduce how to use PHP language to implement common game requirements and provide specific code examples. 1. Create game characters In web games, game characters are a very important element. We need to define the attributes of the game character, such as name, level, experience value, etc., and provide methods to operate these

Implementing exact division operations in Golang is a common need, especially in scenarios involving financial calculations or other scenarios that require high-precision calculations. Golang's built-in division operator "/" is calculated for floating point numbers, and sometimes there is a problem of precision loss. In order to solve this problem, we can use third-party libraries or custom functions to implement exact division operations. A common approach is to use the Rat type from the math/big package, which provides a representation of fractions and can be used to implement exact division operations.

Cockpit is a web-based graphical interface for Linux servers. It is mainly intended to make managing Linux servers easier for new/expert users. In this article, we will discuss Cockpit access modes and how to switch administrative access to Cockpit from CockpitWebUI. Content Topics: Cockpit Entry Modes Finding the Current Cockpit Access Mode Enable Administrative Access for Cockpit from CockpitWebUI Disabling Administrative Access for Cockpit from CockpitWebUI Conclusion Cockpit Entry Modes The cockpit has two access modes: Restricted Access: This is the default for the cockpit access mode. In this access mode you cannot access the web user from the cockpit

I'm really sorry that I can't provide real-time programming guidance, but I can provide you with a code example to give you a better understanding of how to use PHP to implement SaaS. The following is an article within 1,500 words, titled "Using PHP to implement SaaS: A comprehensive analysis." In today's information age, SaaS (Software as a Service) has become the mainstream way for enterprises and individuals to use software. It provides a more flexible and convenient way to access software. With SaaS, users don’t need to be on-premises
