【问题标题】:Setting container height based on container width and multiply by a constant根据容器宽度设置容器高度并乘以常数
【发布时间】:2012-02-26 13:51:41
【问题描述】:

我正在尝试根据容器的宽度乘以 0.75 来设置容器的高度。

我想出了这个,但不确定我哪里出错了,因为它不起作用:

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

有什么建议吗?

谢谢

【问题讨论】:

  • 您忘记了 #slideContent 的选择器上的 $。

标签: jquery css height width equation


【解决方案1】:

现在就试试吧。

 $(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 的数字。 我相信其余部分本身应该很有意义。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2013-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-26
      • 2020-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多