-
Notifications
You must be signed in to change notification settings - Fork 4
/
jquery.retina.js
43 lines (43 loc) · 1.56 KB
/
jquery.retina.js
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
(function($) {
$.fn.retina = function(options) {
var settings = {
"retina-background" : false,
"retina-suffix" : "@2x"
};
if (options) {
$.extend(settings, options);
}
var preload = function(path, callback) {
var img = new Image();
img.onload = function() { callback(img) };
img.src = path;
};
if (window.devicePixelRatio > 1) {
this.each(function() {
var element = $(this);
if (this.tagName.toLowerCase() == "img" && element.attr("src")) {
var path = element.attr("src").replace(/\.(?!.*\.)/, settings["retina-suffix"] +".");
preload(path, function(img) {
element.attr("src", img.src);
var imgHtml = $("<div>").append(element.clone()).remove().html();
if (!(/(width|height)=["']\d+["']/.test(imgHtml))) {
element.attr("width", img.width / 2);
}
});
}
if (settings["retina-background"]) {
var backgroundImageUrl = element.css("background-image");
if (/^url\(.*\)$/.test(backgroundImageUrl)) {
var path = backgroundImageUrl.substring(4, backgroundImageUrl.length - 1).replace(/\.(?!.*\.)/, settings["retina-suffix"] +".");
preload(path, function(img) {
element.css("background-image", "url(" + img.src + ")");
if (element.css("background-size") == "auto auto") {
element.css("background-size", (img.width / 2) + "px auto");
}
});
}
}
});
}
};
})(jQuery);