【问题标题】:iPad touch events but still allow for pinch zoomiPad 触摸事件,但仍允许捏缩放
【发布时间】:2012-03-14 05:01:26
【问题描述】:

我正在处理一个包含 jquery 滑块的页面。我想为 iPad 网站访问者启用这些滑块。我使用以下代码得到了这个:

        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;
            }

            //initMouseEvent(type, canBubble, cancelable, view, clickCount, 
            //           screenX, screenY, clientX, clientY, ctrlKey, 
            //           altKey, shiftKey, metaKey, button, relatedTarget);

            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);    
        }

        if ( (navigator.userAgent.match(/iPad/i)) ) {
            init();
        }

这很好用,只是我不再能够在 iPad 上捏放大和缩小页面。有没有办法让我保留捏/缩放功能,然后让触摸事件在 jquery 滑块上正常工作?

下面是实际测试页面:http://www.tonyjacobson.com/napawapa/slider-test4

【问题讨论】:

    标签: jquery ipad jquery-ui touch-event sliders


    【解决方案1】:

    这里的问题在于event.preventDefault(),它取消了对手势的正常解释和处理,方便了你的自定义交互。要保留默认的捏合缩放功能以及自定义事件,您需要将自定义处理程序包装在一个条件中,该条件会根据某些标准评估事件,这表明用户正在尝试使用您的自定义交互,而不是 Apple 的默认值。我认为changedTouches 将是一个相当公平的指标,因为双指缩放功能是一个多点触控事件,这意味着changedTouches 的长度大于 1。所以,您可能只想触发如果changedTouches.length == 1,您的自定义事件(从而防止默认行为)。

    【讨论】:

    • 噢……有趣的想法。我会试一试,让你知道进展如何。谢谢亚伦!
    • 是的,这是一个好方法。我可以定位滑块 dom 元素以清除 touchCancel 或其他东西上的事件侦听器。解决后我会发布解决方案。
    • 很高兴我能帮上忙!祝你好运。
    【解决方案2】:

    这就是我必须做到的:

        function touchHandler(event)
            {
                if (String(event.target).indexOf("#") != -1) { // THIS IS A UNIQUE ATTRIBUTE OF THE JQUERY SLIDER BUTTON. I CHECK TO SEE IF THE TARGET INCLUDES THAT '#' SIGN AND THEN IF IT DOES I GENERATE THE SIMULATED EVENT. OTHERWISE LET THE NOMRAL PAGE BEHAVIOR CONTINUE.
                    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;
                    }
    
                    //initMouseEvent(type, canBubble, cancelable, view, clickCount, 
                    //           screenX, screenY, clientX, clientY, ctrlKey, 
                    //           altKey, shiftKey, metaKey, button, relatedTarget);
    
                    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);   
            }
    
    
            init();
    

    【讨论】:

    • 注意:这也适用于 iPhone 和 Android 浏览器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-10
    • 1970-01-01
    • 1970-01-01
    • 2015-07-03
    • 2011-08-24
    • 2023-02-04
    • 2017-06-28
    相关资源
    最近更新 更多