intersection observer polyfill not working Safari

和自甴很熟 提交于 2019-12-12 14:12:13

问题


I need to detect if my iframe which is loaded from another domain is in viewport.

This works fine native in Chromem,Firefox and IE called Intersection Observer. However this is not working in Safari.

There has been created a polyfill for this: Github link and should be working on Safari as well.

I tested it using this code:

Site A:

<html>
  <head>
  </head>
    <body>
     A lot of text here so the iframe is not inview.
     A lot of text here so the iframe is not inview.
     <br>
     <br>
     .
     .
     .
     .
     .
     .
     .
     <br>
     A lot of text here so the iframe is not inview.
    <iframe src="//siteB.com/test.html" frameborder="0" border="0" scroll="no" scrolling="no" width="300" height="250" style="border: 0px; overflow: hidden !important;"></iframe>
    </body>
    </html>

Site B:

<html>
    <head>
        <script src="intersection-observer.js"></script>
        <script>
            document.addEventListener("DOMContentLoaded", function(event) { 
              //do work
                var io = new IntersectionObserver(
                  entries => {
                    console.log(entries);
                    if (entries["0"].isIntersecting) {
                        alert("inview");
                    } 
                  },
                  {
                    /* Using default options. Details below */
                  }
                );
                // Start observing an element
                var img1 = document.getElementById("img1");
                console.log(img1);
                io.observe(img1);
            });
        </script>
    </head> 
    <body>
        <img id="img1" src="motor.gif" style="position:absolute; top:0px; left:0px"/>
    </body>
</html>

However I've tested this on an Iphone 6 however it immediatly does the alert and after clicking the alert away it doesn't show up anymore when scrolling up and down.

The intersection-observer-test.html page they included return passes for all 25 tests.

Anyone have a solution to this?


回答1:


I saw a issue on github where it outlined safari getting the incorrect height because of quirks mode : https://github.com/w3c/IntersectionObserver/issues/282 . Maybe add

<!DOCTYPE html>
<html>

can fix it.



来源:https://stackoverflow.com/questions/51062099/intersection-observer-polyfill-not-working-safari

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!