iPad触摸事件,但仍然允许捏缩放

时间:2012-03-14 05:01:26

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

我正在处理包含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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

这就是我所做的一切:

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