Click event called twice on touchend in iPad

后端 未结 3 1562
伪装坚强ぢ
伪装坚强ぢ 2020-11-30 00:44

I am using jquery animate for the slide. I have an arrow at the end of the slide and given the click event on that arrow. The working of it is to move one item inside the si

相关标签:
3条回答
  • 2020-11-30 01:16

    iPad both understands touchstart/-end and mousestart/-end.

    Is gets fired like this:

    ┌─────────────────────┬──────────────────────┬─────────────────────────┐
    │Finger enters tablet │ Finger leaves tablet │ Small delay after leave │
    ├─────────────────────┼──────────────────────┼─────────────────────────┤
    │touchstart           │ touchend             │ mousedown               │
    │                     │                      │ mouseup                 │
    └─────────────────────┴──────────────────────┴─────────────────────────┘
    

    You have to detect if the user is on a tablet and then relay on the touch start things...:

    /********************************************************************************
    * 
    * Dont sniff UA for device. Use feature checks instead: ('touchstart' in document) 
    *   The problem with this is that computers, with touch screen, will only trigger 
    *   the touch-event, when the screen is clicked. Not when the mouse is clicked.
    * 
    ********************************************************************************/
    var isIOS = ((/iphone|ipad/gi).test(navigator.appVersion));
    var myDown = isIOS ? "touchstart" : "mousedown";
    var myUp = isIOS ? "touchend" : "mouseup";
    

    and then bind it like this:

    $('#next_item').bind(myDown, function(e) { 
    

    You can also make a event that takes care of it, see:

    http://benalman.com/news/2010/03/jquery-special-events/

    Basic normalized down event:

    $.event.special.myDown = {
        setup: function() {
            var isIOS = ((/iphone|ipad/gi).test(navigator.appVersion));
            var myDown = isIOS ? "touchstart" : "mousedown";
            $(this).bind(myDown + ".myDownEvent", function(event) {
                event.type = "myDown";
                $.event.handle.call(this, event);
            });
        },
        teardown: function() {
            $(this).unbind(".myDownEvent");
        }
    };
    

    After jQuery 1.9.0 $.event.handle changed name to $.event.dispatch, to support both you can write use this fallback:

    // http://stackoverflow.com/questions/15653917/jquery-1-9-1-event-handle-apply-alternative
    // ($.event.dispatch||$.event.handle).call(this, event);
    $.event.special.myDown = {
        setup: function() {
            var isIOS = ((/iphone|ipad/gi).test(navigator.appVersion));
            var myDown = isIOS ? "touchstart" : "mousedown";
            $(this).bind(myDown + ".myDownEvent", function(event) {
                event.type = "myDown";
                ($.event.dispatch||$.event.handle).call(this, event);
            });
        },
        teardown: function() {
            $(this).unbind(".myDownEvent");
        }
    };
    
    0 讨论(0)
  • 2020-11-30 01:25

    Be careful with using a UA sniffer for iPad/iPod. You're ditching all Android devices with a solution like that! A better solution is to detect the touch support, it will work on all Mobile/Tablet devices:

    var isTouchSupported = "ontouchend" in document;
    
    0 讨论(0)
  • 2020-11-30 01:33

    Extending H Dog answer. Here is the code that worked for me.

     if (isTouchSupported) {
        $('#playanimationbtn').off("mouseup");
        $('#stopanimationbtn').off("mouseup");
        $('#playanimationbtn').off("mousedown");
        $('#stopanimationbtn').off("mousedown");
        $('#playanimationbtn').off("click");
        $('#stopanimationbtn').off("click");
        document.getElementById("playanimationbtn").addEventListener("touchend", PlayAnimation);
        document.getElementById("stopanimationbtn").addEventListener("touchend", StopAnimation);
    } else {
        $('#playanimationbtn').off("touchstart");
        $('#playanimationbtn').off("touchend");
        $('#playanimationbtn').off("touchmove");
        $('#playanimationbtn').off("touchend");
        $('#playanimationbtn').off("touchleave");
        $('#stopanimationbtn').off("touchstart");
        $('#stopanimationbtn').off("touchend");
        $('#stopanimationbtn').off("touchmove");
        $('#stopanimationbtn').off("touchend");
        $('#stopanimationbtn').off("touchleave");
        document.getElementById("playanimationbtn").addEventListener("click", PlayAnimation);
        document.getElementById("stopanimationbtn").addEventListener("click", StopAnimation);
    }
    
    0 讨论(0)
提交回复
热议问题