Skip to content

Instantly share code, notes, and snippets.

@justinobney
Created June 14, 2012 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinobney/2933023 to your computer and use it in GitHub Desktop.
Save justinobney/2933023 to your computer and use it in GitHub Desktop.
Full Screen Background Image
(function (a) {
a.backstretch = function (k, i, l) {
function m(c) {
try {
f = {
left: 0,
top: 0
};
d = e.width();
b = d / j;
if (b >= e.height()) {
g = (b - e.height()) / 2;
h.centeredY && a.extend(f, {
top: "-" + g + "px"
})
} else {
b = e.height();
d = b * j;
g = (d - e.width()) / 2;
h.centeredX && a.extend(f, {
left: "-" + g + "px"
})
}
a("#backstretch img").width(d).height(b).css(f)
} catch (n) {}
typeof c == "function" && c()
}
var h = {
centeredX: true,
centeredY: true,
speed: 0,
opacity: 1
},
e = "onorientationchange" in window ? a(document) : a(window),
j, d, b, g, f;
i && typeof i == "object" && a.extend(h, i);
a(document).ready(function () {
if (k) {
var c = a("<div />").attr("id", "backstretch").css({
left: 0,
top: 0,
position: "fixed",
overflow: "hidden",
zIndex: -9999,
opacity: h.opacity
}),
n = a("<img />").css({
position: "relative",
display: "none"
}).bind("load", function (o) {
var p = a(this);
j = a(o.target).width() / a(o.target).height();
m(function () {
p.fadeIn(h.speed, function () {
typeof l == "function" && l()
})
})
}).appendTo(c);
a("body").prepend(c);
n.attr("src", k);
a(window).resize(m)
}
});
return this
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment