Skip to content

Instantly share code, notes, and snippets.

@ryanburnette
Last active August 29, 2015 14:10
Show Gist options
  • Save ryanburnette/144423f81b19aa1bd11b to your computer and use it in GitHub Desktop.
Save ryanburnette/144423f81b19aa1bd11b to your computer and use it in GitHub Desktop.
/* global Modernizr */
var fixNoBackgroundSize = function () {
var bg2img
;
bg2img = function ($el) {
var bg
, classes
, $r
;
$el = $el.eq(0); // meant to work with one element at a time
classes = $el.attr('class').split(' ');
classes.push('bg2img');
$r = $('<img>');
console.log(classes);
classes.forEach(function (cl) {
$r.addClass(cl);
});
bg = $el.css('background-image').match(/\((.*?)\)/)[1].replace(/('|")/g,'');
$r.attr('src', bg);
$r.css({
width: '100%'
, background: 'none'
});
return $el.replaceWith($r);
};
if ( !Modernizr.backgroundsize ) {
$('*').each(function () {
if ( $(this).css('background-size') !== 'auto' ) {
bg2img($(this));
}
});
}
};
@ryanburnette
Copy link
Author

Here I was toying around with this idea of looking for images with background-size set and replacing them with an img element. It's not a viable production solution because there are too many ways to break.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment