Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Layzr {
- constructor(config){
- this._lastScroll = 0;
- this._ticking = false;
- config = config || {};
- this._optionsContainer = document.querySelector(config.container) || window;
- this._optionsSelector = config.selector || '[data-layzr]';
- this._optionsAttr = config.attr || 'data-lazyr';
- this._optionsAttrRetina = config.retinaAttr || 'data-layzr-retina';
- this._optionsAttrBg = config.bgAttr || 'data-layzr-bg';
- this._optionsAttrHidden = config.hiddenAttr || 'data-layzr-hidden';
- this._optionsThreshold = config.threshold || 0;
- this._optionsCallback = config.callback || null;
- this._retina = window.devicePixelRatio > 1;
- this._srcAttr = this._retina ? this._optionsAttrRetina : this._optionsAttr;
- this._nodes = document.querySelectorAll(this._optionsSelector);
- this._create();
- }
- _requestScroll(){
- if ( this._optionsContainer === window ) {
- this._lastScroll = window.scrollY || window.pageYOffset;
- } else {
- this._lastScroll = this._optionsContainer.scrollTop + this._getOffset(this._optionsContainer);
- }
- this._requestTick();
- }
- _requestTick() {
- this._ticking || (requestAnimationFrame(this.update.bind(this)), this._ticking = !0);
- }
- _getOffset(a) {
- var b = 0;
- do isNaN(a.offsetTop) || (b += a.offsetTop);
- while (a = a.offsetParent);
- return b
- }
- _getContainerHeight() {
- return this._optionsContainer.innerHeight || this._optionsContainer.offsetHeight;
- }
- _create() {
- this._requestScroll();
- this._optionsContainer.addEventListener("scroll", this._requestScroll.bind(this), !1);
- this._optionsContainer.addEventListener("resize", this._requestScroll.bind(this), !1);
- }
- _destroy() {
- this._optionsContainer.removeEventListener("scroll", this._requestScroll.bind(this), !1);
- this._optionsContainer.removeEventListener("resize", this._requestScroll.bind(this), !1);
- }
- _inViewport(a) {
- var b = this._lastScroll,
- c = b + this._getContainerHeight(),
- d = this._getOffset(a),
- e = d + this._getContainerHeight(),
- f = this._optionsThreshold / 100 * window.innerHeight;
- return e >= b - f && c + f >= d && !a.hasAttribute(this._optionsAttrHidden);
- }
- _reveal(a) {
- var b = a.getAttribute(this._srcAttr) || a.getAttribute(this._optionsAttr);
- a.hasAttribute(this._optionsAttrBg) ? a.style.backgroundImage = "url(" + b + ")" : a.setAttribute("src", b), "function" == typeof this._optionsCallback && this._optionsCallback.call(a), a.removeAttribute(this._optionsAttr), a.removeAttribute(this._optionsAttrRetina), a.removeAttribute(this._optionsAttrBg), a.removeAttribute(this._optionsAttrHidden);
- }
- updateSelector() {
- this._nodes = document.querySelectorAll(this._optionsSelector);
- }
- update() {
- for (var a = this._nodes.length, b = 0; a > b; b++) {
- var c = this._nodes[b];
- c.hasAttribute(this._optionsAttr) && this._inViewport(c) && this._reveal(c)
- }
- this._ticking = !1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement