Determine the differences between the mobile phone and the pc, and determine the events on the pc.

Source: Internet
Author: User

Determine the differences between the mobile phone and the pc, and determine the events on the pc.
Determine whether it is a mobile phone:
Function isMobile () {var sUserAgent = navigator. userAgent. toLowerCase (), bIsIpad = sUserAgent. match (/ipad/I) = "ipad", bIsIphoneOs = sUserAgent. match (/iphone OS/I) = "iphone OS", bIsMidp = sUserAgent. match (/midp/I) = "midp", bIsUc7 = sUserAgent. match (/rv: 1.2.3.4/I) = "rv: 1.2.3.4", bIsUc = sUserAgent. match (/ucweb/I) = "ucweb", bIsAndroid = sUserAgent. match (/android/I) = "android", bIsCE = sUserAgent. match (/windows ce/I) = "windows ce", bIsWM = sUserAgent. match (/windows mobile/I) = "windows mobile", bIsWebview = sUserAgent. match (/webview/I) = "webview"; return (bIsIpad | bIsIphoneOs | bIsMidp | bIsUc7 | bIsUc | bIsAndroid | bIsCE | bIsWM );}

Determine the event to use: var touchStart, touchMove, touchEnd; touchStart = isMobile ()? 'Touchstart': 'mouselow'; touchMove = isMobile ()? 'Touchmove ': 'mousemove'; touchEnd = isMobile ()? 'Touchend': 'mouseup ';

Processing of three events: touchstart: function (e) {var e = e | window. event; // determine which event to use stopDefault (e); // different browsers prevent different if (isMobile ()) {// for mobile phone var touch = e. touches [0]; this. y1 = touch. pageY} else {this. y1 = e. pageY; // if it is not a mobile phone} this. y2 = 0 ;}, touchmove: function (e) {var e = e | window. event; stopDefault (e); if (isMobile () {var touch = e. touches [0]; this. y2 = touch. pageY;} else {this. y2 = e. pageY ;}},
Touchend: function (e) {var e = e | window. event; stopDefault (e); if (this. y2 = 0) {return;} var diffY = this. y2-this.y1; if (diffY> 50) {this. doNext ();} else if (diffY <-50) {this. doPrev ();} this. y1 = 0, this. y2 = 0 ;},
Default method for blocking browser events: function stopDefault (e) {var e = e | window. event; if (e. preventDefault) {e. preventDefault ();} else {e. returnValue = false ;}}

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.