Created
September 30, 2012 22:12
-
-
Save periscopesnippets/3808588 to your computer and use it in GitHub Desktop.
jQuery: Image Preloader
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
$.fn.preloader = function (options) { | |
var defaults = { | |
delay: 200, | |
preload_parent: "", | |
check_timer: 300, | |
ondone: function () {}, | |
oneachload: function (image) {}, | |
fadein: 500 | |
}; | |
// variables declaration and precaching images and parent container | |
var options = $.extend(defaults, options), | |
root = $(this), | |
images = root.find("img") | |
.css({ | |
"visibility": "hidden", | |
opacity: 0 | |
}), | |
timer, counter = 0, | |
i = 0, | |
checkFlag = [], | |
delaySum = options.delay, | |
init = function () { | |
timer = setInterval(function () { | |
if (counter >= checkFlag.length) { | |
clearInterval(timer); | |
options.ondone(); | |
return; | |
} | |
for (i = 0; i < images.length; i++) { | |
if (images[i].complete === true) { | |
if (checkFlag[i] === false) { | |
checkFlag[i] = true; | |
options.oneachload(images[i]); | |
counter++; | |
delaySum = delaySum + options.delay; | |
} | |
$(images[i]) | |
.css("visibility", "visible") | |
.delay(delaySum) | |
.animate({ | |
opacity: 1 | |
}, options.fadein, | |
function () { | |
$(this) | |
.parent() | |
.removeClass("preloader"); | |
}); | |
} | |
} | |
}, options.check_timer); | |
}; | |
images.each(function () { | |
if ($(this) | |
.parent(options.preload_parent) | |
.length === 0) $(this) | |
.wrap("<div class='preloader' />"); | |
else $(this) | |
.parent() | |
.addClass("preloader"); | |
checkFlag[i++] = false; | |
}); | |
images = $.makeArray(images); | |
var icon = jQuery("<img />", { | |
id: 'loadingicon', | |
src: '/assets/img/ajax-loader.gif' | |
}) | |
.hide() | |
.appendTo("body"); | |
timer = setInterval(function () { | |
if (icon[0].complete === true) { | |
clearInterval(timer); | |
init(); | |
icon.remove(); | |
return; | |
} | |
}, 100); | |
}; | |
$(function () { | |
jQuery(".grid") | |
.preloader(); //activate the preloader on specified images | |
}); |
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
$.fn.preloader = function (options) { | |
var defaults = { | |
delay: 200, | |
preload_parent: "", | |
check_timer: 300, | |
ondone: function () {}, | |
oneachload: function (image) {}, | |
fadein: 500 | |
}; | |
// variables declaration and precaching images and parent container | |
var options = $.extend(defaults, options), | |
root = $(this), | |
images = root.find("img") | |
.css({ | |
"visibility": "hidden", | |
opacity: 0 | |
}), | |
timer, counter = 0, | |
i = 0, | |
checkFlag = [], | |
delaySum = options.delay, | |
init = function () { | |
timer = setInterval(function () { | |
if (counter >= checkFlag.length) { | |
clearInterval(timer); | |
options.ondone(); | |
return; | |
} | |
for (i = 0; i < images.length; i++) { | |
if (images[i].complete === true) { | |
if (checkFlag[i] === false) { | |
checkFlag[i] = true; | |
options.oneachload(images[i]); | |
counter++; | |
delaySum = delaySum + options.delay; | |
} | |
$(images[i]) | |
.css("visibility", "visible") | |
.delay(delaySum) | |
.animate({ | |
opacity: 1 | |
}, options.fadein, | |
function () { | |
$(this) | |
.parent() | |
.removeClass("preloader"); | |
}); | |
} | |
} | |
}, options.check_timer); | |
}; | |
images.each(function () { | |
if ($(this) | |
.parent(options.preload_parent) | |
.length === 0) $(this) | |
.wrap("<div class='preloader' />"); | |
else $(this) | |
.parent() | |
.addClass("preloader"); | |
checkFlag[i++] = false; | |
}); | |
images = $.makeArray(images); | |
var icon = jQuery("<img />", { | |
id: 'loadingicon', | |
src: '/assets/img/ajax-loader.gif' | |
}) | |
.hide() | |
.appendTo("body"); | |
timer = setInterval(function () { | |
if (icon[0].complete === true) { | |
clearInterval(timer); | |
init(); | |
icon.remove(); | |
return; | |
} | |
}, 100); | |
}; | |
$(function () { | |
jQuery(".grid") | |
.preloader(); //activate the preloader on specified images | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment