我假设您刚刚包含了 slimbox JS 文件。它不起作用,因为加载 slimbox 时 DOM 没有图像,因为它们是在之后添加的,并且 slimbox 仅适用于在调用函数时已经存在的链接。
通过对 slimbox.js 进行一些修改,很容易实现您所说的。你会在 slimbox.js 的底部找到这个:
// AUTOLOAD CODE BLOCK (MAY BE CHANGED OR REMOVED)
if (!/android|iphone|ipod|series60|symbian|windows ce|blackberry/i.test(navigator.userAgent)) {
jQuery(function($) {
$("a[rel^='lightbox']").slimbox({/* Put custom options here */}, null, function(el) {
return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
});
});
}
将它包装在一个函数中,并在动态更改 DOM 时调用它(在附加图像之后)。把上面的代码改成这样:
function loadsb(){
if (!/android|iphone|ipod|series60|symbian|windows ce|blackberry/i.test(navigator.userAgent)) {
$("a[rel^='lightbox']").slimbox({/* Put custom options here */}, null, function(el) {
return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
});
} }
loadsb()
之后调用appendTo('#FBalbum');
编辑:既然你有麻烦,让我让它变得更容易。
将此用作您的:http slimbox.js
: //pastebin.com/8iye0PEB。我已经从该文件中删除了函数声明,将其添加到主页上的 JS 中。
现在我从你包含的 JS 中看到,Jquery 对象没有被 jQuery 引用$
,它被 jQuery 引用。所以现在你的主页 JS 将是:
<script>
//Function to load SlimBox. PS: I'm refrencing the jQuery object by "jQuery" and not $ sign because of your markup
function loadsb(){
if (!/android|iphone|ipod|series60|symbian|windows ce|blackberry/i.test(navigator.userAgent)) {
jQuery("a[rel^='lightbox']").slimbox({/* Put custom options here */}, null, function(el) {
return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
});
} }
jQuery(document).ready(function() {
var AlbumID = "163187497033669";
var graph = "https://graph.facebook.com/" + AlbumID + "/photos?callback=?";
jQuery.getJSON(graph, function(data) {
var albumItem = [];
for(var key in data){
for(var key2 in data[key]){
val2=data[key][key2];
if(typeof(val2.source)!="undefined"){
albumItem.push(
'<li><a class="imageLink" rel="lightbox" href="' + val2.source + '" ><img src="' + val2.picture + '"/></a></li>'
);
};
};
};
jQuery('<ul />', {
'class': 'album',
html: albumItem.join('')
}).appendTo('#FBalbum');
//Let's now call the function we created above
loadsb();
//Slimbox is now loaded.
});
});
</script>
另外,请注意:现在我们必须手动加载 slimbox,仅将 JS 文件包含在页面中是行不通的。每当您想加载 SB 时,您都必须使用函数声明和调用。(或者只是将函数声明移动到 slimbox.js 文件并在该文件本身中调用它一次。)