1
0
Fork 0
SnoozeSquad/SnoozeSquad.min.js

9 lines
1.3 KiB
JavaScript
Raw Normal View History

2016-05-28 15:51:39 +00:00
/******************************************************************************
******************************* SnoozeSquad.js *******************************
******************************************** v0.1 ** by Starbeamrainbowlabs **
* From https://github.com/sbrl/SnoozeSquad/
* 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{}}}