How to bind both Mousedown and Touchstart, but not respond to both? Android, JQuery

后端 未结 9 763
无人及你
无人及你 2020-12-04 14:24

Working on a website that is also viewable on mobile and need to bind an action on both touchstart and mousedown.

Looks like this

 $(\"#roll\").bind         


        
相关标签:
9条回答
  • 2020-12-04 14:40
    element.on('touchstart mousedown', function(e) {
        e.preventDefault();
        someAction();
    });
    

    preventDefault cancels the event, as per specs

    You get touchstart, but once you cancel it you no longer get mousedown. Contrary to what the accepted answer says, you don't need to call stopPropagation unless it's something you need. The event will propagate normally even when cancelled. The browser will ignore it, but your hooks will still work.

    Mozilla agrees with me on this one:

    calling preventDefault() on a touchstart or the first touchmove event of a series prevents the corresponding mouse events from firing

    EDIT: I just read the question again and you say that you already did this and it didn't fix the Android default browser. Not sure how the accepted answer helped, as it does the same thing basically, just in a more complicated way and with an event propagation bug (touchstart doesn't propagate, but click does)

    0 讨论(0)
  • 2020-12-04 14:41

    I have been using this function:

    //touch click helper
    (function ($) {
        $.fn.tclick = function (onclick) {
    
            this.bind("touchstart", function (e) { 
                onclick.call(this, e); 
                e.stopPropagation(); 
                e.preventDefault(); 
            });
    
            this.bind("click", function (e) { 
               onclick.call(this, e);  //substitute mousedown event for exact same result as touchstart         
            });   
    
            return this;
        };
    })(jQuery);
    

    UPDATE: Modified answer to support mouse and touch events together.

    0 讨论(0)
  • 2020-12-04 14:50

    I recommend you try jquery-fast-click. I tried the other approach on this question and others. Each fixed one issue, and introduced another. fast-click worked the first time on Android, ios, desktop, and desktop touch browsers (groan).

    0 讨论(0)
  • 2020-12-04 14:54

    I think the best way is :

    var hasTouchStartEvent = 'ontouchstart' in document.createElement( 'div' );
    
    document.addEventListener( hasTouchStartEvent ? 'touchstart' : 'mousedown', function( e ) {
        console.log( e.touches ? 'TouchEvent' : 'MouseEvent' );
    }, false );
    
    0 讨论(0)
  • 2020-12-04 14:57

    Fixed using this code

    var mobile   = /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent); 
    var start = mobile ? "touchstart" : "mousedown";
    $("#roll").bind(start, function(event){
    
    0 讨论(0)
  • 2020-12-04 15:01

    Write this code and add j query punch touch js.it will work simulate mouse events with touch events

    function touchHandler(event)
    {
        var touches = event.changedTouches,
            first = touches[0],
            type = "";
             switch(event.type)
        {
            case "touchstart": type = "mousedown"; break;
            case "touchmove":  type="mousemove"; break;        
            case "touchend":   type="mouseup"; break;
            default: return;
        }
    
        var simulatedEvent = document.createEvent("MouseEvent");
        simulatedEvent.initMouseEvent(type, true, true, window, 1, 
                                  first.screenX, first.screenY, 
                                  first.clientX, first.clientY, false, 
                                  false, false, false, 0/*left*/, null);
        first.target.dispatchEvent(simulatedEvent);
        event.preventDefault();
    }
    
    function init() 
    {
        document.addEventListener("touchstart", touchHandler, true);
        document.addEventListener("touchmove", touchHandler, true);
        document.addEventListener("touchend", touchHandler, true);
        document.addEventListener("touchcancel", touchHandler, true);    
    } 
    
    0 讨论(0)
提交回复
热议问题