Created
July 29, 2015 17:28
-
-
Save EvanLovely/2896acbec1def61ae6be to your computer and use it in GitHub Desktop.
Turn img tags to background images
This file contains hidden or 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
$('.js-bg-to-img').each(function () { | |
$(this).find('img').each(function () { | |
var $img = $(this); | |
var src = ''; | |
var $wrapMe; | |
src = $img.attr('src'); | |
$wrapMe = $img; | |
if (src.length) { | |
// we have a value | |
if ($wrapMe.parent().hasClass('bg-img') === false) { | |
// first run | |
$wrapMe | |
.addClass('hide') | |
.wrap('<div></div>') | |
.parent() | |
.attr('style', 'background-image: url(' + src + ');') | |
.addClass('bg-img bg-img--expand'); | |
} | |
} else { | |
// error; no `src` found | |
console.error("img src wasn't found, so it wasn't turned into a background image.", $img); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment