林currentyl上http://davidpetersson.nu/madartwork/阿賈克斯負載犯規等待IMG加載當我試圖計算高度
工作,我想從帖子的東西加載到我的起始頁一個div。很有用。均田。
我有一個特定的div和一個點擊事件,從帖子和ajax中獲取內容 - 將它加載到div中。 div的高度爲0px直到我加載,然後我測量的內容和動畫的高度,以配合內容。
它適用於文本,但是當我嘗試加載包含圖片的帖子時,有時圖片在我測量時尚未完全加載。
例子。點擊「六級」
我用的腳本是這樣的:
jQuery(document).ready(function(){
var $mydiv = jQuery('#single-post-container');
$mydiv.css('height', $mydiv.height());
jQuery.ajaxSetup({cache:false});
jQuery(window).resize(function() {
jQuery("#single-post-container").wrapInner('<div/>');
var newheight = jQuery('div:first',"#single-post-container").height();
jQuery("#single-post-container").css({height: newheight});
});
jQuery(".view-details").click(function(event123){
event123.preventDefault();
var post_id = jQuery(this).attr("href");
jQuery("#single-post-container").load(post_id + " .post", function(){
jQuery('#single-post-container').wrapInner('<div/>');
var newheight = jQuery('div:first','#single-post-container').height();
//Does sometimes not include the height of the images, since they havnt completely loaded
jQuery('#single-post-container').animate({height: newheight});
jQuery('html, body').animate({
scrollTop: jQuery('body').offset().top
}, 800);
});
return false;
});
});
我試圖inlcude從這個帖子jQuery Ajax wait until all images are loaded
.imagesLoaded但我還沒有設法得到它的工作。
(jQuery的,而不是$的使用,因爲WordPress的)
這是我的第一個AJAX腳本,tinkerings之一,所以東西可能是真的錯了,所以,請溫柔:)
真棒!不太清楚計數是如何工作的,但如果內容中根本不包含img,是否可以修改if語句來運行? (就像頁面上的其他樂隊一樣) –
是的。這個怎麼樣?編輯我的答案。 @DavidPetersson – eisbehr