Mobile Webkit reflow issue

后端 未结 6 643
盖世英雄少女心
盖世英雄少女心 2020-12-05 06:04

I\'ve been experiencing an issue in mobile versions of webkit (specifically Webkit 534.46 on iOS 5.1.1 as mobile Safari, and now Chrome for iOS) which doesn\'t happen on any

相关标签:
6条回答
  • 2020-12-05 06:18

    Looks like this is a known bug:

    the core problem is: if the page moves programatically (i.e. the user didn’t cause the scroll) the elements inside the fix element are unavailable.

    Use absolute positioning, change the markup, or use one of the hybrid workarounds.

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

    Ironically, my original reflow fix (linked to in the question) is now working in my real app, too. Putting a variant of it here in case is useful to anyone else. It can be called on any container element, or if nothing is passed in it reflows the whole document.

    var forceReflow = function(elem){
        elem = elem || document.documentElement;
    
        // force a reflow by increasing size 1px
        var width = elem.style.width,
            px = elem.offsetWidth+1;
    
        elem.style.width = px+'px';
    
        setTimeout(function(){
            // undo resize, unfortunately forces another reflow
            elem.style.width = width;
            elem = null;
        }, 0);
    };
    

    The nice thing about this is that it doesn't require creating / adding / removing elements, just tweaking the container.

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

    I believe this is better, and achieves the same effect, allowing links to be clickable in fixed footers. Somehow, doing urlbar hiding causes links in the fixed footer to be unclickable until you scroll a little bit. I have seen this too when focusing inputs, and I attach an event handler to all focus events to fire this off as well. I do this with dojo to attach the events.

            if(navigator.userAgent.match(/iPhone/i)){
            /* The famous iOS can't-click-links until touch fix, I attach onfocus */
                query('input,textarea,select', this.domNode).on('focus', function(el){
                    document.documentElement.style.paddingRight = '1px';
                    setTimeout(function () {
                    document.documentElement.style.paddingRight = '';
                    }, 0);
                });
            }
    
    0 讨论(0)
  • 2020-12-05 06:31

    My install of iWebInspector is pretty busted right now, but after messing around with jsfiddle and the iOS sim it seems like your hunch is correct - despite being position:fixed, the browser thinks the page has scrolled, and screws up the click targets.

    It looks a lot like this is the same issue as iOS Safari: Anchors within a fixed positioned element only work once, which also hasn't been solved with pure CSS. Also related: Fixed position navbar only clickable once in Mobile Safari on iOS5.

    Tangentially, and I'm sure it's been noticed already, it's not possible to scroll the left side, so on an iPhone the index only shows A-M.

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

    The answer that actually solved my particular issue was a variation of a solution found in one of @Paul Sweatte's links:

    Essentially, a plain div which is taller than the body is added. When it is removed, it causes the body to effectively scroll or reflow. Setting the delay to 0ms between adding/removing is enough to allow the DOM to recalculate without causing any flickering. This was the minimal script I could find which fully solved the problem for all position:fixed elements on my particular instance of this issue.

    var hack = document.createElement("div");
    hack.style.height = "101%";
    document.body.appendChild(hack);
    setTimeout(function(){
        document.body.removeChild(hack);
        hack = null;
    }, 0);
    
    0 讨论(0)
  • 2020-12-05 06:38

    Here's a variation of McKamey's workaround. It avoids reflowing twice, and may help with flickering (depending on your app):

    setTimeout(function(){
        document.body.style.borderBottom = 
            document.body.style.borderBottom === 'none' ? '1px solid white' : 'none';
    }, 0);
    
    0 讨论(0)
提交回复
热议问题