Skip to content

Instantly share code, notes, and snippets.

@dohoons
Last active February 22, 2016 06:28
Show Gist options
  • Save dohoons/8abc48728408da2006ce to your computer and use it in GitHub Desktop.
Save dohoons/8abc48728408da2006ce to your computer and use it in GitHub Desktop.
// 프리로딩
(function () {
var imgArr = [];
var loaded = 0;
var total = 0;
var progress = 0;
$("*").each(function(index, el) {
if(this.tagName == "IMG") {
imgArr.push($(this).attr("src"));
} else {
var bg = $(this).css("background");
var url = "";
if(bg) {
if(bg.indexOf("url") > -1) {
url = bg.split('url(')[1].split(')')[0];
url = url.replace('"','').replace('"','');
url = url.replace("'","").replace("'","");
imgArr.push(url);
}
}
}
});
total = imgArr.length;
$(imgArr).each(function(i, val) {
preloadImg(val);
});
function preloadImg(url) {
var i = new Image();
i.crossOrigin = '*';
$(i).on("load error", function () {
loaded++;
var progress = Math.round(loaded / total * 100);
// 로딩 진행률
$(".loading .progress span").css("width", progress + "%");
$(".loading .progress_txt").html(progress + "%");
if(progress == 100) {
setTimeout(function () {
// 로딩 제거
$("body.page_ready").removeClass("page_ready");
// 초기화
pageLoaded();
}, 200);
}
});
i.src = url;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment