Last active
August 29, 2015 14:10
-
-
Save ryanburnette/144423f81b19aa1bd11b to your computer and use it in GitHub Desktop.
This file contains 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
/* 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)); | |
} | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.