【发布时间】:2011-06-01 21:36:24
【问题描述】:
我在 jQuery 和我正在构建的自定义照片库方面遇到了很大的问题。我搜索了高低,并尝试了无数不同的解决方案,但没有一个能完美运行。所以让我介绍一下信息:
此照片库左侧有缩略图,中间有一张大图。每个缩略图都可以被点击,它调用一个传入其 ID 的函数。我想要它做的是,AJAX POST 到服务器以获取图像评论和图像名称。淡出当前大图div,切换img标签中的src,获取新图片的宽度,设置叠加评论div的宽度,淡入大图div。
问题是我无法获得新图像加载后的宽度(即使使用 .load() 的形式等待图像完成加载)。现在,当我淡入新图像然后获取宽度时,我可以让它工作,但这不是我需要的。我需要在淡入之前获取宽度并设置 div 的宽度。
任何想法或更正都会很棒,希望我提供了足够的信息。
这是我正在努力的代码:
function thumbClick(val) {
$.post('ajax.php', {
photo_in: val
}, function (data) {
$('div#picture').fadeOut('slow', function () {
//$('img#big-picture').load(function(){
$('img#big-picture').one('load', function () {
//alert($('img#big-picture').width());
//will alert 0
$('div#picture').fadeIn('slow', function () {
$('div#comment-box').width($('img#big-picture').width());
//set comment to what I want
$('p#comment').html("");
//alert($('img#big-picture').width());
//will alert new image width after FadeIN
$('p#comment').html(data.comment);
});
}); //end of one(load)
$('img#big-picture').attr("src", data.newImage);
}); //end of fadeout
}, "json"); //end of post
}
【问题讨论】:
标签: javascript jquery css