0

我正在处理包含 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/napapapa/slider-test4

4

2 回答 2

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();
于 2012-03-16T20:40:36.597 回答
0

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

于 2012-03-14T05:27:26.787 回答