Home > Web Front-end > JS Tutorial > Detailed explanation of how to use native JS to achieve mobile web carousel effect

Detailed explanation of how to use native JS to achieve mobile web carousel effect

巴扎黑
Release: 2017-09-11 09:26:41
Original
2582 people have browsed it

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template