Detecting that the browser has no mouse and is touch-only

前端 未结 24 1991
终归单人心
终归单人心 2020-11-27 09:35

I\'m developing a webapp (not a website with pages of interesting text) with a very different interface for touch (your finger hides the screen when you click) and mouse (re

24条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-27 09:55

    I spent hours figuring out this problem for my Phonegap app and I came up with this hack. It generates a console warning if the event triggered is a "passive" event, meaning it doesn't actuate any change, but it works! I would be interested in any ideas to improve or a better method. But this effectively allows my to use $.touch() universally.

    $(document).ready(function(){
      $("#aButton").touch(function(origElement, origEvent){
        console.log('Original target ID: ' + $(origEvent.target).attr('id'));
      });
    });
    
    $.fn.touch = function (callback) {
        var touch = false;
        $(this).on("click", function(e){
            if (!touch)
            {
                console.log("I click!");
                let callbackReal = callback.bind(this);
                callbackReal(this, e);
            }else{
                touch = true;
            }
            touch = false;
        });
        $(this).on("touchstart", function(e){
            if (typeof e.touches != typeof undefined)
            {
                e.preventDefault();
                touch = true;
                console.log("I touch!");
                let callbackReal = callback.bind(this);
                callbackReal(this, e);
            }
        });
    }
    
    
    

提交回复
热议问题