我有一個圖像/媒體tumblr的第一頁上加載12-25圖像,它有可能是相當大的。如何重新啓動一個功能,如果它不是第一次工作?
我有jQuery做佈局和圖像的各種美容東西,特別是,我檢查每個帖子,看看它是否是一個photoset,它是由Tumblr作爲iframe服務。
從視覺上看,可以看到photoset的縮略圖預覽圖庫,當您單擊縮略圖時,它會打開一張包含全尺寸照片的lightbox幻燈片。
我的tweek等待iframe加載,刪除縮略圖庫並將其替換爲庫中的第一個完整分辨率圖像。
這裏是我的代碼:
$(".post_relative").each(function() {
var post_relative = $(this);
var photoset = post_relative.find('.html_photoset');
if(photoset.length){
var myFrame = photoset.find("iframe");
myFrame.load(function(){
console.log("frame loaded");
var newCover = myFrame.contents().find('.photoset').children(':first-child').children(':first-child');
myFrame.contents().find('img').each(function(){
$(this).hide();
})
newCover.children(':first-child').remove();
var psPre = newCover.attr("href")+ '?' + (Math.random() * 1000000);
newCover.append("<img src='"+ psPre +"' alt='Cover Page' />");
var psHeight = newCover.find('img');
psHeight.load(function(){
if (psHeight.height()>heightRef){
psHeight.height(heightRef);
}
newCover.detach();
post_relative.append(newCover);
myFrame.hide();
})
})
}
無論如何,它的工作原理,時間75%。我只能想象Tumblr有時會超載,或者頁面上可能會出現太多內容?
是什麼導致jQuery不一致?是否有jQuery代碼,允許我仔細檢查代碼是否正確完成?
你可以看到它的工作或沒有在這裏工作: http://syndex.me
如果它不工作,第三個職位將是這樣的:
如果它工作正常,將是這樣的:
我猜測它可能是代碼的一部分。我怎樣才能讓這個更堅實?也許通過埃文的回答? – RGBK
@埃文的答案很好,因爲你還需要iframe加載的錯誤處理。事實上,也許這是你唯一的問題。如果你確定一個框架加載沒有錯誤,並沒有觸發你的處理程序,那麼我的答案更相關。 – Jacob