如何为div创造一个逐渐增加的高度

时间:2014-08-17 07:05:56

标签: javascript jquery

当我要求它进行循环时,这是代码没有得到任何高度变化

$(document).ready(function() {  
var increase = [2,4,8,10];

    for (var i=0; i<=increase.length; i++) {

        $('#cir').height(i);
      }
   });

5 个答案:

答案 0 :(得分:3)

改为使用animate()

$('#cir').animate({ height: "+=10" });

请参阅Fiddle

答案 1 :(得分:0)

请解决此问题“未捕获的ReferenceError:$未定义”如果您收到它。如果没有,那么修改你的代码:

$(document).ready(function() {  
var increase = [2,4,8,10];

for (var i=0; i<=increase.length; i++) {

    $('#cir').height(i); // this will always give you 4px because you have been using
  }                      // index i value instead of array's index value. it should be 
});                       // $('#cir').height(increase[i]); or if you want to use animation you can use $('#cir').animate({ height: "+=10" });

答案 2 :(得分:0)

使用以下代码,通过数组中提到的高度来增加div的高度:

$(document).ready(function() {  
    var increase = [2,4,8,10];
    var divctrl = $('#cir');
    var divctrlHeight = divctrl.height();
    for (var i=0; i<=increase.length; i++) {           
       divctrl.height(divctrlHeight + increase[i]);
  }

});

答案 3 :(得分:0)

您可以使用以下代码:

$(document).ready(function() {  
   var increase = [2,4,8,10];

   for (var i=0; i<=increase.length; i++) {

      $('#cir').height($('#cir').height() + increase[i]);
   }
});

答案 4 :(得分:0)

我找到了正确的答案:

    $(document).ready(function(){
    var increase = [20,40,80,100,250]

        for (var i=0; i < 12; i++) {

    $('#cir').animate({height: 250 + increase[i]})
    }
    });

这会根据for循环中提出的数组的值逐渐增加div的大小:)