How can I recognize touch events using jQuery in Safari for iPad? Is it possible?

前端 未结 8 1999
抹茶落季
抹茶落季 2020-11-22 13:09

Is it possible to recognize touch events on the iPad\'s Safari browser using jQuery?

I used mouseOver and mouseOut events in a web application. Are there any simila

8条回答
  •  萌比男神i
    2020-11-22 13:25

    Using touchstart or touchend alone is not a good solution, because if you scroll the page, the device detects it as touch or tap too. So, the best way to detect a tap and click event at the same time is to just detect the touch events which are not moving the screen (scrolling). So to do this, just add this code to your application:

    $(document).on('touchstart', function() {
        detectTap = true; // Detects all touch events
    });
    $(document).on('touchmove', function() {
        detectTap = false; // Excludes the scroll events from touch events
    });
    $(document).on('click touchend', function(event) {
        if (event.type == "click") detectTap = true; // Detects click events
           if (detectTap){
              // Here you can write the function or codes you want to execute on tap
    
           }
     });
    

    I tested it and it works fine for me on iPad and iPhone. It detects tap and can distinguish tap and touch scroll easily.

提交回复
热议问题