I am trying to update my flickr gallery script to instead replace a background image url with the output of the flickr script.
I created a pen here showing my progress.
<div id="banner">
<div class="container">
<header>
<div class="row">
</div>
<h2>Page Title</h2>
</header>
</div>
</div>
var flickrID = '189509301@N03';
var photoCount = '1';
var tags = "'featured','hcdbanner'"; // PHOTO TAGS
var tagmode="all"
if (tags != ''){tags = '&tags=' + tags}
var limit = photoCount-1;
var flickrUrl = 'https://api.flickr.com/services/feeds/photos_public.gne?id=' + flickrID + '&per_page=' + photoCount + '' + tags + '&format=json'
$.ajax({
url: flickrUrl,
dataType: "jsonp",
jsonp: 'jsoncallback',
success: function (data) {
//console.log(data);
var flickrhtml = '';
//Loop Through Each Feed Item
$.each(data.items, function(i, item) {
flickrhtml += "'background:(url"item.media.m.replace('_m', '_b')"';
return i<limit;
});
$('#banner').html(flickrhtml);
}
});
The idea behind it would be for each image on our flickr account we give a certain set of tags, it would then append it as a background image for the #banner tag.
Would anyone be able top provide the proper way to attach the output to the #banner tag?
2
Answers
There are multiple problems in the ajax success function. If you check the console, you’ll see there is a syntax error in the code that creates the background urls. In addition, the code is adding the urls to the banner as html rather than as css.
To fix these problems, modify the string formatting to:
Array.map followed by Array.join is a better way to combine these urls than jQuery.each.
The list of background image urls can then be applied using jQuery css
And here is the final result with minimal changes to your original code:
Simple solution using plain HTML and JS. I’m only using the first image listed in the response.