-
-
Save LuisSevillano/8405308f6d3ea7423c9f9c13b7e248e3 to your computer and use it in GitHub Desktop.
easy script for ai2html files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// easy script for ai2html files | |
// ai2html uses css's display block/in-line to show correct artboards depending on screen size. | |
// if we not use some script like this one or a lazy load plugin our reader will download one png file for each artboard although he is not seeing it. | |
// function that manage every time the screen is resized. Avoids to call 'checkimages' function multiples times. | |
function debounce(fn, delay) { | |
var timer = null; | |
return function () { | |
var context = this, args = arguments; | |
clearTimeout(timer); | |
timer = setTimeout(function () { | |
fn.apply(context, args); | |
}, delay); | |
}; | |
} | |
// check what div is hidden and what doesnt. | |
function isHidden(el) { | |
return el.offsetWidth > 0; | |
} | |
// function changes the data-src attribute to src forcing to downloading the correct image. | |
function checkImages(){ | |
var imagesArray = document.querySelectorAll(".g-aiImg"); | |
for (var i in imagesArray) { | |
if (isHidden(imagesArray[i])) { | |
imagesArray[i].src = imagesArray[i].getAttribute("data-src"); | |
} | |
} | |
} | |
//event listeners | |
document.addEventListener('DOMContentLoaded', checkImages); | |
window.addEventListener('resize', debounce(checkImages, 300)); | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment