使用div高度为marginTop

时间:2013-04-15 14:37:56

标签: jquery variables height

我需要制作一个简单的动画,但问题是它们没有设置内容块的高度(取决于其中的文本数量)。所以我想我需要知道内容块的高度并将该数字放在marginTop中。但是当你想通时,我不知道该怎么做。

$('nav').hide().fadeIn(1200, function(){
    $('#content_home').animate({
    marginTop: '-50px'},1000);    
});

这是我用于动画的jQuery代码。所以我需要#content_home的高度并将其放在marginTop值中。

我想我需要编写一个计算高度并将其放入变量的函数,并将该变量放入marginTop值。

4 个答案:

答案 0 :(得分:1)

var result = $("#content_home").height();

您还可以使用其他功能,例如.innerHeight().outerHeight(),这些功能都适用于您。

答案 1 :(得分:1)

您是否尝试过.height()

$('nav').hide().fadeIn(1200, function(){
    var contentHomeHeight = $('#content_home').height();
    $('#content_home').animate({
    marginTop: contentHomeHeight },1000);    
});

答案 2 :(得分:1)

我想你回答了自己的问题:

  

我想我需要编写一个计算高度并将其放入变量的函数,并将该变量放入marginTop值。

你可以通过使用.height()函数来计算某事物的高度:

$('nav').hide().fadeIn(1200, function(){
    var height = ($('#contentblock').height() * -1);

    $('#content_home').animate({
    marginTop: height},1000);    
});

答案 3 :(得分:1)

你不能只在jQuery $('#content_home')元素上使用.height()方法吗?