Skip to content

Commit

Permalink
Move lazyIconScript to standard JavaScript
Browse files Browse the repository at this point in the history
**NOTE**
Since *jquery*s latest release has many breaking changes normalizing to standard ES5 but a bit of CSS3 transitions.

Applies to OpenUserJS#904 ... post trial change for *jquery* incompatibilities.
  • Loading branch information
Martii committed Aug 16, 2016
1 parent 7a25e7e commit 3081449
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 21 deletions.
17 changes: 17 additions & 0 deletions public/css/common.css
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,20 @@ ul.flaggedList {
border-radius: 0 3px 3px 0;
text-shadow: 0 1px #045481;
}


.translucent-icon {
opacity: 0;
}

.hide-icon {
display: none;
}

.opaque-icon {
opacity: 1;
}

.fade-icon {
transition: opacity 0.33s ease-in-out;
}
79 changes: 58 additions & 21 deletions views/includes/scripts/lazyIconScript.html
Original file line number Diff line number Diff line change
@@ -1,27 +1,64 @@
<script type="text/javascript">
(function () {
'use strict';

$(window).load(function () {
window.setTimeout(function () {
$('[data-icon-src]').each(function () {
var container = $(this);
var size = container.data('icon-size');
var img = new Image();

if (size) {
img.width = size;
img.height = size;
}

$(img).load(function () {
var that = $(this).hide();

container.empty().append(that);
that.fadeIn();
}).attr('src', container.data('icon-src'));
});
}, 13);
});
function onShow(aImg) {
aImg.classList.remove('translucent-icon');
aImg.classList.add('opaque-icon');
}

function onError(aEv) {
var img = aEv.target;
var container = img.parentNode;

container.removeChild(img);
}

function onLoad(aEv) {
var img = aEv.target;
var container = img.parentNode;

var node = container.querySelector('i');

container.removeChild(node);
img.classList.remove('hide-icon');

setTimeout(onShow, 250, img);
}

function onTimeout() {
var nodes = document.querySelectorAll('[data-icon-src]');
var i = null;
var container = null;
var dataIconSrc = null;
var node = null;
var img = null;

for (i = 0; container = nodes[i]; i++) {
dataIconSrc = container.getAttribute('data-icon-src');

node = container.querySelector('i');

img = new Image();

img.addEventListener('load', onLoad);
img.addEventListener('error', onError);

img.classList.add('hide-icon');
img.classList.add('fade-icon');
img.classList.add('translucent-icon');

img.src = dataIconSrc;

container.appendChild(img);
}
}

function onDOMContentLoaded(aEv) {
window.setTimeout(onTimeout, 13);
}

document.addEventListener('DOMContentLoaded', onDOMContentLoaded);

})();
</script>

0 comments on commit 3081449

Please sign in to comment.