jquery - 我如何在 jquery 中将特定高度设置为 100%

标签 jquery css height

我想对 div 中的内容进行一点“预览”,所以我把它做得很小,只显示第一行。现在我想为它设置动画,当您单击标题时,div 将具有 100% 的高度,但是当我使用 animate(height:"100%") 时,滑动效果不佳。

<a href="javascript:void(0);" class="show_it">title:</a>
<div>
    content<br>
    content_hidden
</div>

<script>
$('.show_it').click(function() {
  $(this).next("div").animate({
    height: 'auto'
  }, 1000, function() {
  });
});
</script>

<style>
div{
    height:20px;
    overflow:hidden;
}
</style>

最佳答案

虽然我确信有更好的方法来做到这一点,因为这种方法最好是草率的,但如果您找不到其他解决方案,您总是可以尝试这个:

$('.show_it').click(function() {

// Animate to 100% in 1 millisecond
$(this).parent().next("div").animate({height: 'auto'}, 1);

// Record the height of the div
var newHeight = $(this).parent().next("div").height();

// Animate height to 0% in 1 millisecond
$(this).parent().next("div").animate({height: '0px'}, 1);

// Do the animation using the new fixed height.
  $(this).parent().next("div").animate({
    height: newHeight,
  }, 1000, function() {
  });
});

但是,正如我所说,这非常草率 - 如果它符合您的需要,请尝试 jquery slideDown();这是一种更好的方法。

关于jquery - 我如何在 jquery 中将特定高度设置为 100%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6877552/

相关文章:

javascript - 只显示固定元素上方的背景图像/颜色

css - 如何在 react js 代码中动态更改 css 选择器属性值?

jquery - 形成编辑模式显示在错误的位置

WordPress 网站上的 Javascript 错误

javascript - 如何强制文本数组中的空格?

css - 带有自定义控件的 HTML5 视频

css - 我无法在我的 wordpress 网站上使用@font-face 支持

html - 100% 背景高度不起作用(尽管 html 和正文高度为 100%)

iOS - 带有 UITextView 的 TableView 单元格未正确调整大小

html - 如何制作全高? (尝试了所有选项)