-->

How to use FastClick.js with Phonegap and JQM?

2019-02-04 19:21发布

问题:

I've tried a few different ways to remove the 300ms delay due to the webkit browsers handling of touch events. The library, FastClick.js, seems to be the preferred method, yet I'm having a little trouble implementing it. I've included it and also added an event listener, but I don't know if I've added the listener correctly. Should this be working or am I failing to do something? Thank you!

Consider the code below, where

<!DOCTYPE html>
<html>
<head>
    <title>
        Calculator
    </title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-   scale=1.0, user-scalable=no;">
    <meta charset="utf-8">
    <script type="text/javascript" charset="utf-8" src="phonegap.js">
    </script>
    <<script type='application/javascript' src='js/fastclick.js'></script>
    <script type="text/javascript">

    function onBodyLoad()
    {       
        document.addEventListener("deviceready", onDeviceReady, false);
         $(function() {
         FastClick.attach(document.body);
         });

    }

    function onDeviceReady()
    {


    }
    </script>
    <script>
                window.addEventListener('load', function() {
                new FastClick(document.body);
                }, false);
    </script>

    <link rel="stylesheet" href="./css/jquerymobile.css" type="text/css">
    <link rel="stylesheet" href="./css/jquerymobile.nativedroid.css" type="text/css">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">   
</head>

<body onload="onBodyLoad()">
    <!--START OF PAGE 1-->
    <div data-role="page" data-theme='b' id="one">
        <div data-role="content">
              <a href="#one" data-transition="none" data-
        </div>
    </div>
</body>

回答1:

Try with the below code.

function onBodyLoad()
{       
    document.addEventListener("deviceready", onDeviceReady, false);

}

function onDeviceReady()
{
  alert('test');
  FastClick.attach(document.body);
}

If everything is fine, you should be able to see the alert box.

Have a look on http://phonegap-tips.com/articles/fast-touch-event-handling-eliminate-click-delay.html



回答2:

I can also think of another solution.. Note: I have not personally tried this..

$(document).on('pageinit', '.ui-page', function (event, data)
{
   FastClick.attach(activePage);
});


回答3:

You should use the built in vclick event in jQuery Mobile - same idea as FastClick.

$(document).on('vclick', '#someButton', function(){ 

});

Source: How to use FastClick with jQuery Mobile the right way?