相关疑难解决方法(0)

如何使用jQuery将元素动画化为其自然高度

我试图让一个元素动画到它的"自然"高度 - 即它的高度height: auto;.

我想出来了:

var currentHeight = $this.height();
$this.css('height', 'auto');
var height = $this.height();
$this.css('height', currentHeight + 'px');

$this.animate({'height': height});
Run Code Online (Sandbox Code Playgroud)

有一个更好的方法吗?这感觉有点像黑客.

编辑:这是一个完整的脚本,可供想要测试的任何人使用.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html lang="en"> 
    <head>
        <title>jQuery</title>
        <style type="text/css">
            p { overflow: hidden; background-color: red; border: 1px solid black; }
            .closed { height: 1px; }
        </style>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
        <script type="text/javascript">
        $().ready(function()
        {
            $('div').click(function()
            {
                $('p').each(function()
                {
                    var $this = $(this);

                    if ($this.hasClass('closed'))
                    {
                        var currentHeight = $this.height();
                        $this.css('height', 'auto'); …
Run Code Online (Sandbox Code Playgroud)

javascript css jquery

16
推荐指数
3
解决办法
2万
查看次数

标签 统计

css ×1

javascript ×1

jquery ×1