【问题标题】:Dynamically assign height to a div将高度动态分配给 div
【发布时间】:2020-02-29 02:54:51
【问题描述】:

我想这样做:<div class="container" style="height: calc(100vh - 349px);"> 但通过 JavaScript 动态执行,因为“394”会有所不同。

我这样做了:

<script>
        var afooter = document.getElementById('footer').positionInfo.height;
        var acontainer = document.getElementById('container').positionInfo.height;
        var dif = parseInt(acontainer) - parseInt(afooter);
        document.getElementById('contenedor').setAttribute('style','height: calc(100vh -' + dif + '');
</script>

我做错了什么?

【问题讨论】:

  • 看起来您忘记在字符串中添加px);'height: calc(100vh - ' + dif + 'px);'
  • 是的,你是对的。但是还不行=(
  • 你在做什么?这是为了某种粘性页脚吗?
  • 是的,一个粘性页脚

标签: javascript html height


【解决方案1】:

试试这个功能。

function resizeElementHeight(element) {
  var height = 0;
  var body = window.document.body;
  if (window.innerHeight) {
      height = window.innerHeight;
  } else if (body.parentElement.clientHeight) {
      height = body.parentElement.clientHeight;
  } else if (body && body.clientHeight) {
      height = body.clientHeight;
  }
  element.style.height = ((height - element.offsetTop) + "px");
}

【讨论】:

  • 你想分享你的仓库吗?
【解决方案2】:

我找到了一种解决方法,即禁用某些东西而不是不显示它们,因此页脚现在具有固定高度。这样&lt;div class="container" style="height: calc(100vh - 349px);"&gt;现在就可以工作了。

非常感谢大家的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-28
    • 2013-02-28
    • 2015-11-30
    • 1970-01-01
    相关资源
    最近更新 更多