【发布时间】:2016-08-19 15:33:46
【问题描述】:
我现在在 http://davidpetersson.nu/madartwork/ 工作
我想将帖子中的内容加载到起始页上的 div 中。威奇的作品。有点。
我有一个特定的 div 和一个 click 事件,它从帖子中获取内容并将其 ajax 加载到 div 中。在我加载之前,div 的高度为 0px,然后我测量内容并对高度进行动画处理以匹配内容。
它适用于文本,但是当我尝试加载包含图片的帖子时,有时我测量时图像没有完全加载。
示例。点击“Band 6”
我使用的脚本是这样的:
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;
});
});
我试图从这篇帖子jQuery Ajax wait until all images are loaded 中加入 .imagesLoaded
但我还没有设法让它工作。
(使用 jQuery 而不是 $ 因为 wordpress)
这是我的第一个 ajax-script-tinkerings 之一,所以有些东西可能真的是错误的,所以要温柔 :)
【问题讨论】:
标签: jquery ajax wordpress image-loading