Detect if an Ember View or Component is in the viewport @ 60FPS
ember-in-viewport is built and maintained by DockYard, contact us for expert Ember.js consulting.
This ember-cli addon adds a simple, highly performant Ember Mixin to your app. This Mixin, when added to a View or Component (collectively referred to as Components), will allow you to check if that Component has entered the browser's viewport. By default, the Mixin uses the IntersectionObserver API if it detects it in your user's browser – failing which, it fallsback to using requestAnimationFrame, then if not available, the Ember run loop and event listeners.
- Dummy app (
ember serve): https://github.com/DockYard/ember-in-viewport/tree/master/tests/dummy - ember-infinity
- ember-light-table
- Tracking advertisement impressions
- Lazy loading images
- Occlusion culling
ember install ember-in-viewport
Usage is simple. First, add the Mixin to your Component:
import Component from '@ember/component';
import InViewportMixin from 'ember-in-viewport';
export default Component.extend(InViewportMixin, {
// ...
});These hooks fire once whenever the Component enters or exits the viewport. You can handle them the same way you would handle any other native Ember hook:
import Component from '@ember/component';
import InViewportMixin from 'ember-in-viewport';
export default Component.extend(InViewportMixin, {
didEnterViewport() {
console.log('entered');
},
didExitViewport() {
console.log('exited');
}
});The didScroll hook fires when an element enters the viewport. For example, if you scrolled down in order to move the element in the viewport, the didScroll hook would fire and also receive the direction as a string. You can then handle it like another hook as in the above example.
import Component from '@ember/component';
import InViewportMixin from 'ember-in-viewport';
export default Component.extend(InViewportMixin, {
didScroll(direction) {
console.log(direction); // 'up' || 'down' || 'left' || 'right'
}
});To apply an .active class to your Component when it enters the viewport, you can simply bind the active class to the mixed in property viewportEntered, like so:
import Component from '@ember/component';
import InViewportMixin from 'ember-in-viewport';
export default Component.extend(InViewportMixin, {
classNameBindings: [ 'viewportEntered:active' ]
});This hook fires whenever the Component leaves the viewport.
The mixin comes with some options. Due to the way listeners and IntersectionObserver API or requestAnimationFrame is setup, you'll have to override the options this way:
import Component from '@ember/component';
import InViewportMixin from 'ember-in-viewport';
import { setProperties } from '@ember/object';
export default Component.extend(InViewportMixin, {
init() {
this._super(...arguments);
setProperties(this, {
viewportEnabled : true,
viewportUseRAF : true,
viewportSpy : false,
viewportScrollSensitivity : 1,
viewportRefreshRate : 150,
intersectionThreshold : 0,
scrollableArea : null,
viewportTolerance: {
top : 50,
bottom : 50,
left : 20,
right : 20
}
});
}
});-
viewportEnabled: booleanDefault:
trueSet to false to have no listeners registered. Useful if you have components that function with either viewport listening on or off.
-
viewportUseIntersectionObserver: booleanDefault: Depends on browser
Read-only
The Mixin by default will use the IntersectionObserver API. If IntersectionObserver is not supported in the target browser, ember-in-viewport will fallback to rAF. We prevent users from explicitly setting this to
trueas browsers lacking support for IntersectionObserver will throw an error.(https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) (https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/thresholds#Browser_compatibility)
-
intersectionThreshold: decimal or arrayDefault: 0
A single number or array of numbers between 0.0 and 1.0. A value of 0.0 means the target will be visible when the first pixel enters the viewport. A value of 1.0 means the entire target must be visible to fire the didEnterViewport hook. Similarily, [0, .25, .5, .75, 1] will fire didEnterViewport every 25% of the target that is visible. (https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#Thresholds)
Some notes:
- If the target is offscreen, you will get a notification via
didExitViewportthat the target is initially offscreen. Similarily, this is possible to notify if onscreen when your site loads. - If intersectionThreshold is set to anything greater than 0, you will not see
didExitViewporthook fired due to our use of theisIntersectingproperty. See last comment here: https://bugs.chromium.org/p/chromium/issues/detail?id=713819 for purpose ofisIntersecting - To get around the above issue and have
didExitViewportfire, set yourintersectionThresholdto[0, 1.0]. When set to just1.0, when the element is 99% visible and still has isIntersecting as true, when the element leaves the viewport, the element isn't applicable to the observer anymore, so the callback isn't called again. - If your intersectionThreshold is set to 0 you will get notified if the target
didEnterViewportanddidExitViewportat the appropriate time.
- If the target is offscreen, you will get a notification via
-
scrollableAreaDefault: null
A CSS selector for the scrollable area. e.g.
".my-list" -
viewportUseRAF: booleanDefault: Depends on browser
As its name suggests, if this is
trueand the IntersectionObserver API is not available in the target browser, the Mixin will userequestAnimationFrame. Unless you want to force enabling or disabling this, you won't need to override this option. -
viewportSpy: booleanDefault:
falseWhen
true, the Mixin will continually watch theComponentand re-fire hooks whenever it enters or leaves the viewport. Because this is expensive, this behaviour is opt-in. When false, the Mixin will only watch theComponentuntil it enters the viewport once, and then it setsviewportEnteredtotrue(permanently), and unbinds listeners. This reduces the load on the Ember run loop and your application.NOTE: If using IntersectionObserver (default), viewportSpy should always be set to true. However, browsers (Safari) that don't currently support IntersectionObserver, this addon will use rAF which, depending on your use case, the default of
falsemay be acceptable. -
viewportScrollSensitivity: numberDefault:
1This value determines the degree of sensitivity (in
px) in which a DOM element is considered to have scrolled into the viewport. For example, if you setviewportScrollSensitivityto10, thedidScroll{...}hooks would only fire if the scroll was greater than10px. -
viewportRefreshRate: numberDefault:
100If
IntersectionObserverandrequestAnimationFrameis not present, this value determines how often the Mixin checks your component to determine whether or not it has entered or left the viewport. The lower this number, the more often it checks, and the more load is placed on your application. Generally, you'll want this value between100to300, which is about the range at which people consider things to be "real-time".This value also affects how often the Mixin checks scroll direction.
-
viewportTolerance: objectDefault:
{ top: 0, left: 0, bottom: 0, right: 0 }This option determines how accurately the
Componentneeds to be within the viewport for it to be considered as entered. Add bottom margin to preemptively trigger didEnterViewport.For IntersectionObserver, this property interpolates to rootMargin. For rAF, this property will use
bottomtolerance and measure against the height of the container to determine when to trigger didEnterViewport.Also, if your sentinel (component that uses this mixin) is a zero-height element, ensure that the sentinel actually is able to enter the viewport.
You can set application wide defaults for ember-in-viewport in your app (they are still manually overridable inside of a Component). To set new defaults, just add a config object to config/environment.js, like so:
module.exports = function(environment) {
var ENV = {
// ...
viewportConfig: {
viewportEnabled : false,
viewportUseRAF : true,
viewportSpy : false,
viewportScrollSensitivity : 1,
viewportRefreshRate : 100,
viewportListeners : [],
intersectionThreshold : 0,
scrollableArea : null,
viewportTolerance: {
top : 0,
left : 0,
bottom : 0,
right : 0
}
}
};
};
Note if you want to disable right and left in-viewport triggers, set these values to `Infinity`.| Chrome | 51 [1] |
| Firefox (Gecko) | 55 [2] |
| MS Edge | 15 |
| Internet Explorer | Not supported |
| Opera [1] | 38 |
| Safari | Safari Technology Preview |
| Chrome for Android | 59 |
| Android Browser | 56 |
| Opera Mobile | 37 |
- [1] Reportedly available, it didn't trigger the events on initial load and lacks
isIntersectinguntil later versions. - [2] This feature was implemented in Gecko 53.0 (Firefox 53.0 / Thunderbird 53.0 / SeaMonkey 2.50) behind the preference
dom.IntersectionObserver.enabled.
ember serve- Visit your app at http://localhost:4200.
ember testember test --serve
ember build
For more information on using ember-cli, visit http://www.ember-cli.com/.
DockYard, Inc © 2015