如何增加现有高度的高度?

wha*_*ore 14 javascript css jquery

例如,我想添加20px<div id='example'></div>目前的20px.

我可以获得现有的高度并添加20px并输入它作为新的高度,但我希望了解是否有更好的方式可能像+=操作员一样工作.

and*_*lrc 24

有很多方法可以做到:http://jsperf.com/jquery-height-vs-css-height

jsbin.com/utaduy

$('#example').css( "height", "+=20px" );
Run Code Online (Sandbox Code Playgroud)
$('#example').height( $("#example").height() + 20 );
Run Code Online (Sandbox Code Playgroud)


Jas*_*per 22

您可以将一个函数传递给height()具有元素当前高度作为参数return的函数,函数的函数将是新的高度:

$('#example').height(function (index, height) {
    return (height + 20);
});
Run Code Online (Sandbox Code Playgroud)

这是一个演示:http://jsfiddle.net/grajh/

文档:http://api.jquery.com/height/