0

I am working on a page which containes jquery sliders. I want to enable those sliders for iPad site visitors. I got this working with the following code:

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

This works great except I am no longer able to pinch zoom in and out of the page on the iPad. Is there a way for me to preserve the pinch/zoom functionality and then have the touch events work properly on the jquery sliders?

Here's the test page in action: http://www.tonyjacobson.com/napawapa/slider-test4

tonejac
  • 1,083
  • 3
  • 18
  • 32

2 Answers2

0

The issue here lies with event.preventDefault(), which cancels the normal interpretation and handling of the gesture and facilitates your custom interaction. To preserve the default pinch-to-zoom feature along with your custom event, you need to wrap your custom handler in a condition that evaluates the event for certain criteria that would suggest that the user is trying to use your custom interaction as opposed to Apple's defaults. I would image that changedTouches would be a pretty fair indicator, as the pinch-to-zoom feature is a multi-touch event, meaning that the length of changedTouches is greater than 1. So, you might only want to trigger your custom event (thereby preventing the default behavior) if changedTouches.length == 1.

Aaron
  • 5,137
  • 1
  • 18
  • 20
  • Ooooh... interesting idea. I'll give it a shot and let you know how it goes. Thanks Aaron! – tonejac Mar 16 '12 at 19:56
  • Yeah, this is a good approach. I can target the slider dom elements to clear the event listeners on touchCancel or something. I'll post the solution after I get it sorted out. – tonejac Mar 16 '12 at 20:20
0

Here's what I got to make it all work:

    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();
tonejac
  • 1,083
  • 3
  • 18
  • 32