Skip to content

Instantly share code, notes, and snippets.

@blackmiaool
Last active January 26, 2020 12:12
Show Gist options
  • Save blackmiaool/2d75bf84ab377c0bd6d2327123cddf4a to your computer and use it in GitHub Desktop.
Save blackmiaool/2d75bf84ab377c0bd6d2327123cddf4a to your computer and use it in GitHub Desktop.
preload image
function preloadImage(src) {
const $dom = $("<img />", {
src
});
if ($dom[0].naturalWidth) {
return Promise.resolve($dom);
}
$dom.css({
opacity: 0.01,
position: 'fixed',
bottom: 0,
left: 0,
height: '1px',
width: '1px',
'z-index': 10000,
'pointer-events': 'none',
});
$(document.body).append($dom);
return new Promise((resolve) => {
$dom.on("load", () => {
$dom.detach();
resolve($dom);
});
});
}
function preloadImage(src) {
const dom=document.createElement("img");
if (dom.naturalWidth) {
return Promise.resolve(dom);
}
dom.setAttribute("src",src);
dom.setAttribute('style','opacity:0.01;position:fixed;bottom:0;left:0;height:1px;width:1px;z-index:10000;pointer-events:none;');
document.body.appendChild(dom);
return new Promise(resolve => {
dom.addEventListener('load',()=>{
document.body.removeChild(dom);
resolve(dom);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment