由于fadeOut()通过隐藏目标元素完成,所以你的#main将完全隐藏,因为你的新数据加载完毕后,呈现高度不可见的,因此没有意义的任何动画。
但是你可以只使用类似$('#main').show(400)
将从一个大小为(0,0),动画元素和不透明度为0到任何大小由所允许的容器和内容以及1的完全可见不透明度(并行运行这些动画,使它们都可见)。但是假设你关心高度的动画比你关于衰落的做的更多,你仍然有一个问题:在load()调用它的回调的时候,目标元素的高度已经为为内容的高度(或尽可能接近它)。所以动画不会做任何事情。
I posted a plugin on a previous question,会做你想要什么,但你需要使用$.get(),而不是load():
$.get(url, function(data) {
$('#main').showHtml(data);
});
...其中showHtml被定义为:
// Animates the dimensional changes resulting from altering element contents
// Usage examples:
// $("#myElement").showHtml("new HTML contents");
// $("div").showHtml("new HTML contents", 400);
// $(".className").showHtml("new HTML contents", 400,
// function() {/* on completion */});
(function($)
{
$.fn.showHtml = function(html, speed, callback)
{
return this.each(function()
{
// The element to be modified
var el = $(this);
// Preserve the original values of width and height - they'll need
// to be modified during the animation, but can be restored once
// the animation has completed.
var finish = {width: this.style.width, height: this.style.height};
// The original width and height represented as pixel values.
// These will only be the same as `finish` if this element had its
// dimensions specified explicitly and in pixels. Of course, if that
// was done then this entire routine is pointless, as the dimensions
// won't change when the content is changed.
var cur = {width: el.width()+'px', height: el.height()+'px'};
// Modify the element's contents. Element will resize.
el.html(html);
// Capture the final dimensions of the element
// (with initial style settings still in effect)
var next = {width: el.width()+'px', height: el.height()+'px'};
el .css(cur) // restore initial dimensions
.animate(next, speed, function() // animate to final dimensions
{
el.css(finish); // restore initial style settings
if ($.isFunction(callback)) callback();
});
});
};
})(jQuery);
这工作很好,非常感谢你:) – 2010-09-10 16:20:59