Safari in ios8 is scrolling screen when fixed elements get focus

前端 未结 12 1105
失恋的感觉
失恋的感觉 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.

    0 讨论(0)
  • 2020-12-12 11:59

    This is now fixed in iOS 10.3!

    Hacks should no longer be needed.

    0 讨论(0)
  • 2020-12-12 12:00

    I had the issue, below lines of code resolved it for me -

    html{
    
     overflow: scroll; 
    -webkit-overflow-scrolling: touch;
    
    }
    
    0 讨论(0)
  • 2020-12-12 12:04

    Based on this good analysis of this issue, I've used this in html and body elements in css:

    html,body{
        -webkit-overflow-scrolling : touch !important;
        overflow: auto !important;
        height: 100% !important;
    }
    

    I think it's working great for me.

    0 讨论(0)
  • 2020-12-12 12:05

    Haven't dealt with this particular bug, but maybe put an overflow: hidden; on the body when the text area is visible (or just active, depending on your design). This may have the effect of not giving the browser anywhere "down" to scroll to.

    0 讨论(0)
  • 2020-12-12 12:08

    Cleanly? no.

    I recently had this problem myself with a fixed search field in a sticky header, the best you can do at the moment is keep the scroll position in a variable at all times and upon selection make the fixed element's position absolute instead of fixed with a top position based on the document's scroll position.

    This is however very ugly and still results in some strange back and forth scrolling before landing on the right place, but it is the closest I could get.

    Any other solution would involve overriding the default scroll mechanics of the browser.

    0 讨论(0)
提交回复
热议问题