使用jQuery将元素动画到自动高度 [英] Animate element to auto height with jQuery

查看:75
本文介绍了使用jQuery将元素动画到自动高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将< div> 200px 改变为 auto code>高度。我似乎无法让它工作。有人知道如何做到这一点吗?

I want to animate a <div> from 200px to auto height. I can’t seem to make it work though. Does anyone know how?

以下是代码:

Here’s the code:

$("div:first").click(function(){
  $("#first").animate({
    height: "auto"
  }, 1000 );
});


推荐答案


  1. 保存当前身高:

  1. Save the current height:

var curHeight = $('#first').height();


  • 暂时将高度切换为自动:

  • Temporarily switch the height to auto:

    $('#first').css('height', 'auto');
    


  • 获取汽车高度:

  • Get the auto height:

    var autoHeight = $('#first').height();
    


  • 切换回 curHeight 和animate to autoHeight

  • Switch back to curHeight and animate to autoHeight:

    $('#first').height(curHeight).animate({height: autoHeight}, 1000);
    


  • 和在一起:

    var el = $('#first'),
        curHeight = el.height(),
        autoHeight = el.css('height', 'auto').height();
    el.height(curHeight).animate({height: autoHeight}, 1000);
    

    这篇关于使用jQuery将元素动画到自动高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆