Last active
January 26, 2020 12:12
-
-
Save blackmiaool/2d75bf84ab377c0bd6d2327123cddf4a to your computer and use it in GitHub Desktop.
preload image
This file contains hidden or 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
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); | |
}); | |
}); | |
} |
This file contains hidden or 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
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