最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - intersection observer polyfill not working Safari - Stack Overflow

programmeradmin0浏览0评论

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/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?

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./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?

Share Improve this question edited Jun 27, 2018 at 12:31 Nikola Lukic 4,2666 gold badges48 silver badges80 bronze badges asked Jun 27, 2018 at 11:51 user3605780user3605780 7,09213 gold badges46 silver badges71 bronze badges 1
  • 1 Try to implement code snippet if is possible. – Nikola Lukic Commented Jun 27, 2018 at 12:06
Add a ment  | 

1 Answer 1

Reset to default 4

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

<!DOCTYPE html>
<html>

can fix it.

发布评论

评论列表(0)

  1. 暂无评论