Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
6ec4ba6
Merge pull request #127 from dret/patch-1
mpb May 3, 2016
033d9c0
Merge pull request #122 from carlosagsmendes/gh-pages
mpb May 3, 2016
642cae1
Merge branch 'gh-pages' of https://github.com/WICG/IntersectionObserv…
philipwalton Jun 10, 2016
1dc9b46
Merge branch 'gh-pages' of https://github.com/WICG/IntersectionObserv…
philipwalton Jul 6, 2016
08f6453
Add IntersectionObserver polyfill
surma Mar 16, 2016
c8194ef
Check root element in constructor
surma Mar 19, 2016
11fe232
Typo
surma Mar 19, 2016
32db646
Debounce scroll event
surma Mar 19, 2016
ea6cf8f
Fix tests
surma Mar 19, 2016
603aeca
Use MutationObserver
surma Mar 28, 2016
f9a5bc4
Add license header
surma Mar 28, 2016
91b8449
Calculate a rects width and height if not given
surma Apr 1, 2016
4897665
Code review
surma Apr 4, 2016
5b920aa
Minor typo
surma Apr 4, 2016
422e188
Move tests to mocha
surma Apr 9, 2016
c01ede1
Fix margin tests
surma Apr 9, 2016
dd933cc
Use consistent indents
surma Apr 9, 2016
359c944
Improve intersection detection
surma Apr 14, 2016
7cc778c
Comment in ratio tests
surma Apr 15, 2016
cf20074
Implement @jeremenichelli review
surma Apr 21, 2016
197ea9f
Remove `resize` event listener on disconnect()
surma Apr 21, 2016
f5dd7f8
These styles are redundant - now applied directly to the test element…
triblondon May 3, 2016
0af606c
No longer needed
triblondon May 3, 2016
fb28b71
CDNjs annoyingly doesn't have the latest version of expect, so async …
triblondon May 3, 2016
1ab103d
Make it look nicer
triblondon May 3, 2016
fd83bb4
Conventional to load the script under test after the test framework
triblondon May 3, 2016
4cffaed
Move test definitions into <head>. Start testing at onload to avoid …
triblondon May 3, 2016
d0ca103
Conventional to put test cases in a test suite
triblondon May 3, 2016
73e5ba5
Change async test implementation to support more legacy browsers, mor…
triblondon May 3, 2016
728b05a
Add a test
triblondon May 3, 2016
b70f27f
Move the test JavaScript into its own file
philipwalton Jun 6, 2016
adeaee3
Rename the polyfill to intersection-observer
philipwalton Jun 6, 2016
3d8b1b4
Add initial polyfill code and tests
philipwalton May 26, 2016
c047367
Update to support IE8
philipwalton May 27, 2016
c480aa8
Fix test failures against native IO
philipwalton Jun 3, 2016
7825e11
Remove expect.js and reference a cdn version
philipwalton Jun 6, 2016
093faf5
For IE8 into standards mode
philipwalton Jun 8, 2016
9be1c17
Handle spec changes post Chrome 51
philipwalton Jun 8, 2016
5e4b667
Move styles and fixture creation inside the test
philipwalton Jun 9, 2016
c7078bf
Remove visibilitychange checking
philipwalton Jun 10, 2016
ab6bd13
Listen for events/mutations instead of polling
philipwalton Jun 10, 2016
deafb28
Remove scheduling/descheduling callback logic
philipwalton Jun 10, 2016
5f84a64
Fix a flaky viewport test
philipwalton Jun 10, 2016
fdcf54e
Add eslint and fix lint errors
philipwalton Jun 10, 2016
dc21b2a
Use polyfill.io to load IE8 polyfills
philipwalton Jun 10, 2016
a46a0c5
Refactor to use the default mocha timeout
philipwalton Jun 15, 2016
4e516da
Fix failing tests
philipwalton Jun 16, 2016
6c526b9
Add support for IE7 zero-sized html element
philipwalton Jun 16, 2016
3cdad13
Address review comments
philipwalton Jun 18, 2016
5c8ef4d
Fix lint errors
philipwalton Jul 1, 2016
b7409f5
Add an IntersectionObserver instance registry
philipwalton Jul 1, 2016
e1cd229
Add polyfill README and package.json
philipwalton Jul 1, 2016
9b1a3bf
Address PR review feedback
philipwalton Jul 9, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Intersection Observers

[Explainer Doc](./explainer.md)

[Draft Spec](https://wicg.github.io/IntersectionObserver/)

[Polyfill](./polyfill/)
45 changes: 45 additions & 0 deletions polyfill/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"env": {
"browser": true,
"node": true,
"mocha": true
},
"globals": {
"expect": false,
"sinon": false
},
"extends": [
"eslint:recommended"
],
"rules": {
// possible errors
"no-cond-assign": 0,
"no-empty": 0,
"valid-jsdoc": [2, {
"prefer": {"return": "return"},
"requireReturn": false
}],
// best practices
"no-extra-bind": 2,
// style
"comma-spacing": [2, {"before": false, "after": true}],
"eol-last": [0],
"key-spacing": [2, {"beforeColon": false, "afterColon": true}],
"linebreak-style": [2, "unix"],
"max-len": [2, 80, {"ignoreUrls": true}],
"no-multiple-empty-lines": [2, {"max": 2}],
"no-unused-vars": ["error", {"vars": "all"}],
"no-trailing-spaces": 2,
"object-curly-spacing": [2, "never"],
"quotes": [2, "single"],
"require-jsdoc": [2, {
"require": {
"FunctionDeclaration": true,
"MethodDefinition": true,
"ClassDeclaration": true
}
}],
"semi": [2, "always"],
"semi-spacing": [2, {"before": false, "after": true}],
}
}
139 changes: 139 additions & 0 deletions polyfill/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# `IntersectionObserver` polyfill

This library polyfills the native [`IntersectionObserver`](http://wicg.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.

- [Installation](#installation)
- [Configuring the polyfill](#configuring-the-polyfill)
- [Browser support](#browser-support)
- [Running the tests](#running-the-tests)

## Installation

You can install the polyfill via npm or by downloading a [zip](https://github.com/WICG/IntersectionObserver/archive/gh-pages.zip) of this repository:

```sh
npm install intersection-observer
```

### Adding the polyfill to your site

The examples below show various ways to add the `IntersectionObserver` polyfill to your site. Be sure to include the polyfill prior to referencing it anywhere in your JavaScript code.

**Using `<script>` tags in the HTML:**

```html
<!-- Load the polyfill first. -->
<script src="path/to/intersectin-observer.js"></script>

<!-- Load all other JavaScript. -->
<script src="app.js"></script>
```

**Using a module loader (e.g. Browserify or Webpack):**

```js
// Require the polyfill before requiring any other modules.
require('intersection-observer');

require('./foo.js');
require('./bar.js');
```

## Configuring the polyfill

It's impossible to handle all possible ways a target element could intersect with a root element without resorting to constantly polling the document for intersection changes.

To avoid this extra work and performance penalty, the default configuration of the polyfill optimizes for the most common `IntersectionObserver` use cases, which primarily include target elements intersecting with a root element due to:

- User scrolling.
- Resizing the window.
- Changes to the DOM.

All of the above can be handled without polling the DOM.

There are, however, additional use cases that the default configuration will not detect. These include target elements intersecting with a root element due to:

- CSS changes on `:hover`, `:active`, or `:focus` states.
- CSS changes due to transitions or animations with a long initial delay.
- Resizable `<textarea>` elements that cause other elements to move around.
- Scrolling of non-document elements in browsers that don't support the event capture phase.

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.

**Enabling polling for all instance:**

To enable polling for all instance, set a value for `POLL_INTERVAL` on the `IntersectionObserver` prototype:


```js
IntersectionObserver.prototype.POLL_INTERVAL = 100; // Time in milliseconds.
```

**Enabling polling for individual instance:**

To enable polling on only specific instances, set a `POLL_INTERVAL` value on the instance itself:

```js
var io = new IntersectionObserver(callback);
io.POLL_INTERVAL = 100; // Time in milliseconds.
io.observe(someTargetElement);
```

**Note:** the `POLL_INTERVAL` property must be set prior to calling the `.observe` method, or the default configuration will be used.

## Browser support

The polyfill has been tested and known to work in the latest version of all browsers.

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 support those browsers is via the following script from the [polyfill.io](https://cdn.polyfill.io/v2/docs/):

```html
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=es5,getComputedStyle"></script>
```

**Note:** future versions of polyfill.io will include this `IntersectionObserver` polyfill, so the above code will no longer be necessary.

With these polyfills, `IntersectionObserver` has been tested an known to work in the following browsers:

<table>
<tr>
<td align="center">
<img src="https://raw.github.com/alrra/browser-logos/master/chrome/chrome_48x48.png" alt="Chrome"><br>
</td>
<td align="center">
<img src="https://raw.github.com/alrra/browser-logos/master/firefox/firefox_48x48.png" alt="Firefox"><br>
</td>
<td align="center">
<img src="https://raw.github.com/alrra/browser-logos/master/safari/safari_48x48.png" alt="Safari"><br>
6+
</td>
<td align="center">
<img src="https://raw.github.com/alrra/browser-logos/master/edge/edge_48x48.png" alt="Edge"><br>
</td>
<td align="center">
<img src="https://raw.github.com/alrra/browser-logos/master/internet-explorer/internet-explorer_48x48.png" alt="Internet Explorer"><br>
7+
</td>
<td align="center">
<img src="https://raw.github.com/alrra/browser-logos/master/opera/opera_48x48.png" alt="Opera"><br>
</td>
<td align="center">
<img src="https://raw.github.com/alrra/browser-logos/master/android/android_48x48.png" alt="Android"><br>
4.4+
</td>
<td align="center">
<img src="https://raw.github.com/alrra/browser-logos/master/safari-ios/safari-ios_48x48.png" alt="iOS"><br>
9.2+
</td>
</tr>
</table>

## Running the tests

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.

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.
74 changes: 74 additions & 0 deletions polyfill/intersection-observer-test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<!--
Copyright 2016 Google Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>IntersectionObserver Tests</title>

<!-- Dependencies -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.5.3/mocha.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.5.3/mocha.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/expect.js/0.2.0/expect.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sinon.js/1.15.4/sinon.min.js"></script>

<!-- Polyfills for IE7-8 -->
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=es5,getComputedStyle"></script>
</head>
<body>
<div id="mocha"></div>

<!-- Setup -->
<script>mocha.setup({ui:'bdd'});</script>

<!-- Script under test -->
<script src="intersection-observer.js"></script>

<!-- Tests -->
<script src="intersection-observer-test.js"></script>

<!-- Reporting -->
<script>
var runner = mocha.run();
var failedTests = [];

runner.on('end', function(){
window.mochaResults = runner.stats;
window.mochaResults.reports = failedTests;
});

runner.on('fail', function(test, err){
var flattenTitles = function(test){
var titles = [];
while (test.parent.title){
titles.push(test.parent.title);
test = test.parent;
}
return titles.reverse();
};

failedTests.push({
name: test.title,
result: false,
message: err.message,
stack: err.stack,
titles: flattenTitles(test)
});
});
</script>
</body>
</html>
Loading