Jquery Flickr API随机div背景
我想用FlickrAPI随机拍摄一张带有“风景”标签的照片。随机照片链接似乎是通过编程方式添加到ChromeDevTools中的html on inspect元素中的。但是没有照片。我做错了什么Jquery Flickr API随机div背景,jquery,ajax,json,Jquery,Ajax,Json,我想用FlickrAPI随机拍摄一张带有“风景”标签的照片。随机照片链接似乎是通过编程方式添加到ChromeDevTools中的html on inspect元素中的。但是没有照片。我做错了什么 $(document).ready(function() { $.getJSON("https://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?", {
$(document).ready(function() {
$.getJSON("https://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?",
{
tags: "landscape",
format: "json"
},
//The callback function
function(data) {
//Get random photo from the api's items array
var randomPhoto = data.items[Math.floor(Math.random() * data.items.length)];
$(".portret").css({
position: "relative",
height: "100vh",
//Use the randomPhoto's link
backgroundImage: "url("+randomPhoto.link+")",
backgroundPosition: "center",
backgroundRepeat: "no-repeat",
backgroundSize: "cover"
});
}
);
});
您必须使用
媒体
对象来设置背景图像
您使用的“链接”属性不正确,这是Flickr照片详细信息的链接。您只需要图像的url
试试看:
$.getJSON("https://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?",
{
tags: "landscape",
format: "json"
},
//The callback function
function(data) {
//Get random photo from the api's items array
var randomPhoto = data.items[Math.floor(Math.random() * data.items.length)];
$(".portret").css({
position: "relative",
height: "100vh",
//Use the randomPhoto's link
backgroundImage: "url("+randomPhoto.media.m+")",
backgroundPosition: "center",
backgroundRepeat: "no-repeat",
backgroundSize: "cover"
});
}
);