From 5266f94cc345620c19680c82c72f0d4d33ea77c4 Mon Sep 17 00:00:00 2001 From: Michael Cavalea Date: Thu, 16 Jul 2015 11:20:31 -0400 Subject: [PATCH] fix destroy method (#32) --- bower.json | 2 +- dist/layzr.js | 15 ++++++++------- dist/layzr.min.js | 2 +- package.json | 2 +- src/layzr.js | 15 ++++++++------- 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/bower.json b/bower.json index 46352a8..4c40097 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "layzr.js", - "version": "1.4.2", + "version": "1.4.3", "description": "A small, fast, modern, and dependency-free library for lazy loading.", "homepage": "http://callmecavs.github.io/layzr.js/", "main": ["dist/layzr.js"], diff --git a/dist/layzr.js b/dist/layzr.js index 1cb1dc8..a7666a8 100644 --- a/dist/layzr.js +++ b/dist/layzr.js @@ -41,6 +41,9 @@ function Layzr(options) { // nodelist this._nodes = document.querySelectorAll(this._optionsSelector); + // scroll and resize handler + this._handlerBind = this._requestScroll.bind(this); + // call to create this._create(); } @@ -84,19 +87,17 @@ Layzr.prototype._getContainerHeight = function() { Layzr.prototype._create = function() { // fire scroll event once - this._requestScroll(); + this._handlerBind(); // bind scroll and resize event - this._optionsContainer.addEventListener('scroll', this._requestScroll.bind(this), false); - this._optionsContainer.addEventListener('resize', this._requestScroll.bind(this), false); + this._optionsContainer.addEventListener('scroll', this._handlerBind, false); + this._optionsContainer.addEventListener('resize', this._handlerBind, false); }; Layzr.prototype._destroy = function() { - // possibly remove attributes, and set all sources? - // unbind scroll and resize event - this._optionsContainer.removeEventListener('scroll', this._requestScroll.bind(this), false); - this._optionsContainer.removeEventListener('resize', this._requestScroll.bind(this), false); + this._optionsContainer.removeEventListener('scroll', this._handlerBind, false); + this._optionsContainer.removeEventListener('resize', this._handlerBind, false); }; Layzr.prototype._inViewport = function(node) { diff --git a/dist/layzr.min.js b/dist/layzr.min.js index 274bf31..274f01a 100644 --- a/dist/layzr.min.js +++ b/dist/layzr.min.js @@ -3,4 +3,4 @@ * Copyright (c) 2015 Michael Cavalea - http://callmecavs.github.io/layzr.js/ * License: MIT */ -!function(t,i){"function"==typeof define&&define.amd?define([],i):"object"==typeof exports?module.exports=i():t.Layzr=i()}(this,function(){"use strict";function t(t){this._lastScroll=0,this._ticking=!1,t=t||{},this._optionsContainer=document.querySelector(t.container)||window,this._optionsSelector=t.selector||"[data-layzr]",this._optionsAttr=t.attr||"data-layzr",this._optionsAttrRetina=t.retinaAttr||"data-layzr-retina",this._optionsAttrBg=t.bgAttr||"data-layzr-bg",this._optionsAttrHidden=t.hiddenAttr||"data-layzr-hidden",this._optionsThreshold=t.threshold||0,this._optionsCallback=t.callback||null,this._retina=window.devicePixelRatio>1,this._srcAttr=this._retina?this._optionsAttrRetina:this._optionsAttr,this._nodes=document.querySelectorAll(this._optionsSelector),this._create()}return t.prototype._requestScroll=function(){this._lastScroll=this._optionsContainer===window?window.pageYOffset:this._optionsContainer.scrollTop+this._getOffset(this._optionsContainer),this._requestTick()},t.prototype._requestTick=function(){this._ticking||(requestAnimationFrame(this.update.bind(this)),this._ticking=!0)},t.prototype._getOffset=function(t){return t.getBoundingClientRect().top+window.pageYOffset},t.prototype._getContainerHeight=function(){return this._optionsContainer.innerHeight||this._optionsContainer.offsetHeight},t.prototype._create=function(){this._requestScroll(),this._optionsContainer.addEventListener("scroll",this._requestScroll.bind(this),!1),this._optionsContainer.addEventListener("resize",this._requestScroll.bind(this),!1)},t.prototype._destroy=function(){this._optionsContainer.removeEventListener("scroll",this._requestScroll.bind(this),!1),this._optionsContainer.removeEventListener("resize",this._requestScroll.bind(this),!1)},t.prototype._inViewport=function(t){var i=this._lastScroll,e=i+this._getContainerHeight(),o=this._getOffset(t),n=o+this._getContainerHeight(),s=this._optionsThreshold/100*window.innerHeight;return n>=i-s&&e+s>=o&&!t.hasAttribute(this._optionsAttrHidden)},t.prototype._reveal=function(t){var i=t.getAttribute(this._srcAttr)||t.getAttribute(this._optionsAttr);t.hasAttribute(this._optionsAttrBg)?t.style.backgroundImage="url("+i+")":t.setAttribute("src",i),"function"==typeof this._optionsCallback&&this._optionsCallback.call(t),t.removeAttribute(this._optionsAttr),t.removeAttribute(this._optionsAttrRetina),t.removeAttribute(this._optionsAttrBg),t.removeAttribute(this._optionsAttrHidden)},t.prototype.updateSelector=function(){this._nodes=document.querySelectorAll(this._optionsSelector)},t.prototype.update=function(){for(var t=this._nodes.length,i=0;t>i;i++){var e=this._nodes[i];e.hasAttribute(this._optionsAttr)&&this._inViewport(e)&&this._reveal(e)}this._ticking=!1},t}); \ No newline at end of file +!function(t,i){"function"==typeof define&&define.amd?define([],i):"object"==typeof exports?module.exports=i():t.Layzr=i()}(this,function(){"use strict";function t(t){this._lastScroll=0,this._ticking=!1,t=t||{},this._optionsContainer=document.querySelector(t.container)||window,this._optionsSelector=t.selector||"[data-layzr]",this._optionsAttr=t.attr||"data-layzr",this._optionsAttrRetina=t.retinaAttr||"data-layzr-retina",this._optionsAttrBg=t.bgAttr||"data-layzr-bg",this._optionsAttrHidden=t.hiddenAttr||"data-layzr-hidden",this._optionsThreshold=t.threshold||0,this._optionsCallback=t.callback||null,this._retina=window.devicePixelRatio>1,this._srcAttr=this._retina?this._optionsAttrRetina:this._optionsAttr,this._nodes=document.querySelectorAll(this._optionsSelector),this._handlerBind=this._requestScroll.bind(this),this._create()}return t.prototype._requestScroll=function(){this._lastScroll=this._optionsContainer===window?window.pageYOffset:this._optionsContainer.scrollTop+this._getOffset(this._optionsContainer),this._requestTick()},t.prototype._requestTick=function(){this._ticking||(requestAnimationFrame(this.update.bind(this)),this._ticking=!0)},t.prototype._getOffset=function(t){return t.getBoundingClientRect().top+window.pageYOffset},t.prototype._getContainerHeight=function(){return this._optionsContainer.innerHeight||this._optionsContainer.offsetHeight},t.prototype._create=function(){this._handlerBind(),this._optionsContainer.addEventListener("scroll",this._handlerBind,!1),this._optionsContainer.addEventListener("resize",this._handlerBind,!1)},t.prototype._destroy=function(){this._optionsContainer.removeEventListener("scroll",this._handlerBind,!1),this._optionsContainer.removeEventListener("resize",this._handlerBind,!1)},t.prototype._inViewport=function(t){var i=this._lastScroll,e=i+this._getContainerHeight(),o=this._getOffset(t),n=o+this._getContainerHeight(),s=this._optionsThreshold/100*window.innerHeight;return n>=i-s&&e+s>=o&&!t.hasAttribute(this._optionsAttrHidden)},t.prototype._reveal=function(t){var i=t.getAttribute(this._srcAttr)||t.getAttribute(this._optionsAttr);t.hasAttribute(this._optionsAttrBg)?t.style.backgroundImage="url("+i+")":t.setAttribute("src",i),"function"==typeof this._optionsCallback&&this._optionsCallback.call(t),t.removeAttribute(this._optionsAttr),t.removeAttribute(this._optionsAttrRetina),t.removeAttribute(this._optionsAttrBg),t.removeAttribute(this._optionsAttrHidden)},t.prototype.updateSelector=function(){this._nodes=document.querySelectorAll(this._optionsSelector)},t.prototype.update=function(){for(var t=this._nodes.length,i=0;t>i;i++){var e=this._nodes[i];e.hasAttribute(this._optionsAttr)&&this._inViewport(e)&&this._reveal(e)}this._ticking=!1},t}); \ No newline at end of file diff --git a/package.json b/package.json index abf9ada..38cbda1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "layzr.js", - "version": "1.4.2", + "version": "1.4.3", "description": "A small, fast, modern, and dependency-free library for lazy loading.", "homepage": "http://callmecavs.github.io/layzr.js/", "main": "dist/layzr.js", diff --git a/src/layzr.js b/src/layzr.js index edd872b..9d5f839 100644 --- a/src/layzr.js +++ b/src/layzr.js @@ -26,6 +26,9 @@ function Layzr(options) { // nodelist this._nodes = document.querySelectorAll(this._optionsSelector); + // scroll and resize handler + this._handlerBind = this._requestScroll.bind(this); + // call to create this._create(); } @@ -69,19 +72,17 @@ Layzr.prototype._getContainerHeight = function() { Layzr.prototype._create = function() { // fire scroll event once - this._requestScroll(); + this._handlerBind(); // bind scroll and resize event - this._optionsContainer.addEventListener('scroll', this._requestScroll.bind(this), false); - this._optionsContainer.addEventListener('resize', this._requestScroll.bind(this), false); + this._optionsContainer.addEventListener('scroll', this._handlerBind, false); + this._optionsContainer.addEventListener('resize', this._handlerBind, false); }; Layzr.prototype._destroy = function() { - // possibly remove attributes, and set all sources? - // unbind scroll and resize event - this._optionsContainer.removeEventListener('scroll', this._requestScroll.bind(this), false); - this._optionsContainer.removeEventListener('resize', this._requestScroll.bind(this), false); + this._optionsContainer.removeEventListener('scroll', this._handlerBind, false); + this._optionsContainer.removeEventListener('resize', this._handlerBind, false); }; Layzr.prototype._inViewport = function(node) {