Mobile Safari: Disable scrolling pages “out of screen”

前端 未结 2 1854
执念已碎
执念已碎 2021-02-11 05:55

I want to block scrolling page \"out of the iPhone screen\" (when gray Safari\'s background behind the page border is visible). To do this, I\'m cancelling touchmove event:

2条回答
  •  说谎
    说谎 (楼主)
    2021-02-11 06:44

    From what I understand of your question, you've attempted to combine the code you've presented above with the code snippet provided by Ross Boucher on Posterous. Attempting to combine these two snippets back-to-back won't work, because in disabling touchmove, you've also disabled the shim that allows mousemove to work via his sample.

    This question and its answers sketch out a workable solution to your problem. You should try these two snippets to see if they resolve your issue:

    This snippet, which disables the old scrolling behavior:

    elementYouWantToScroll.ontouchmove = function(e) {
        e.stopPropagation();
    }; 
    

    Or this one, from the same:

    document.ontouchmove = function(e) {
        var target = e.currentTarget;
        while(target) {
            if(checkIfElementShouldScroll(target))
                return;
            target = target.parentNode;
        }
    
        e.preventDefault();
    };
    

    Then, drop in the code on Posterous:

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

    And that should do it for you. If it doesn't, something else isn't working with Mobile Safari.

提交回复
热议问题