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

前端 未结 8 1918
抹茶落季
抹茶落季 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条回答
  • 2020-11-22 13:13

    I was a little bit worried about using only touchmove for my project, since it only seems to fire when your touch moves from one location to another (and not on the initial touch). So I combined it with touchstart, and this seems to work very well for the initial touch and any movements.

    <script>
    
    function doTouch(e) {
        e.preventDefault();
        var touch = e.touches[0];
    
        document.getElementById("xtext").innerHTML = touch.pageX;
        document.getElementById("ytext").innerHTML = touch.pageY;   
    }
    
    document.addEventListener('touchstart', function(e) {doTouch(e);}, false);
    document.addEventListener('touchmove', function(e) {doTouch(e);}, false);
    
    </script>
    
    X: <div id="xtext">0</div>
    Y: <div id="ytext">0</div>
    
    0 讨论(0)
  • 2020-11-22 13:18

    You can use .on() to capture multiple events and then test for touch on the screen, e.g.:

    $('#selector')
    .on('touchstart mousedown', function(e){
      e.preventDefault();
      var touch = e.touches[0];
      if(touch){
        // Do some stuff
      }
      else {
        // Do some other stuff
      }
    });
    
    0 讨论(0)
  • 2020-11-22 13:18

    jQuery Core doesn't have anything special, but you can read on jQuery Mobile Events page about different touch events, which also work on other than iOS devices as well.

    They are:

    • tap
    • taphold
    • swipe
    • swipeleft
    • swiperight

    Notice also, that during scroll events (based on touch on mobile devices) iOS devices freezes DOM manipulation while scrolling.

    0 讨论(0)
  • 2020-11-22 13:24

    I just tested benmajor's GitHub jQuery Touch Events plugin for both 1.4 and 1.7+ versions of jQuery. It is lightweight and works perfectly with both on and bind while providing support for an exhaustive set of touch events.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 13:26

    The simplest approach is to use a multitouch JavaScript library like Hammer.js. Then you can write code like:

    canvas
        .hammer({prevent_default: true})
        .bind('doubletap', function(e) { // And double click
            // Zoom-in
        })
        .bind('dragstart', function(e) { // And mousedown
            // Get ready to drag
        })
        .bind('drag', function(e) { // And mousemove when mousedown
            // Pan the image
        })
        .bind('dragend', function(e) { // And mouseup
            // Finish the drag
        });
    

    And you can keep going. It supports tap, double tap, swipe, hold, transform (i.e., pinch) and drag. The touch events also fire when equivalent mouse actions happen, so you don't need to write two sets of event handlers. Oh, and you need the jQuery plugin if you want to be able to write in the jQueryish way as I did.

    0 讨论(0)
提交回复
热议问题