Skip to content

Commit

Permalink
feat: add imagesLoad util
Browse files Browse the repository at this point in the history
  • Loading branch information
kitian616 committed Dec 1, 2018
1 parent 7fb22d5 commit cdf5560
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
2 changes: 1 addition & 1 deletion _includes/scripts/components/lightbox.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function () {
var SOURCES = window.TEXT_VARIABLES.sources;
window.Lazyload.js(SOURCES.jquery, function() {
window.pageLoad.then(function() {
window.imagesLoad.then(function() {
/* global Gallery */
var $pageGalleryModal = $('.js-page-gallery-modal');
var pageGalleryModal = $pageGalleryModal.modal({ onChange: handleModalChange });
Expand Down
26 changes: 26 additions & 0 deletions _includes/scripts/utils/imagesLoad.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
(function() {
var images = document.getElementsByTagName('img'), image;
var imagesCount = images.length, loadedCount = 0;
var i, j, loaded = false, cbs = [];
imagesCount < 1 && (loaded = true);
for (i = 0; i < imagesCount; i++) {
image = images[i];
image.complete ? handleImageLoad() : image.addEventListener('load', handleImageLoad);
}
function handleImageLoad() {
loadedCount++;
if (loadedCount === imagesCount) {
loaded = true;
if (cbs.length > 0) {
for (j = 0; j < cbs.length; j++) {
cbs[j]();
}
}
}
}
window.imagesLoad = {
then: function(cb) {
cb && (loaded ? cb() : (cbs.push(cb)));
}
};
})();
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
window.pageLoad = (function () {
var loaded = false, cbs = [];
window.addEventListener('load', function () {
var i, cb; loaded = true;
var i;
loaded = true;
if (cbs.length > 0) {
for (i = 0; i < cbs.length; i++) {
cb = cbs[i]; cb();
cbs[i]();
}
}
});
Expand Down
7 changes: 5 additions & 2 deletions _layouts/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{%- include analytics.html -%}
{%- include head.html -%}
<script>
{%- include scripts/utils.js -%}
{%- include scripts/utils/utils.js -%}
{%- include scripts/lib/throttle.js -%}
{%- include scripts/lib/lazyload.js -%}
</script>
Expand All @@ -18,6 +18,9 @@
<div class="root" data-is-touch="false">
{{ content }}
</div>
<script>{%- include scripts/common.js -%}</script>
<script>
{%- include scripts/utils/imagesLoad.js -%}
{%- include scripts/common.js -%}
</script>
</body>
</html>
7 changes: 5 additions & 2 deletions docs/_layouts/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{%- include analytics.html -%}
{%- include head.html -%}
<script>
{%- include scripts/utils.js -%}
{%- include scripts/utils/utils.js -%}
{%- include scripts/lib/throttle.js -%}
{%- include scripts/lib/lazyload.js -%}
</script>
Expand All @@ -26,6 +26,9 @@
<div class="root" data-is-touch="false">
{{ content }}
</div>
<script>{%- include scripts/common.js -%}</script>
<script>
{%- include scripts/utils/imagesLoad.js -%}
{%- include scripts/common.js -%}
</script>
</body>
</html>

0 comments on commit cdf5560

Please sign in to comment.