Cross-slide list effect for mobile touch events

Source: Internet
Author: User

It is not difficult to realize the horizontal sliding effect on the mobile side, and it is easy to realize the principle and business logic of the implementation. Principle: Touchstart (Finger press the moment to get relative to the position of the page)--"touchmove (how much the finger moves, how much the element moves accordingly).

Next, let's talk about the implementation logic:

It's actually the finger dragging the list in which direction to move how many pixels, and set the left and right drag boundary values.

Attach code and comments:

<div id= "Common_wrap" class= "Common-wrap" >

Set Style:

*{padding:0;margin:0;} Html,body{width:100%;overflow:hidden;}. common-wrap{width:100%;height:105px;border-bottom:8px solid #eee;}. common-kit__h4{font-size:14px;margin-top:17px;margin-left:8px;letter-spacing:0.2px;}. common-kit{width:100%;p osition:relative;}. Common-kit__list{width:558px;position:absolute; margin-top:10px;height:80px;}. Common-kit__list li{position:relative;list-style:none;width:80px;height:80px;background-color: #eee; float:left; margin-left:13px;}. Common-kit__list Li A{text-decoration:none;font-size:12px;position:absolute;top:50%;transform:translatey (-50%); text-align:center;padding:0px 12px; }.common-kit__list li:first-child{margin-left:8px;}

JS Code:

function Slidecommonkit () {var mask = document.getElementById (' mask '); var common_kit__list=document.queryselector ('.        Common-kit__list ');        var startposition, Endposition, DeltaX, DeltaY, movelength;    var commonkitleft; /* The finger presses the moment to trigger the Touchstart event */Mask.addeventlistener (' Touchstart ', function (e) {commonkitleft=parseint (common_kit__l        Ist.style.left);  var touch = e.targettouches[0]; Targettouches the list of finger actions on the current DOM element startposition = {//takes the first finger on the screen relative to the page's coordinates X:touch.pagex, y:    Touch.pagey}});        /* Finger move triggers touchmove event */Mask.addeventlistener (' Touchmove ', function (e) {var touch = e.targettouches[0]; Endposition = {X:touch.pagex, y:touch.pagey} deltax = Endposition.x-startpositio   n.x;   Move to the last coordinate x-coordinates at the beginning x movelength = Math.Abs (deltax); Get the distance of the moving x-direction/* function to the left */var swipeleft=function () {if (deltax< (-30)) {//here with 30 as the judge whether to trigger, if de LtaX is less than-30, the description moves left if (Math.Abs (commonkitleft) +movelength > (Common_kit__list.offsetwidth-win Dow.innerwidth)) {//To determine the critical value COMMON_KIT__LIST.STYLE.LEFT=WINDOW.INNERWIDTH-COMMON_KIT__LIST.OFFSETWI                dth+ ' px ';  }else{common_kit__list.style.left=commonkitleft-movelength+ ' px ';   Last left value-the distance moved (because the distance is a positive number, while moving to the right is negative, so use-)}}} swipeleft ();                Execute this function/* function to move right */var swiperight=function () {if (deltax>30) {/* mostly logical */                if (Commonkitleft+movelength > 0) {common_kit__list.style.left=0+ ' px ';                }else{common_kit__list.style.left=commonkitleft+movelength+ ' px ';            }}} swiperight (); });}; Slidecommonkit ();

As follows:

This allows for a horizontal slide list effect.

Note: Use touch event must download Touch.min.js <script src= "Touch.min.js" ></script> to use, native JS is not this event.

Cross-slide list effect for mobile touch events

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.