2013-03-15 56 views
2

我做了一个div宽度100%和高度宽度* .1在jquery中。准确的数字。高度

我设法扩大高度,尽管比例但我无法设法扩大它准确。

我其实想把它扩大到400px,可能吗?

这里是我的代码:http://jsfiddle.net/2drYk/3/

* 
{ 
margin:0; 
padding:0; 
border:0 
} 

.container 
{ 
    width:100%; 
    background:black; 

} 

<figure class="container"> 
</figure> 

$('.container').height($(this).width()*0.1); 
    var toggleCheck =0; 
$('.container').click(function(){ 

    if(toggleCheck ==1){ 
     $(this).stop().animate({height: ($(this).height()-$(this).width()*0.1)}); 
     toggleCheck = 0; 
    }else{ 
$(this).stop().animate({height: ($(this).width()*0.1 + $(this).height())}); 
     toggleCheck = 1; 
    } 
}); 
+1

我不明白:如果要扩大到400像素,为什么烦心事服用计算?可能我不明白你的问题? – Sunyatasattva

+0

是[this](http://jsfiddle.net/2drYk/6/)你想要什么? – pktangyue

回答

4

LIVE DEMO

var $cont = $('.container'), 
    init_size = $cont.width() * 0.1, 
    c = 0,    // counter 
    s = [400,init_size]; // sizes array 

$cont.height(init_size); 

$cont.click(function(){ 
    $(this).stop().animate({ height: s[c++%2] }); 
}); 
+0

使用'animate'很好,很好的演示。对于非数学类型,请解释'C++%2'来替代高度值。 –

+0

+1优雅的解决方案。 s [C++%2]是黄金。 –

+1

'C++'后增加'c'(返回原始值,然后递增)。 '%2'取这个值并将其限制为值'0'和'1'。该值用作从数组's'访问的索引。从's'返回的值被分配给'height'。一举一举。 '%'被称为[模数(或余数)运算符](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Arithmetic_Operators) –

0
$(this).stop().animate({height: 400}); 
1

,如果你只是想通过400像素成长的容器,你可以使用下面的JS:

var container = $('.container'); 
container.height(container.width()*0.1); 
var originalHeight = container.height(); 
var newHeight = 400 + originalHeight; 

container.click(function(){ 
    if($(this).height() == originalHeight){ 
    $(this).stop().animate({height: newHeight}); 
    }else{ 
    $(this).stop().animate({height: originalHeight}); 
    } 
}); 

http://jsfiddle.net/2drYk/16/