Solve the problem of sliding elements on Android

Source: Internet
Author: User

Move around, swipe up and down:
When you need a page swipe in the page, and you need to swipe up or down on an element on the page, the swipe event provided directly with Zepto is not directly achievable for the following reasons:
(1) Touchend will not be triggered on Android low-end machine.
(2) Event sniffing through event bubbling mechanism for swipe events in Zepto
Monitor Touchstart, Touchmove, touchend on document and then decide whether to swipe up or down or left or right. The touch event of the document is then determined by which element the touch event bubbles up, and then the sliding event on that element is triggered.
Binding the swipe event directly on an element bubbles to the body, and if you swipe up or down it will trigger page scrolling, causing the page to scroll when the element is sliding.
Sliding event Triggering process:
(1) When IOS triggers "swipe", the following event occurs: Touchstart-> Touchmove * multiple times-> Touchend-> Scroll
(2) Touchend does not trigger on android4.x-> Touchmove, the touchend will not be triggered until the default event is blocked
Touchstart-> Touchmove-> Touchcancel-> Scroll * multiple times
And there will be a click on the low version of Android, there will be poor displacement phenomenon.

Therefore, the following script overrides the swipe event to resolve the above problem.

/** * Sliding events Android 4.0 won't fire touchend event*/Define (function(require,exports,module) {functionswipeevent (selector) { This. selector =selector;  This. Swipeleftcallback =function(){};  This. Swiperightcallback =function(){};  This. Swipeupcallback =function(){};  This. Swipedowncallback =function(){};  This. _initswipe (selector); } SwipeEvent.prototype.bind=function(evtname,callback) {Switch(evtname) { Case"Swipeleft":             This. Swipeleftcallback =callback;  Break;  Case"Swiperight":             This. Swiperightcallback =callback;  Break;  Case"Swipeup":             This. Swipeupcallback =callback;  Break;  Case"Swipedown":             This. Swipedowncallback =callback;  Break;    }    }; Swipeevent.prototype._initswipe=function(selector) {varSelf = This; varStartX, Starty; varEndX, EndY; varDistancex, Distancey; $ (selector). On (' Touchstart ',function(event) {StartX= Event.targettouches[0].clientx;//the list of fingers located on the current DOM element. Starty = Event.targettouches[0].clienty; }). On ("Touchmove",function(event) {EndX= Event.changedtouches[0].clientx; EndY= Event.changedtouches[0].clienty;//a list of fingers that involve the current event. Distancex = Math.Abs (StartX-endx); Distancey= Math.Abs (Starty-EndY); if(Distancex > 1 | | distancey > 1) {//The decision is to swipe left or right to prevent the default event from taking effect Android4 touchendEvent.preventdefault ();            } event.stoppropagation (); }). On (' Touchend ',function(event) {EndX= Event.changedtouches[0].clientx; EndY= Event.changedtouches[0].clienty; Distancex= Math.Abs (StartX-endx); Distancey= Math.Abs (Starty-EndY); if(Distancex > | | distancey > 10) {//Android 4. X, a single click will also result in a displacement difference                    if(Distancex >= Distancey) {//Swipe left and right                        if(Startx-endx > 0) {Self.swipeleftcallback (event); }Else if(Startx-endx < 0) {Self.swiperightcallback (event); }                    }Else{                        if(Starty-endy > 0) {Self.swipeupcallback (event); }Else if(Starty-endy < 0) {Self.swipedowncallback (event);    }                    }                }            });    }; Module.exports=swipeevent;});

Reference: http://www.cnblogs.com/ytu2010dt/p/5767491.html

Http://www.myexception.cn/web/1874295.html

Solve the problem of sliding elements on Android

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.