iOS Safari – How to disable overscroll but allow scrollable divs to scroll normally?

后端 未结 14 1610
南方客
南方客 2020-11-28 17:50

I\'m working on an iPad-based web app, and need to prevent overscrolling so that it seems less like a web page. I\'m currently using this to freeze the viewport and disable

14条回答
  •  我在风中等你
    2020-11-28 18:13

    This solution doesn't require you to put a scrollable class on all your scrollable divs so is more general. Scrolling is allowed on all elements which are, or are children of, INPUT elements contenteditables and overflow scroll or autos.

    I use a custom selector and I also cache the result of the check in the element to improve performance. No need to check the same element every time. This may have a few issues as only just written but thought I'd share.

    $.expr[':'].scrollable = function(obj) {
        var $el = $(obj);
        var tagName = $el.prop("tagName");
        return (tagName !== 'BODY' && tagName !== 'HTML') && (tagName === 'INPUT' || $el.is("[contentEditable='true']") || $el.css("overflow").match(/auto|scroll/));
    };
    function preventBodyScroll() {
        function isScrollAllowed($target) {
            if ($target.data("isScrollAllowed") !== undefined) {
                return $target.data("isScrollAllowed");
            }
            var scrollAllowed = $target.closest(":scrollable").length > 0;
            $target.data("isScrollAllowed",scrollAllowed);
            return scrollAllowed;
        }
        $('body').bind('touchmove', function (ev) {
            if (!isScrollAllowed($(ev.target))) {
                ev.preventDefault();
            }
        });
    }
    

提交回复
热议问题