将facebook专辑嵌入网站 [英] embed facebook albums into website
本文介绍了将facebook专辑嵌入网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用graph api来获取相册,我将所有相册都放到我的网站中,但是当我刷新页面时相册的位置会发生变化。为什么会这样?
I have used graph api to fetch the albums, I got all the albums into my website but the positions of the albums changing when I refresh the page. why it should happening?
我在这里附上我的脚本。
I am attaching my script here.
<script>
$(document).ready(function() {
var albumIdsUrl = "https://graph.facebook.com/<myname>/albums?callback=?";
$.getJSON(albumIdsUrl, function(data) {
var len = data.data.length;
for(var i=0;i<len;i++){
var aid = data.data[i].id;
getAlbumCoverPhoto(data.data[i].cover_photo, data.data[i].id, data.data[i].name, data.data[i].count);
}
});
});
function getAlbumCoverPhoto(coverPhoto, albumId, albumName, count) {
var coverPhotoUrl = "https://graph.facebook.com/" + coverPhoto + "?callback=?";
$.getJSON(coverPhotoUrl, function(coverPhotoData) {
if(typeof(coverPhotoData.picture)!="undefined"){
htmlData = '<li><figure><a class="imageLink" href="fb_album_photos.html?id='+ albumId + '"><img src="' + coverPhotoData.picture + '" /></a></figure><figcaption>'+albumName+'</br>'+count+' Photos</figcaption></li>';
$('#FBalbum').append(htmlData);
}
});
}
</script>
推荐答案
返回的数据并不总是相同如果你想要一致的结果,它需要排序。
The data being returned isn't always in the same order so it needs sorted if you want consistent results.
这似乎运作良好(但可以清理一下):
This seems to work well(but it could be cleaned up a bit):
$.getJSON(albumIdsUrl, function(data) {
var len = data.data.length;
data.data.sort(function(a, b)
{
if (a.id == b.id) return 0;
if (a.id < b.id)
return -1;
else
return 1;
});
for(var i=0;i<len;i++){
var aid = data.data[i].id;
getAlbumCoverPhoto(data.data[i].cover_photo, data.data[i].id, data.data[i].name, data.data[i].count);
}
});
这篇关于将facebook专辑嵌入网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文