jquery's method of supporting sliding events on mobile devices
Original jquery does not support the mobile web as well, such as sliding events. In the use of Drop-down refresh, rotation map, and other operations, you need to use the sliding event, here is recommended a Jquery plug-in touchswipe-jquery-plugin.
GitHub Address: Https://github.com/mattbryson/TouchSwipe-Jquery-Plugin
GitHub and official documentation have been described in detail, Here is a brief look at the basic function of sliding operation.
$ (function () { //enable swiping ... $ ("#test"). Swipe ( { //generic swipe handler for all directions swipe:function (event, direction, Distance, duration, fingercount, fingerdata) { $ (This). Text ("you swiped " + direction ); }, //default is 75px, set to 0 for demo so&nBsp;any distance triggers swipe
threshold:0 }); });
After the introduction of the jquery library and the TouchSwipe, you can use the code above to detect the slide event, where the direction refers to the direction, with ' up ', ' down ', ' left ', ' right ' and distance to the sliding distance. The following threshold refers to the trigger range, the minimum number of slides to trigger, note that the actual use, generally can not be 0, because set to 0 will cover click, tap and other clicks. If you want to detect the entire page of the slide, you can change the #test to body.
native JavaScript implements mobile Web site sliding event code
Under my test success.
Test Platform: Samsung s5830i
Operating system: Android 2.3.6
Browser: UC Browser
HTML standard: HTML5
Three events were tested: Touchstart, Touchmove, and Touchend, and the coordinates of the touch point on the page were obtained, and the left and right slides were judged according to the coordinates. The
code is as follows:
<!-- HTML5 --> <! doctype html> <html> <head> <title>touchevent Test </title> <meta charset= "GBK" >
</head> <body> <h2>touchevent Test </h2> <br /> <div id= "Version" Style= "Border:2px solid black;backGround-color:yellow "></div> <br /> <br /> <br /> <br /> <br / > <br /> <div id= "Result" style= "border:2px solid red; color:red;" > does not trigger event! </div> <div id= "Test" style= "border:2px solid red" > <ul > <li id= "Li1" > Test entries 1</li> <li id = "Li2" > Test entry 2</li> <li id= "Li3" > Test entries 3</li> <li id= "LI4" > Test entries 4</li> <li id= "LI5" > Test entry 5</li > &NBSP;&Nbsp; <li id= "Li6" > Test entries 6</li> <li id= "Li7" > Test entry 7</li > <li id= "Li8" > Test entries 8</li> <li id = "Li9" > Test entry 9</li> <li id= "li10" > Test entries 10</li> <li id= "Li11"> Test Entry 11</li> <li id= "li12" > Test entries 12</li> <li id= "li13" > Test entries 13</li> <li id= "li14" > Test entry 14</li > <li id= "li15" > Test entries 15</li> <li id = "LI16" > Test entry 16</li> &Nbsp; <li id= "li17" > Test entries 17</li> <li id = "Li18" > Test entry 18</li>
<li id= "li19" > Test entry 19</li> <li id= "li20" > Test entries 20</li> </ul> </div> <script tYpe= "Text/javascript" > //global variable, touch start position
var startX = 0, startY = 0;
//touchstart Events Function touchsatrtfunc (evt) { try
{
//evt.preventdefault () //Prevent browser zoom, scroll bar scrolling, etc.
var touch = evt.touches[0]; //Get First Contact
var x = number (Touch.pagex); //page contact X coordinate
var y = number (touch.pagey); //page Contact y-coordinate //Record Contact initial position startX = x; startY = y; var text = ' Touchstart event triggers: (' + x + ', ' + y + ') '; document.getelementbyid ("Result" ) .innerhtml = text; } catch (e) { alert (' Touchsatrtfunc: ' + e.message '); } } //touchmove event, this event could not get coordinates function touchmovefunc (evt) { try { //evt.preventdefault () //prevents the browser from zooming, scrolling, and so on when touching var touch = evt.touches[0]; //Get First Contact var x = number (Touch.pagex); //page contact X coordinate var y = number (touch.pagey); //page Contact y-coordinate var text = ' Touchmove Event Trigger: (' + x + ', ' + y + ') '; //judgment Sliding Direction if (x - startx != 0) { text += ' <br/> left-right slideMove '; } if (y - starty != 0) { text += ' <br/> sliding up and down '; } document.getelementbyid ("Result"). InnerHTML = text; } catch (e) { alert (' Touchmovefunc: ' + e.message '); } } //touchend Events &nbsP;function touchendfunc (evt) { try { //evt.preventdefault () //Prevent browser zoom, scroll bar scrolling, etc. var text = ' Touchend Event trigger '; document.getelementbyid ("result") .innerhtml = text; } catch (e) { alert (' TouchEndFunc: ' + E.message); } } //Binding Events function bindevent () { &nbsP;document.addeventlistener (' Touchstart ', touchsatrtfunc, false); Document.addeventlistener (' Touchmove ', touchmovefunc, false); Document.addeventlistener (' Touchend ', touchendfunc, false); } // Determine whether to support touch events function istouchdevice () { &nbSp; document.getelementbyid ("version") .innerhtml = navigator.appversion; try { document.createevent ("TouchEvent"); alert (" Support TouchEvent Events! "); bindevent (); // Binding Events } catch (e) { Alert ("Does not support TouchEvent events!") " + e.message); } } window.onload = istouchdevice; </script> </body> </html>
is the way the jquery mobile implementation slides up and down:
jquery Mobile left and right events: swipe (triggered when sliding horizontally above 30px), swipeleft (left-sliding), Swiperight (sliding right)
jquery Mobile Vertical Slide event: scrollstart (scroll up), Scrollend (scrolling stop)
jquery Mobile Touch Click event: Tap (Quick Touch), taphold (Touch and hold above 750ms trigger)
<! doctype html> <html> <head> <title>ajax Test </title> <meta charset= "GBK" > <meta name= "Viewport" content= "width=device-width, Initial-scale=1 "> < Link rel= "stylesheet" href= "Jquery-mobile/jquery.mobile-1.2.0.min.css"/> <link rel= "stylesheet" href= " Jquery-mobile/jquery.mobile.structure-1.2.0.min.css "/> &Nbsp; <script src= "Jquery-mobile/jquery-1.8.2.min.js" ></script> <script src= "jquery-mobile/ Jquery.mobile-1.2.0.min.js "></script> < /head> <body> <div data-role= "page" data-theme= "B" > <div data-role = "header" ></div> <div data-role= "Content" > <script> &nbSp; //scrollstart Events function Scrollstartfunc (evt) { try { var target = $ (evt.target); while (target.attr ("id") == undefined) { target = target.parent (); } //get contact Target id attribute value &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;VAR&NBSP;TARGETID&NBSP;=&NBSP;TARGET.ATTR ("id"); alert ("targetId: " + targetid); } catch (e) { alert (' Myscrollfunc: ' + e.message; } } &NBSP;&Nbsp; function myinit () { //binding up and down events $ ("#myul"). Bind (' Scrollstart ', function () { scrollstartfunc (event);  ); } window.onload = myinit; </script> <!-- ListView Test -->
<ul id= "Myul" data-role= "ListView" data-inset= "true" > <li data-role= "List-divider" > Information list </li> <li id= "Li1" data-role= "Fieldcontain" > Information 1</li> <li id= "Li2" data-role= " Fieldcontain ">Information 2</li> <li id= "Li3" data-role= "Fieldcontain" > Information 3</li> <li id= "Li4" data-role= "Fieldcontain" > Information 4</li> <li id= "Li5" data-role= "Fieldcontain" > Information 5</li> <li id= "Li6" data-role= "Fieldcontain" > Information 6</li> <li id= "Li7" data-role= " Fieldcontain"> Information 7</li> <li id= "Li8" data-role= "Fieldcontain" > Information 8</li> <li id= "Li9" data-role= "Fieldcontain" > Information 9</li> <li id= "li10" data-role= "Fieldcontain" > Information 10</li> </ul> </div> </body> </html>