简体   繁体   English

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

[英]jQuery animate div height after dynamic loaded content

I load page via AJAX and its content is represented as a string. 我通过AJAX加载页面,其内容表示为字符串。 After that I want to animate container's height if the content is bigger than actual container's height. 此后,如果内容大于实际容器的高度,我想为容器的高度设置动画。 But, _height is 0. Why ? 但是, _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();
}

Edit: heres a fiddle with a working animation for the container (or other div) of a loaded (simulation) content: 编辑:这是一个带有工作动画的小提琴,用于已加载(模拟)内容的容器(或其他div):

http://jsfiddle.net/sK6LN/ 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>');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM