Skip to content

Commit

Permalink
restructure functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabian Hoffmann committed Nov 5, 2016
1 parent c63106f commit 7daddd5
Showing 1 changed file with 23 additions and 34 deletions.
57 changes: 23 additions & 34 deletions src/justlazy.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@
}(this, function() {
"use strict";

/**
* Creates an img html node and sets the attributes of the
* image. The placeholder will be replaced by the generated
* image.
*/
var _createImage = function(imgPlaceholder, imgAttributes, onloadCallback, onreplaceCallback) {
var img = document.createElement("img");

Expand All @@ -44,9 +39,6 @@
_replacePlaceholderWithImage(imgPlaceholder, img, onreplaceCallback);
};

/**
* Replaces the img placeholder (html node of any type) with the img.
*/
var _replacePlaceholderWithImage = function(imgPlaceholder, img, onreplaceCallback) {
var parentNode = imgPlaceholder.parentNode;
if (!!parentNode) {
Expand All @@ -57,9 +49,6 @@
}
};

/**
* Reads out the relevant attributes of the imagePlaceholder.
*/
var _resolveImageAttributes = function(imgPlaceholder) {
return {
src: imgPlaceholder.getAttribute("data-src"),
Expand All @@ -74,6 +63,29 @@
return options || {};
};

var _isVisible = function(placeholder, optionalThreshold) {
var windowInnerHeight = window.innerHeight || document.documentElement.clientHeight;
var threshold = optionalThreshold || 0;

return placeholder.getBoundingClientRect().top - windowInnerHeight <= threshold;
};

var _loadImgIfVisible = function(imgPlaceholder, options) {
var scrollEventCallback = function(e) {
if (_isVisible(imgPlaceholder, options.threshold)) {
lazyLoad(imgPlaceholder, options);

if (window.removeEventListener) {
window.removeEventListener(e.type, scrollEventCallback, false);
} else {
window.detachEvent("on" + e.type, scrollEventCallback);
}
}
};

return scrollEventCallback;
};

/**
* Lazy loads image with img tag.
*
Expand Down Expand Up @@ -104,29 +116,6 @@
}
};

var _isVisible = function(placeholder, optionalThreshold) {
var windowInnerHeight = window.innerHeight || document.documentElement.clientHeight;
var threshold = optionalThreshold || 0;

return placeholder.getBoundingClientRect().top - windowInnerHeight <= threshold;
};

var _loadImgIfVisible = function(imgPlaceholder, options) {
var scrollEventCallback = function(e) {
if (_isVisible(imgPlaceholder, options.threshold)) {
lazyLoad(imgPlaceholder, options);

if (window.removeEventListener) {
window.removeEventListener(e.type, scrollEventCallback, false);
} else {
window.detachEvent("on" + e.type, scrollEventCallback);
}
}
};

return scrollEventCallback;
};

/**
* Registers the lazy loading event. If the placeholder becomes visible, the image
* will be loaded automatically.
Expand Down

0 comments on commit 7daddd5

Please sign in to comment.