Safari in ios8 is scrolling screen when fixed elements get focus

前端 未结 12 1084
失恋的感觉
失恋的感觉 2020-12-12 11:24

In IOS8 Safari there is a new bug with position fixed.

If you focus a textarea that is in a fixed panel, safari will scroll you to the bottom of the page.

T

12条回答
  •  失恋的感觉
    2020-12-12 11:58

    None of these solutions worked for me because my DOM is complicated and I have dynamic infinite scroll pages, so I had to create my own.

    Background: I am using a fixed header and an element further down that sticks below it once the user scrolls that far down. This element has a search input field. In addition, I have dynamic pages added during forward and backwards scroll.

    Problem: In iOS, anytime the user clicked on the input in the fixed element, the browser would scroll all the way to the top of the page. This not only caused undesired behavior, it also triggered my dynamic page add at the top of the page.

    Expected Solution: No scroll in iOS (none at all) when the user clicks on the input in the sticky element.

    Solution:

         /*Returns a function, that, as long as it continues to be invoked, will not
        be triggered. The function will be called after it stops being called for
        N milliseconds. If `immediate` is passed, trigger the function on the
        leading edge, instead of the trailing.*/
        function debounce(func, wait, immediate) {
            var timeout;
            return function () {
                var context = this, args = arguments;
                var later = function () {
                    timeout = null;
                    if (!immediate) func.apply(context, args);
                };
                var callNow = immediate && !timeout;
                clearTimeout(timeout);
                timeout = setTimeout(later, wait);
                if (callNow) func.apply(context, args);
            };
        };
    
         function is_iOS() {
            var iDevices = [
              'iPad Simulator',
              'iPhone Simulator',
              'iPod Simulator',
              'iPad',
              'iPhone',
              'iPod'
            ];
            while (iDevices.length) {
                if (navigator.platform === iDevices.pop()) { return true; }
            }
            return false;
        }
    
        $(document).on("scrollstop", debounce(function () {
            //console.log("Stopped scrolling!");
            if (is_iOS()) {
                var yScrollPos = $(document).scrollTop();
                if (yScrollPos > 200) { //200 here to offset my fixed header (50px) and top banner (150px)
                    $('#searchBarDiv').css('position', 'absolute');
                    $('#searchBarDiv').css('top', yScrollPos + 50 + 'px'); //50 for fixed header
                }
                else {
                    $('#searchBarDiv').css('position', 'inherit');
                }
            }
        },250,true));
    
        $(document).on("scrollstart", debounce(function () {
            //console.log("Started scrolling!");
            if (is_iOS()) {
                var yScrollPos = $(document).scrollTop();
                if (yScrollPos > 200) { //200 here to offset my fixed header (50px) and top banner (150px)
                    $('#searchBarDiv').css('position', 'fixed');
                    $('#searchBarDiv').css('width', '100%');
                    $('#searchBarDiv').css('top', '50px'); //50 for fixed header
                }
            }
        },250,true));
    

    Requirements: JQuery mobile is required for the startsroll and stopscroll functions to work.

    Debounce is included to smooth out any lag created by the sticky element.

    Tested in iOS10.

提交回复
热议问题