Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript检测ipad或iphone屏幕上的双击_Javascript_Ios_Ipad_Events - Fatal编程技术网

使用javascript检测ipad或iphone屏幕上的双击

使用javascript检测ipad或iphone屏幕上的双击,javascript,ios,ipad,events,Javascript,Ios,Ipad,Events,我想检测用户是否双击了ipad或iphone 是否存在提供此功能的javascript事件?您可以使用jQuery移动库构建应用程序:;它内置了很多触控事件。您还可以尝试jQuery touch library:在iPad上双击是很棘手的,因为默认情况下,浏览器会将此事件作为“缩放”事件吸收,如下所示: 但是,如果取消默认行为,您可以自己获取双击。下面是一个插件的示例,它可以为您实现这一点。technoweenie的jquery.doubletap可以 此链接: 似乎能够解决任务…一个基本想法

我想检测用户是否双击了ipad或iphone


是否存在提供此功能的javascript事件?

您可以使用jQuery移动库构建应用程序:;它内置了很多触控事件。您还可以尝试jQuery touch library:

在iPad上双击是很棘手的,因为默认情况下,浏览器会将此事件作为“缩放”事件吸收,如下所示:


但是,如果取消默认行为,您可以自己获取双击。下面是一个插件的示例,它可以为您实现这一点。

technoweenie的jquery.doubletap可以 此链接:
似乎能够解决任务…

一个基本想法是这样做:

  • 要创建双击(或双击)事件,您需要在
    onClick
    事件上创建代码

  • 您最希望双击/单击的原因是,您已经在
    onClick
    事件中附加了一些内容,并且需要在同一元素上使用不同的手势

  • Without element pointer: dbtapper(false,yourarguments)
    With element pointer: dbtapper(document.getElement*******,yourarguments)
    Alternatively, you can use an unique elem arg for each element:
                        dbtapper('omega',yourarg) or dbtapper(69,yourarg)
    Though this is not recommended if you have a huge amount of double tapping elements.
    
  • 这意味着您的
    onClick
    事件只应在确认
    setTimeout()
    后启动
    onClick
    事件

  • 因此,基本代码将使用
    setTimeout()
    命令启动附加到
    onClick
    事件的函数。第一次单击显示“启动计时器+使用
    setTimeout()
    运行函数,时间为..500毫秒。第二次单击时,您将检查第二次单击是否在特定的时间范围内,以便将其计为双击。因此,如果结束时间小于500毫秒,您将取消
    setTimeout()
    使用
    clearTimeout()
    然后启动一个完全不同的函数(您希望为双击/单击启动的函数)

  • 停止默认操作?-可能像
    stopPropagation()
    cancelBubble()
    这样的事情会起作用。老实说,我不知道,但这就是我要开始研究的地方


  • 您还可以使用支持委派事件的,可以用于双击或双击。在纯javascript中:

    var mylatesttap;
    function doubletap() {
    
       var now = new Date().getTime();
       var timesince = now - mylatesttap;
       if((timesince < 600) && (timesince > 0)){
    
        // double tap   
    
       }else{
                // too much time to be a doubletap
             }
    
       mylatesttap = new Date().getTime();
    
    }
    
    var mylatesttap;
    函数doubletap(){
    var now=new Date().getTime();
    var timesince=now-mylatesttap;
    如果((时间间隔<600)和&(时间间隔>0)){
    //双击
    }否则{
    //时间太多了,不能做替身
    }
    mylatesttap=newdate().getTime();
    }
    
    基于最新的jquery文档,双击实现


    我用JavaScript创建了这个程序,它似乎运行良好。(我在iPad2上测试了它。) 它基本上就是上面所说的代码

    var clickTimer = null;
    
    function touchStart() {
        if (clickTimer == null) {
            clickTimer = setTimeout(function () {
                clickTimer = null;
                alert("single");
    
            }, 500)
        } else {
            clearTimeout(clickTimer);
            clickTimer = null;
            alert("double");
    
        }
    }
    
    检测双抽头 在触摸设备上尝试以下代码段

    event.preventDefault()
    将禁用
    div上的双击缩放#双击

    document.getElementById(“双击”).addEventListener(“touchstart”,tapHandler);
    var tapedTwice=false;
    函数tapHandler(事件){
    如果(!tapedTwice){
    tapedTwice=true;
    setTimeout(函数(){tapedTwice=false;},300);
    返回false;
    }
    event.preventDefault();
    //双击的动作如下
    警惕(“你敲了我两下!!!”);
    }
    
    div#双击{
    高度:50px;
    宽度:200px;
    边框:1px纯黑;
    背景颜色:浅蓝色;
    线高:50px;
    文本对齐:居中;
    保证金:50px自动;
    }

    双击我!!!
    我在Stackoverflow的某个地方找到了这个解决方案,但忘了确切的帖子是什么。双击浏览器,双击iPhone就可以了

    我使用
    'touchend click'
    来检测浏览器和iPhone上的双击。若要仅在iPhone上实现,请删除单击

    var timeout;
    var lastTap = 0;
    
    $('element').on('touchend click',function(e){
        var currentTime = new Date().getTime();
        var tapLength = currentTime - lastTap;        
    
        e.preventDefault();
        clearTimeout(timeout);
    
        if(tapLength < 500 && tapLength > 0){
    
            //Double Tap/Click
    
        }else{
    
            //Single Tap/Click
    
            timeout = setTimeout(function(){
                //Single Tap/Click code here
    
                clearTimeout(timeout); 
            }, 500);
        }
        lastTap = currentTime;
    });
    
    var超时;
    var-lastTap=0;
    $('element')。在('touchend click',函数(e)上{
    var currentTime=new Date().getTime();
    var tapLength=currentTime-lastTap;
    e、 预防默认值();
    clearTimeout(超时);
    如果(tapLength<500&&tapLength>0){
    //双击/单击
    }否则{
    //轻触/点击
    timeout=setTimeout(函数(){
    //点击/点击这里的代码
    clearTimeout(超时);
    }, 500);
    }
    lastTap=当前时间;
    });
    
    可能已经晚了(实际上是的,已经很晚了),但是对于那些潜伏在未来的人来说,纯易的JS解决方案:

    var tapped="";
    function dbtapper(elem=false,arg1, arg2, arg3, timer=1400){
        if(tapped==""){
            //Here is single tap
    
            tapped = setTimeout(function(){tapclean()}, timer)
            lastclick = elem;
            //variable lastclick is to prevent double clicking from happen
            //when clicking different object with double-click listener.
    
        } else if(lastclick==elem){
            //Here is double tap, if you want more (triple&beyond),
            //add doubletap/tripletap checker variable into tapclean().
            //and set it to true here!, example included.
            //you could also add more setTimeout to var-tapped so user will
            //have more time to do triple&up taps.
    
            tapclean();
            //doubletapped=true;
            mypersonalfunc(arg1, arg2, arg3);
        } else {
            tapped = setTimeout(function(){tapclean()}, timer);
            lastclick = elem;
    }
    }
    function tapclean(){
        clearTimeout(tapped);
        tapped="";
        //doubletapper=false;
    }
    
    使用此功能,您可以轻松地将单点、双点、三点、第四点……设置为无限远 注意:要使用元素检查,请调用如下函数:
    dbtapper(你的元素),即:单击我

    它不是强制的,您可以使用它而不必使用fine(
    dbtapper()
    ),但是如果需要将参数传递到函数中,请确保在第一个参数没有指向元素的情况下键入false

    Without element pointer: dbtapper(false,yourarguments)
    With element pointer: dbtapper(document.getElement*******,yourarguments)
    Alternatively, you can use an unique elem arg for each element:
                        dbtapper('omega',yourarg) or dbtapper(69,yourarg)
    Though this is not recommended if you have a huge amount of double tapping elements.
    

    该插件在selectorUse
    event.preventDefault()上的多点触控手势下工作得非常糟糕
    禁用默认缩放事件。我建议在要双击的特定元素中执行此操作,而不是在整个文档中。有趣的解决方案。我想知道是设置/清除超时还是这个更有效。@raypixar:这正是Jorge所说的,询问者所说的wanted@Thariama无硅双抽头检测ngle tap检测没有任何意义。它们都应该被检测。快速建议:在函数的开头添加
    if(mylatesttap==undefined){mylatesttap=new Date().getTime();}
    。您可能还想输入
    setTimeout(singleTap,doubleTapTimeout+10)
    其中doubleTapTimeout是以毫秒为单位的超时(在回答的示例中,这将是
    600
    ),而
    singleTap
    是一个检查是否实现了双击的函数(这将需要在窗口变量下使用另一个“全局”变量)