-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjquery.imageReloader.js
More file actions
74 lines (64 loc) · 1.8 KB
/
jquery.imageReloader.js
File metadata and controls
74 lines (64 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* jQuery Image Reloader
*
* @author Dumitru Glavan
* @link http://dumitruglavan.com/
* @version 1.0 (3-FEB-2012)
* @requires jQuery v1.4 or later
*
* @example $('.slow-images').imageReloader()
*
* Find source on GitHub: https://github.com/doomhz/jQuery-Image-Reloader
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
(function ($) {
$.fn.imageReloader = function (options) {
options = $.extend({}, options, {
loadingClass: "loading-image",
reloadTime: 1500,
maxTries: 10
});
var $self = $(this);
if ($self.length > 1) {
$self.each(function (i, el) {
$(el).imageReloader(options);
});
return $self;
}
$self.data("reload-times", 0);
var imageHeight = $self.height()
var imageWidth = $self.width()
var $imageReplacer = $('<div class="' + options.loadingClass + '">');
$imageReplacer.css({height: imageHeight, width: imageWidth})
$imageReplacer.hide();
$imageReplacer.insertAfter($self);
var showImage = function () {
$self.show();
$imageReplacer.remove();
};
$self.bind("error", function () {
$self.hide();
$imageReplacer.show()
var reloadTimes = $self.data("reload-times");
if (reloadTimes < options.maxTries) {
setTimeout(function () {
$self.attr("src", $self.attr("src"));
var reloadTimes = $self.data("reload-times");
reloadTimes++;
$self.data("reload-times", reloadTimes);
}, options.reloadTime);
} else if (!$self.is(":visible")) {
showImage();
$self.attr("alt", "Image not found :(");
}
});
$self.bind("load", function () {
showImage();
});
return this;
};
})(jQuery);