简体   繁体   English

填充时的jQuery动画div

[英]jQuery animate div when filled

Here is a very simple example of what I have: http://jsfiddle.net/MxKLU/ 这是我所拥有的非常简单的示例: http : //jsfiddle.net/MxKLU/

Click on the links space-one and space-two and the contents within the div space will change. 单击space-onespace-two的链接,div space的内容将更改。 This will cause a jaring seen by the red background, when the contents gets larger and smaller than its predecessor. 当内容变得比其前任更大或更小时,这将导致在红色背景下看到震颤。 I am looking for a way to animate the height of the div space so that this jaring does not occur. 我正在寻找一种动画div space的高度的方法,以免发生这种震颤。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Hope it helps you. 希望对您有帮助。

$('a[id^="space"]').click(function(){
      $('div#space div').hide();
      $('#space, #'+$(this).text()).slideDown();
 });

Demo: http://jsfiddle.net/MxKLU/2/ 演示: http//jsfiddle.net/MxKLU/2/

My proposition: http://jsfiddle.net/dxvEr/3/ 我的主张: http : //jsfiddle.net/dxvEr/3/

it does not need to change height to 0 in the middle of the process. 它不需要在过程的中间将高度更改为0。

I've added another div. 我添加了另一个div。 Outer div has overflow:hidden and static height so it wouln't react to change in its content, but we check inner div height and use animation to set outer div to the same height. 外层div有溢出:隐藏高度和静态高度,因此不会对内容的变化做出反应,但是我们检查内层div的高度,并使用动画将外层div设置为相同的高度。

I'd do it like this 我会这样

$("#space").css("height",$(".selected").height());

$('#space-one-appear').click(function(){
    $('#space-one').attr('class','selected');
    $('#space-two').attr('class','unselected');
    $("#space").animate({height:$(".selected").height()}, 500);
});

$('#space-two-appear').click(function(){
    $('#space-two').attr('class','selected');
    $('#space-one').attr('class','unselected');
    $("#space").animate({height:$(".selected").height()}, 500);
});

jsfiddle -> http://jsfiddle.net/MxKLU/5/ jsfiddle-> http://jsfiddle.net/MxKLU/5/

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

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