1
0
Fork 0

Add support for passive events.

This commit is contained in:
Starbeamrainbowlabs 2016-06-09 09:06:10 +01:00
parent 661a80cde9
commit e8539a4b35
2 changed files with 9 additions and 2 deletions

View File

@ -18,10 +18,17 @@ class SnoozeSquad
start() start()
{ {
// Passive event listener check from
// https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md
var supportsPassiveEvents = false;
try {
var opts = Object.defineProperty({}, 'passive', { get: function() { supportsPassiveEvents = true; } });
window.addEventListener("test", null, opts);
} catch (e) {}
window.addEventListener("scroll", (function(event) { window.addEventListener("scroll", (function(event) {
if((+new Date()) - this.lastUpdateTime > this.updateInterval) if((+new Date()) - this.lastUpdateTime > this.updateInterval)
this.update(); this.update();
}).bind(this)); }).bind(this), supportsPassiveEvents ? { passive: true } : false);
} }
update() update()

2
SnoozeSquad.min.js vendored
View File

@ -5,4 +5,4 @@
* Licensed under the MIT license. * Licensed under the MIT license.
******************************************************************************/ ******************************************************************************/
"use strict";class SnoozeSquad{constructor(t){this.range=1;this.attributeName="data-src";this.updateInterval=250;for(let e in t)this[e]=t[e];this.querySelector=`[${this.attributeName}]`;this.lastUpdateTime=0;this.start()}start(){window.addEventListener("scroll",function(t){if(+new Date-this.lastUpdateTime>this.updateInterval)this.update()}.bind(this))}update(){this.lastUpdateTime=+new Date;var t=this.getSnoozingElements();for(let e of t){if(this.isElementInRange(e))this.wakeUpElement(e)}}getSnoozingElements(){return document.querySelectorAll(this.querySelector)}isElementInRange(t){var e=t.getBoundingClientRect(),i=this.range*window.innerHeight;if(e.bottom>-i&&e.top<window.innerHeight+i&&e.right>-i&&e.left<window.innerWidth+i){return true}return false}wakeUpElement(t){var e=this.attributeName.replace("data-","");if(t.dataset.hasOwnProperty(e)){t.src=t.dataset[e]}else{}}} "use strict";class SnoozeSquad{constructor(t){this.range=1;this.attributeName="data-src";this.updateInterval=250;for(let e in t)this[e]=t[e];this.querySelector=`[${this.attributeName}]`;this.lastUpdateTime=0;this.start()}start(){var t=false;try{var e=Object.defineProperty({},"passive",{get:function(){t=true}});window.addEventListener("test",null,e)}catch(t){}window.addEventListener("scroll",function(t){if(+new Date-this.lastUpdateTime>this.updateInterval)this.update()}.bind(this),t?{passive:true}:false)}update(){this.lastUpdateTime=+new Date;var t=this.getSnoozingElements();for(let e of t){if(this.isElementInRange(e))this.wakeUpElement(e)}}getSnoozingElements(){return document.querySelectorAll(this.querySelector)}isElementInRange(t){var e=t.getBoundingClientRect(),i=this.range*window.innerHeight;if(e.bottom>-i&&e.top<window.innerHeight+i&&e.right>-i&&e.left<window.innerWidth+i){return true}return false}wakeUpElement(t){var e=this.attributeName.replace("data-","");if(t.dataset.hasOwnProperty(e)){t.src=t.dataset[e]}else{}}}