Background images have some enviable CSS properties that I'd like to use for photo galleries -- namely, background-size: cover; and background-position: center center; let you use an image of any aspect ratio and fit it attractively to a fixed-size container.
However, I don't really think setting background images with content is ideal. I'd much rather have <a href="url.jpg"><img src="url.jpg"/></a> in content than <a href="url.jpg" style="background-image: url(url.jpg);"><img src="url.jpg"/></a>. I think a script to take the former and write the latter is ideal.
Given code that looks like this:
<a href="http://thesundaybe.st/media/from-earth-phone-23.jpg"></a>
<a href="http://thesundaybe.st/media/from-earth-phone-24.jpg"></a>
<a href="http://thesundaybe.st/media/from-earth-phone-26.jpg"></a>
<a href="http://thesundaybe.st/media/from-earth-phone-27.jpg"></a>
<a href="http://thesundaybe.st/media/from-earth-phone-28.jpg"></a>
<a href="http://thesundaybe.st/media/from-earth-phone-29.jpg"></a>
<a href="http://thesundaybe.st/media/from-earth-phone-30.jpg"></a>
I'm trying to write a JQuery script to take the href URL and set it as the background image. Basically, turning the above code into this:
<a href="http://thesundaybe.st/media/from-earth-phone-23.jpg" style="background-image: url(http://thesundaybe.st/media/from-earth-phone-23.jpg);"></a>
<a href="http://thesundaybe.st/media/from-earth-phone-24.jpg" style="background-image: url(http://thesundaybe.st/media/from-earth-phone-24.jpg);"></a>
<a href="http://thesundaybe.st/media/from-earth-phone-26.jpg" style="background-image: url(http://thesundaybe.st/media/from-earth-phone-26.jpg);"></a>
<a href="http://thesundaybe.st/media/from-earth-phone-27.jpg" style="background-image: url(http://thesundaybe.st/media/from-earth-phone-27.jpg);"></a>
<a href="http://thesundaybe.st/media/from-earth-phone-28.jpg" style="background-image: url(http://thesundaybe.st/media/from-earth-phone-28.jpg);"></a>
<a href="http://thesundaybe.st/media/from-earth-phone-29.jpg" style="background-image: url(http://thesundaybe.st/media/from-earth-phone-29.jpg);"></a>
<a href="http://thesundaybe.st/media/from-earth-phone-30.jpg" style="background-image: url(http://thesundaybe.st/media/from-earth-phone-30.jpg);"></a>
Here's a jsfiddle with the basic HTML/CSS set up, just needing a bit of JQuery: http://jsfiddle.net/MRSallee/tv3q8/3/
And theoretically, this is what it'd look like when the script does the trick: http://jsfiddle.net/MRSallee/tv3q8/
I got fairly close this this bit of JQuery, but it seems to just use the first href as the background image for all of the <a> tags.
$('a').css({'background-image': 'url(' + $('a').attr('href') + ')'});
Any help is greatly appreciated. Cheers.