2012-02-04 116 views
1

我试图根据其宽度乘以0.75来设置容器的高度。根据容器宽度设置容器高度并乘以常数

我已经想出了这一点,但不知道在那里我已经错了,因为它不工作:

$('#slideContent').load(function() { 
      var wide = ('#slideContent').width() 
      $(this).css('height', $(this).width(wide * 0.75)); 
     }); 

有什么建议?

谢谢

+0

您忘记了'#slideContent'选择器上的$。 – Krazer 2012-02-04 00:34:17

回答

5

现在试试。

$(document).ready(function() { 
       var wide = $('#slideContent').css('width'); 
       var calculate = parseInt(wide, 10)* 0.75; 

       $('#slideContent').css('height', calculate); 
      }); 

首先我把元素的宽度存储在“宽”变量中。 其次我做一个计算变量。当从CSS样式元素中请求宽度或高度时,您将返回一个包含“px”的字符串的值,以滤除此“px”,并使用“parseInt()”函数作为整数。 “,10”确保它是基数为10的数字。 其余的应该是我自己相信很多的意义。

+0

太棒了,这就是为了让我成为仪式的途径 – Ichimonban 2012-02-04 00:41:54

1

如果你想改变高度,为什么不使用$(this).height(modWidth),其中var modWidth = wide * 0.75改变高度?

相关问题