我想对 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/