简体   繁体   English

jQuery动画到容器的高度

[英]jQuery animate to height of container

I know a few solutions to this but not the one I want. 我知道一些解决方案,但我不知道。

I want the height of the animation to be the height of the container when the text fades in. 我希望动画的高度是文本淡入时容器的高度。

This is what I've tried so far: fiddle: https://jsfiddle.net/jzhang172/rxbnnvdf/1/ 到目前为止,这是我尝试过的:提琴: https ://jsfiddle.net/jzhang172/rxbnnvdf/1/

 $(function(){ $('.box').click(function(){ var heightbox = $(this).innerHeight(); var heightparagraph=$(this).find('p').innerHeight(); var trueheight=heightbox+heightparagraph; console.log(heightbox); console.log(heightparagraph); console.log(trueheight); $(this).animate({height: trueheight}, {duration:500}); $(this).find('p').fadeIn(1000); }); }); 
 .box{ width:100%; height:50px; border-bottom:1px solid black; cursor:pointer; overflow:hidden; } .box h2{ margin:0px; padding-top:25px; } body,html{ padding:0; margin:0; } .box p{ display:none; } *{ box-sizing:border-box; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="box one"> <h2> Box One </h2> <p class="info"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean vitae tortor id massa tempus ultricies in a diam. Aliquam consectetur dui arcu, non volutpat mauris tempor eget. Donec et nibh ornare, pretium lorem nec, sagittis dui. Donec efficitur pulvinar mauris eget fringilla. Donec vulputate lectus suscipit mauris egestas mollis. Donec at risus dolor. Sed tincidunt, enim nec pharetra condimentum, massa eros condimentum nisi, ac ullamcorper erat lacus et urna. Proin urna sapien, convallis vel tellus vitae, vulputate congue mi. </p> </div> <div class="box two"> <h2> Box Two </h2> <p class="info"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean vitae tortor id massa tempus ultricies in a diam. Aliquam consectetur dui arcu, non volutpat mauris tempor eget. Donec et nibh ornare, pretium lorem nec, sagittis dui. </p> </div> 

You were missing the margin. 您错过了保证金。 You need this: 你需要这个:

var heightbox = $(this).find('h2').outerHeight(); // because otherwise the box will keep on increasing in height with every click.
var heightparagraph=$(this).find('p').outerHeight(true); // true argument includes the margin of the element to the height.

Fiddle 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM