intersection-observer 0.6.0 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +64 -5
- package/intersection-observer-test.html +7 -0
- package/intersection-observer-test.js +1427 -3
- package/intersection-observer.js +317 -68
- package/package.json +1 -1
package/README.md
CHANGED
@@ -96,20 +96,79 @@ io.USE_MUTATION_OBSERVER = false;
|
|
96
96
|
|
97
97
|
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.
|
98
98
|
|
99
|
-
## Limitations
|
100
99
|
|
101
|
-
|
100
|
+
## iframe support
|
101
|
+
|
102
|
+
### Same-origin iframes
|
103
|
+
|
104
|
+
Same-origin iframes are supported by the polyfill out of the box.
|
105
|
+
|
106
|
+
|
107
|
+
### Cross-origin iframes
|
108
|
+
|
109
|
+
Additional code and configuration are required to support cross-origin iframes,
|
110
|
+
both on the iframe and host sides.
|
111
|
+
|
112
|
+
The setup is as following:
|
113
|
+
|
114
|
+
1. The host and iframe will establish a messaging channel.
|
115
|
+
2. The host will setup its own IntersectionObserver instance for the
|
116
|
+
cross-origin iframe element. It can either use the this polyfill or any other
|
117
|
+
approach. For each IntersectionObserverEntry for the iframe it will forward
|
118
|
+
intersection data to the iframe via messaging.
|
119
|
+
3. The iframe will load the polyfill and configure it by calling the
|
120
|
+
`_setupCrossOriginUpdater()` method. It will call the provided callback
|
121
|
+
whenever it receives the intersection data from the the parent via messaging.
|
122
|
+
|
123
|
+
A hypothetical host code:
|
124
|
+
|
125
|
+
```javascript
|
126
|
+
function forwardIntersectionToIframe(iframe) {
|
127
|
+
createMessagingChannel(iframe, function(port) {
|
128
|
+
var io = new IntersectionObserver(function() {
|
129
|
+
port.postMessage({
|
130
|
+
boundingClientRect: serialize(boundingClientRect),
|
131
|
+
intersectionRect: serialize(intersectionRect)
|
132
|
+
});
|
133
|
+
}, {threshold: [0, 0.1, ..., 1]});
|
134
|
+
io.observe(iframe);
|
135
|
+
});
|
136
|
+
}
|
137
|
+
```
|
138
|
+
|
139
|
+
Notice that the host should provide a `threshold` argument for the desired
|
140
|
+
level of precision. Otherwise, the iframe side may not update as frequently as
|
141
|
+
desired.
|
142
|
+
|
143
|
+
A hypothetical iframe code:
|
144
|
+
|
145
|
+
```javascript
|
146
|
+
createMessagingChannel(parent, function(port) {
|
147
|
+
if (IntersectionObserver._setupCrossOriginUpdater) {
|
148
|
+
var crossOriginUpdater = IntersectionObserver._setupCrossOriginUpdater();
|
149
|
+
port.onmessage = function(event) {
|
150
|
+
crossOriginUpdater(
|
151
|
+
deserialize(event.data.boundingClientRect),
|
152
|
+
deserialize(event.data.intersectionRect)
|
153
|
+
);
|
154
|
+
};
|
155
|
+
}
|
156
|
+
});
|
157
|
+
```
|
158
|
+
|
159
|
+
|
160
|
+
## Limitations
|
102
161
|
|
103
|
-
This polyfill
|
162
|
+
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.
|
104
163
|
|
105
164
|
## Browser support
|
106
165
|
|
107
166
|
The polyfill has been tested and known to work in the latest version of all browsers.
|
108
167
|
|
109
|
-
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/
|
168
|
+
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:
|
110
169
|
|
111
170
|
```html
|
112
|
-
<script src="https://polyfill.io/
|
171
|
+
<script src="https://polyfill.io/v3/polyfill.min.js?features=IntersectionObserver"></script>
|
113
172
|
```
|
114
173
|
|
115
174
|
With these polyfills, `IntersectionObserver` has been tested an known to work in the following browsers:
|
@@ -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
|
|