intersection-observer 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/intersection-observer-test.html +3 -11
- package/intersection-observer-test.js +20 -13
- package/intersection-observer.js +22 -23
- package/package.json +4 -4
- package/.npmignore +0 -3
package/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# `IntersectionObserver` polyfill
|
2
2
|
|
3
|
-
This library polyfills the native [`IntersectionObserver`](http://
|
3
|
+
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
4
|
|
5
5
|
- [Installation](#installation)
|
6
6
|
- [Configuring the polyfill](#configuring-the-polyfill)
|
@@ -9,7 +9,7 @@ This library polyfills the native [`IntersectionObserver`](http://wicg.github.io
|
|
9
9
|
|
10
10
|
## Installation
|
11
11
|
|
12
|
-
You can install the polyfill via npm or by downloading a [zip](https://github.com/
|
12
|
+
You can install the polyfill via npm or by downloading a [zip](https://github.com/w3c/IntersectionObserver/archive/gh-pages.zip) of this repository:
|
13
13
|
|
14
14
|
```sh
|
15
15
|
npm install intersection-observer
|
@@ -1,17 +1,9 @@
|
|
1
1
|
<!--
|
2
2
|
Copyright 2016 Google Inc. All Rights Reserved.
|
3
3
|
|
4
|
-
Licensed under the
|
5
|
-
you may not use this file except in compliance with the License.
|
6
|
-
You may obtain a copy of the License at
|
4
|
+
Licensed under the W3C SOFTWARE AND DOCUMENT NOTICE AND LICENSE.
|
7
5
|
|
8
|
-
|
9
|
-
|
10
|
-
Unless required by applicable law or agreed to in writing, software
|
11
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
-
See the License for the specific language governing permissions and
|
14
|
-
limitations under the License.
|
6
|
+
https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
|
15
7
|
-->
|
16
8
|
<!DOCTYPE html>
|
17
9
|
<html lang="en">
|
@@ -24,7 +16,7 @@ limitations under the License.
|
|
24
16
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.5.3/mocha.min.css">
|
25
17
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.5.3/mocha.min.js"></script>
|
26
18
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/expect.js/0.2.0/expect.min.js"></script>
|
27
|
-
<script src="https://cdnjs.cloudflare.com/ajax/libs/sinon.js/
|
19
|
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/sinon.js/3.2.1/sinon.min.js"></script>
|
28
20
|
|
29
21
|
<!-- Polyfills for IE7-8 -->
|
30
22
|
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=es5,getComputedStyle"></script>
|
@@ -1,17 +1,10 @@
|
|
1
1
|
/**
|
2
2
|
* Copyright 2016 Google Inc. All Rights Reserved.
|
3
3
|
*
|
4
|
-
* Licensed under the
|
5
|
-
* you may not use this file except in compliance with the License.
|
6
|
-
* You may obtain a copy of the License at
|
4
|
+
* Licensed under the W3C SOFTWARE AND DOCUMENT NOTICE AND LICENSE.
|
7
5
|
*
|
8
|
-
*
|
6
|
+
* https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
|
9
7
|
*
|
10
|
-
* Unless required by applicable law or agreed to in writing, software
|
11
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
-
* See the License for the specific language governing permissions and
|
14
|
-
* limitations under the License.
|
15
8
|
*/
|
16
9
|
|
17
10
|
|
@@ -142,14 +135,14 @@ describe('IntersectionObserver', function() {
|
|
142
135
|
it('throws when a threshold is not a number', function() {
|
143
136
|
expect(function() {
|
144
137
|
io = new IntersectionObserver(noop, {threshold: ['foo']});
|
145
|
-
}).to.throwException(
|
138
|
+
}).to.throwException();
|
146
139
|
});
|
147
140
|
|
148
141
|
|
149
142
|
it('throws when a threshold value is not between 0 and 1', function() {
|
150
143
|
expect(function() {
|
151
144
|
io = new IntersectionObserver(noop, {threshold: [0, -1]});
|
152
|
-
}).to.throwException(
|
145
|
+
}).to.throwException();
|
153
146
|
});
|
154
147
|
|
155
148
|
});
|
@@ -161,7 +154,7 @@ describe('IntersectionObserver', function() {
|
|
161
154
|
expect(function() {
|
162
155
|
io = new IntersectionObserver(noop);
|
163
156
|
io.observe(null);
|
164
|
-
}).to.throwException(
|
157
|
+
}).to.throwException();
|
165
158
|
});
|
166
159
|
|
167
160
|
|
@@ -178,6 +171,20 @@ describe('IntersectionObserver', function() {
|
|
178
171
|
io.observe(targetEl2);
|
179
172
|
});
|
180
173
|
|
174
|
+
it('triggers for existing targets when observing begins after monitoring has begun', function(done) {
|
175
|
+
var spy = sinon.spy();
|
176
|
+
io = new IntersectionObserver(spy, {root: rootEl});
|
177
|
+
|
178
|
+
io.observe(targetEl1);
|
179
|
+
setTimeout(function() {
|
180
|
+
io.observe(targetEl2);
|
181
|
+
setTimeout(function() {
|
182
|
+
expect(spy.callCount).to.be(2);
|
183
|
+
done();
|
184
|
+
}, ASYNC_TIMEOUT);
|
185
|
+
}, ASYNC_TIMEOUT);
|
186
|
+
});
|
187
|
+
|
181
188
|
|
182
189
|
it('triggers with the correct arguments', function(done) {
|
183
190
|
io = new IntersectionObserver(function(records, observer) {
|
@@ -632,7 +639,7 @@ describe('IntersectionObserver', function() {
|
|
632
639
|
|
633
640
|
// targetEl5 is initially not in the DOM. Note that this element must be
|
634
641
|
// created outside of the addFixtures() function to catch the IE11 error
|
635
|
-
// described here: https://github.com/
|
642
|
+
// described here: https://github.com/w3c/IntersectionObserver/pull/205
|
636
643
|
var targetEl5 = document.createElement('div');
|
637
644
|
targetEl5.setAttribute('id', 'target5');
|
638
645
|
|
package/intersection-observer.js
CHANGED
@@ -1,17 +1,10 @@
|
|
1
1
|
/**
|
2
2
|
* Copyright 2016 Google Inc. All Rights Reserved.
|
3
3
|
*
|
4
|
-
* Licensed under the
|
5
|
-
* you may not use this file except in compliance with the License.
|
6
|
-
* You may obtain a copy of the License at
|
4
|
+
* Licensed under the W3C SOFTWARE AND DOCUMENT NOTICE AND LICENSE.
|
7
5
|
*
|
8
|
-
*
|
6
|
+
* https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
|
9
7
|
*
|
10
|
-
* Unless required by applicable law or agreed to in writing, software
|
11
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
-
* See the License for the specific language governing permissions and
|
14
|
-
* limitations under the License.
|
15
8
|
*/
|
16
9
|
|
17
10
|
(function(window, document) {
|
@@ -22,12 +15,12 @@
|
|
22
15
|
// features are natively supported.
|
23
16
|
if ('IntersectionObserver' in window &&
|
24
17
|
'IntersectionObserverEntry' in window &&
|
25
|
-
'intersectionRatio' in IntersectionObserverEntry.prototype) {
|
18
|
+
'intersectionRatio' in window.IntersectionObserverEntry.prototype) {
|
26
19
|
|
27
20
|
// Minimal polyfill for Edge 15's lack of `isIntersecting`
|
28
|
-
// See: https://github.com/
|
29
|
-
if (!('isIntersecting' in IntersectionObserverEntry.prototype)) {
|
30
|
-
Object.defineProperty(IntersectionObserverEntry.prototype,
|
21
|
+
// See: https://github.com/w3c/IntersectionObserver/issues/211
|
22
|
+
if (!('isIntersecting' in window.IntersectionObserverEntry.prototype)) {
|
23
|
+
Object.defineProperty(window.IntersectionObserverEntry.prototype,
|
31
24
|
'isIntersecting', {
|
32
25
|
get: function () {
|
33
26
|
return this.intersectionRatio > 0;
|
@@ -49,7 +42,7 @@ var registry = [];
|
|
49
42
|
|
50
43
|
/**
|
51
44
|
* Creates the global IntersectionObserverEntry constructor.
|
52
|
-
* https://
|
45
|
+
* https://w3c.github.io/IntersectionObserver/#intersection-observer-entry
|
53
46
|
* @param {Object} entry A dictionary of instance properties.
|
54
47
|
* @constructor
|
55
48
|
*/
|
@@ -79,7 +72,7 @@ function IntersectionObserverEntry(entry) {
|
|
79
72
|
|
80
73
|
/**
|
81
74
|
* Creates the global IntersectionObserver constructor.
|
82
|
-
* https://
|
75
|
+
* https://w3c.github.io/IntersectionObserver/#intersection-observer-interface
|
83
76
|
* @param {Function} callback The function to be invoked after intersection
|
84
77
|
* changes have queued. The function is not invoked if the queue has
|
85
78
|
* been emptied by calling the `takeRecords` method.
|
@@ -131,6 +124,12 @@ IntersectionObserver.prototype.THROTTLE_TIMEOUT = 100;
|
|
131
124
|
*/
|
132
125
|
IntersectionObserver.prototype.POLL_INTERVAL = null;
|
133
126
|
|
127
|
+
/**
|
128
|
+
* Use a mutation observer on the root element
|
129
|
+
* to detect intersection changes.
|
130
|
+
*/
|
131
|
+
IntersectionObserver.prototype.USE_MUTATION_OBSERVER = true;
|
132
|
+
|
134
133
|
|
135
134
|
/**
|
136
135
|
* Starts observing a target element for intersection changes based on
|
@@ -138,10 +137,11 @@ IntersectionObserver.prototype.POLL_INTERVAL = null;
|
|
138
137
|
* @param {Element} target The DOM element to observe.
|
139
138
|
*/
|
140
139
|
IntersectionObserver.prototype.observe = function(target) {
|
141
|
-
|
142
|
-
if (this._observationTargets.some(function(item) {
|
140
|
+
var isTargetAlreadyObserved = this._observationTargets.some(function(item) {
|
143
141
|
return item.element == target;
|
144
|
-
})
|
142
|
+
});
|
143
|
+
|
144
|
+
if (isTargetAlreadyObserved) {
|
145
145
|
return;
|
146
146
|
}
|
147
147
|
|
@@ -152,6 +152,7 @@ IntersectionObserver.prototype.observe = function(target) {
|
|
152
152
|
this._registerInstance();
|
153
153
|
this._observationTargets.push({element: target, entry: null});
|
154
154
|
this._monitorIntersections();
|
155
|
+
this._checkForIntersections();
|
155
156
|
};
|
156
157
|
|
157
158
|
|
@@ -256,8 +257,6 @@ IntersectionObserver.prototype._monitorIntersections = function() {
|
|
256
257
|
if (!this._monitoringIntersections) {
|
257
258
|
this._monitoringIntersections = true;
|
258
259
|
|
259
|
-
this._checkForIntersections();
|
260
|
-
|
261
260
|
// If a poll interval is set, use polling instead of listening to
|
262
261
|
// resize and scroll events or DOM mutations.
|
263
262
|
if (this.POLL_INTERVAL) {
|
@@ -268,7 +267,7 @@ IntersectionObserver.prototype._monitorIntersections = function() {
|
|
268
267
|
addEvent(window, 'resize', this._checkForIntersections, true);
|
269
268
|
addEvent(document, 'scroll', this._checkForIntersections, true);
|
270
269
|
|
271
|
-
if ('MutationObserver' in window) {
|
270
|
+
if (this.USE_MUTATION_OBSERVER && 'MutationObserver' in window) {
|
272
271
|
this._domObserver = new MutationObserver(this._checkForIntersections);
|
273
272
|
this._domObserver.observe(document, {
|
274
273
|
attributes: true,
|
@@ -358,7 +357,7 @@ IntersectionObserver.prototype._checkForIntersections = function() {
|
|
358
357
|
* Accepts a target and root rect computes the intersection between then
|
359
358
|
* following the algorithm in the spec.
|
360
359
|
* TODO(philipwalton): at this time clip-path is not considered.
|
361
|
-
* https://
|
360
|
+
* https://w3c.github.io/IntersectionObserver/#calculate-intersection-rect-algo
|
362
361
|
* @param {Element} target The target DOM element
|
363
362
|
* @param {Object} rootRect The bounding rect of the root after being
|
364
363
|
* expanded by the rootMargin value.
|
@@ -647,7 +646,7 @@ function getBoundingClientRect(el) {
|
|
647
646
|
rect = el.getBoundingClientRect();
|
648
647
|
} catch (err) {
|
649
648
|
// Ignore Windows 7 IE11 "Unspecified error"
|
650
|
-
// https://github.com/
|
649
|
+
// https://github.com/w3c/IntersectionObserver/pull/205
|
651
650
|
}
|
652
651
|
|
653
652
|
if (!rect) return getEmptyRect();
|
package/package.json
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
{
|
2
2
|
"name": "intersection-observer",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.5.0",
|
4
4
|
"description": "A polyfill for IntersectionObserver",
|
5
5
|
"main": "intersection-observer",
|
6
6
|
"repository": {
|
7
7
|
"type": "git",
|
8
|
-
"url": "git@github.com:
|
8
|
+
"url": "git@github.com:w3c/IntersectionObserver.git"
|
9
9
|
},
|
10
10
|
"keywords": [
|
11
11
|
"Intersection",
|
@@ -16,8 +16,8 @@
|
|
16
16
|
"email": "philip@philipwalton.com",
|
17
17
|
"url": "http://philipwalton.com"
|
18
18
|
},
|
19
|
-
"license": "
|
19
|
+
"license": "W3C-20150513",
|
20
20
|
"bugs": {
|
21
|
-
"url": "https://github.com/
|
21
|
+
"url": "https://github.com/w3c/IntersectionObserver/issues"
|
22
22
|
}
|
23
23
|
}
|
package/.npmignore
DELETED