web-dev-qa-db-fra.com

`-webkit-overflow-scrolling: touch` cassé pour les éléments initialement hors écran dans iOS7

Vu que GM a été publiée, nous pouvons en parler maintenant!

On dirait que dans iOS7 "-webkit-overflow-scrolling: touch" est cassé. les événements tactiles pour les éléments qui sont initialement hors écran ne se déclenchent pas (ou dans certains cas sont tout simplement peu fiables).

Voici un exemple:

<!DOCTYPE html><html>
    <head><title>TEST</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="Apple-mobile-web-app-capable" content="yes" />
    <style>

        #scrollbox {
                position: fixed;
                top: 0px;
                width: 100%;
                height: 100%;
                overflow: scroll;
                -webkit-overflow-scrolling: touch;
        }

        .touchDiv {
            position: relative;
            width:100%;
            height:80px;
            background-color: #FFBBBB;
        }

        ul, li {
            text-indent: 0px;
            list-style: none;
            padding: 0px;
            margin: 0px;
        }

        li {
            padding-top: 10px;
            padding-bottom: 10px;
            border-bottom: 1px solid #333;
        }

    </style>
    <script type="text/javascript">

        function onBodyLoad() {
            var touchDiv = document.getElementById("bottomDiv");
            touchDiv.ontouchstart = function(e) {
                alert("touched");
            };

            touchDiv = document.getElementById("topDiv");
            touchDiv.ontouchstart = function(e) {
                alert("touched");
            };
        }
    </script>
    </head>


    <body onload="onBodyLoad()">


        <div id='scrollbox'>
                <div id="topDiv" class="touchDiv">Fires an event when touched</div>
                <ul>
                    <li><a href='#' onclick="alert('1')">Link 1</a></li>
                    <li><a href='#' onclick="alert('3')">Link 3</a></li>
                    <li><a href='#' onclick="alert('4')">Link 4</a></li>
                    <li><a href='#' onclick="alert('5')">Link 5</a></li>
                    <li><a href='#' onclick="alert('6')">Link 6</a></li>
                    <li><a href='#' onclick="alert('7')">Link 7</a></li>
                    <li><a href='#' onclick="alert('8')">Link 8</a></li>
                    <li><a href='#' onclick="alert('9')">Link 9</a></li>
                    <li><a href='#' onclick="alert('10')">Link 10</a></li>
                    <li><a href='#' onclick="alert('11')">Link 11</a></li>
                    <li><a href='#' onclick="alert('12')">Link 12</a></li>
                    <li><a href='#' onclick="alert('13')">Link 13</a></li>
                    <li><a href='#' onclick="alert('14')">Link 14</a></li>
                    <li><a href='#' onclick="alert('15')">Link 15</a></li>
                    <li><a href='#' onclick="alert('16')">Link 16</a></li>
                    <li><a href='#' onclick="alert('17')">Link 17</a></li>
                    <li><a href='#' onclick="alert('18')">Link 18</a></li>
                    <li><a href='#' onclick="alert('19')">Link 19</a></li>
                    <li><a href='#' onclick="alert('20')">Link 20</a></li>
                    <li><a href='#' onclick="alert('21')">Link 21</a></li>
                    <li><a href='#' ontouchstart="alert('22')">Link 22</a></li>
                </ul>
                <div id="bottomDiv" class="touchDiv">Fires an event when touched 2</div>

        </div>

</body>

Je suppose que cela brisera la majorité des applications Web optimisées pour les mobiles.

Des problèmes avec le code ci-dessus? Et/ou existe-t-il des solutions?

(J'ai déjà signalé un bogue avec Apple - il y a quelque temps, et aucune réponse)

22
Nick H247

Il semble qu'il y ait une solution à ce problème sur forums de développement Apple

Merci beaucoup à M. Baicoianu.

Fondamentalement, vous devez écouter les événements tactiles sur la div de défilement parent. Donc, dans mon cas, ajoutez:

document.getElementById("scrollbox").addEventListener('touchstart', function(event){});

à la fonction onBodyLoad.

Je suppose que c'est un problème de propagation d'événements, résolu en écoutant à un niveau supérieur.

30
Nick H247

La réponse ci-dessus me fait plaisir. Voici la version jQuery du correctif ci-dessus:

$('#scrollbox').on('touchstart', function(event){});
7
DigitalJohn