动态加载内容后jQuery动画div高度

时间:2013-11-07 14:19:29

标签: javascript jquery ajax

我通过AJAX加载页面,其内容表示为字符串。之后,如果内容大于实际容器的高度,我想要为容器的高度设置动画。但是,_height是0.为什么?

function adjustMainContainerHeight() {
    var _height = $('div.content').wrapInner('<div>').outerHeight();

    $("div.main").animate({ height: _height  }, 600);
}

function prepareResult(data) { //data is HTML string returned by server
    $('div.content').html(data);
    adjustMainContainerHeight();
}

1 个答案:

答案 0 :(得分:1)

编辑:继承了加载(模拟)内容的容器(或其他div)的工作动画的小提琴:

http://jsfiddle.net/sK6LN/

function adjustMainContainerHeight(_height) {

    $("div.main").animate({ height: _height  }, 600);
}

function prepareResult(data) { //data is HTML string returned by server
    $('div.content').html(data);
    adjustMainContainerHeight($('div.content').height());
}

$(function(){
    $('div.main').height(0);
    prepareResult('<p>foo</p><p>bar</p><p>foo</p><p>bar</p>');
});