intersection-observer 0.5.1 → 0.12.2

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -1,9 +1,12 @@
1
+ **⚠️ This polyfill was originally hosted in the [IntersectionObserver spec](https://github.com/w3c/IntersectionObserver) repo. Refer to that repo for commit history.**
2
+
1
3
  # `IntersectionObserver` polyfill
2
4
 
3
5
  This library polyfills the native [`IntersectionObserver`](http://w3c.github.io/IntersectionObserver/) API in unsupporting browsers. See the [API documentation](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) for usage information.
4
6
 
5
7
  - [Installation](#installation)
6
8
  - [Configuring the polyfill](#configuring-the-polyfill)
9
+ - [Limitations](#limitations)
7
10
  - [Browser support](#browser-support)
8
11
  - [Running the tests](#running-the-tests)
9
12
 
@@ -60,16 +63,16 @@ There are, however, additional use cases that the default configuration will not
60
63
 
61
64
  If you need to handle any of these use-cases, you can configure the polyfill to poll the document by setting the `POLL_INTERVAL` property. This can be set either globally or on a per-instance basis.
62
65
 
63
- **Enabling polling for all instance:**
66
+ **Enabling polling for all instances:**
64
67
 
65
- To enable polling for all instance, set a value for `POLL_INTERVAL` on the `IntersectionObserver` prototype:
68
+ To enable polling for all instances, set a value for `POLL_INTERVAL` on the `IntersectionObserver` prototype:
66
69
 
67
70
 
68
71
  ```js
69
72
  IntersectionObserver.prototype.POLL_INTERVAL = 100; // Time in milliseconds.
70
73
  ```
71
74
 
72
- **Enabling polling for individual instance:**
75
+ **Enabling polling for individual instances:**
73
76
 
74
77
  To enable polling on only specific instances, set a `POLL_INTERVAL` value on the instance itself:
75
78
 
@@ -93,19 +96,84 @@ var io = new IntersectionObserver(callback);
93
96
  io.USE_MUTATION_OBSERVER = false;
94
97
  ```
95
98
 
96
- This is recommended in cases where the DOM will update frequently but you know those updates will have no affect on the position or your target elements.
99
+ This is recommended in cases where the DOM will update frequently but you know those updates will have no effect on the position or your target elements.
100
+
101
+
102
+ ## iframe support
103
+
104
+ ### Same-origin iframes
105
+
106
+ Same-origin iframes are supported by the polyfill out of the box.
107
+
108
+
109
+ ### Cross-origin iframes
110
+
111
+ Additional code and configuration are required to support cross-origin iframes,
112
+ both on the iframe and host sides.
113
+
114
+ The setup is as following:
115
+
116
+ 1. The host and iframe will establish a messaging channel.
117
+ 2. The host will setup its own IntersectionObserver instance for the
118
+ cross-origin iframe element. It can either use the this polyfill or any other
119
+ approach. For each IntersectionObserverEntry for the iframe it will forward
120
+ intersection data to the iframe via messaging.
121
+ 3. The iframe will load the polyfill and configure it by calling the
122
+ `_setupCrossOriginUpdater()` method. It will call the provided callback
123
+ whenever it receives the intersection data from the the parent via messaging.
124
+
125
+ A hypothetical host code:
126
+
127
+ ```javascript
128
+ function forwardIntersectionToIframe(iframe) {
129
+ createMessagingChannel(iframe, function(port) {
130
+ var io = new IntersectionObserver(function() {
131
+ port.postMessage({
132
+ boundingClientRect: serialize(boundingClientRect),
133
+ intersectionRect: serialize(intersectionRect)
134
+ });
135
+ }, {threshold: [0, 0.1, ..., 1]});
136
+ io.observe(iframe);
137
+ });
138
+ }
139
+ ```
140
+
141
+ Notice that the host should provide a `threshold` argument for the desired
142
+ level of precision. Otherwise, the iframe side may not update as frequently as
143
+ desired.
144
+
145
+ A hypothetical iframe code:
146
+
147
+ ```javascript
148
+ createMessagingChannel(parent, function(port) {
149
+ if (IntersectionObserver._setupCrossOriginUpdater) {
150
+ var crossOriginUpdater = IntersectionObserver._setupCrossOriginUpdater();
151
+ port.onmessage = function(event) {
152
+ crossOriginUpdater(
153
+ deserialize(event.data.boundingClientRect),
154
+ deserialize(event.data.intersectionRect)
155
+ );
156
+ };
157
+ }
158
+ });
159
+ ```
160
+
161
+
162
+ ## Limitations
163
+
164
+ This polyfill does not support the [proposed v2 additions](https://github.com/szager-chromium/IntersectionObserver/blob/v2/explainer.md), as these features are not currently possible to do with JavaScript and existing web APIs.
97
165
 
98
166
  ## Browser support
99
167
 
100
168
  The polyfill has been tested and known to work in the latest version of all browsers.
101
169
 
102
- Legacy support is also possible in very old browsers by including a shim for ES5 as well as the `window.getComputedStyle` method. The easiest way to load the IntersectionObserver polyfill and have it work in the widest range of browsers is via [polyfill.io](https://cdn.polyfill.io/v2/docs/), which will automatically include dependencies where necessary:
170
+ Legacy support is also possible in very old browsers by including a shim for ES5 as well as the `window.getComputedStyle` method. The easiest way to load the IntersectionObserver polyfill and have it work in the widest range of browsers is via [polyfill.io](https://cdn.polyfill.io/v3/), which will automatically include dependencies where necessary:
103
171
 
104
172
  ```html
105
- <script src="https://polyfill.io/v2/polyfill.min.js?features=IntersectionObserver"></script>
173
+ <script src="https://polyfill.io/v3/polyfill.min.js?features=IntersectionObserver"></script>
106
174
  ```
107
175
 
108
- With these polyfills, `IntersectionObserver` has been tested an known to work in the following browsers:
176
+ With these polyfills, `IntersectionObserver` has been tested and known to work in the following browsers:
109
177
 
110
178
  <table>
111
179
  <tr>
@@ -144,4 +212,4 @@ With these polyfills, `IntersectionObserver` has been tested an known to work in
144
212
 
145
213
  To run the test suite for the `IntersectionObserver` polyfill, open the [`intersection-observer-test.html`](./intersection-observer-test.html) page in the browser of your choice.
146
214
 
147
- If you run the tests in a browser that support `IntersectionObserver` natively, the tests will be run against the native implementation. If it doesn't the tests will be run against the polyfill.
215
+ If you run the tests in a browser that supports `IntersectionObserver` natively, the tests will be run against the native implementation. If it doesn't, the tests will be run against the polyfill.
@@ -27,6 +27,13 @@ Licensed under the W3C SOFTWARE AND DOCUMENT NOTICE AND LICENSE.
27
27
  <!-- Setup -->
28
28
  <script>mocha.setup({ui:'bdd'});</script>
29
29
 
30
+ <!-- Uncomment this script to force the polyfill installation
31
+ <script>
32
+ delete window.IntersectionObserver;
33
+ delete window.IntersectionObserverEntry;
34
+ </script>
35
+ -->
36
+
30
37
  <!-- Script under test -->
31
38
  <script src="intersection-observer.js"></script>
32
39