在vue js中设置div的高度

新手上路,请多包涵

我正在使用 vue js。我想使用纯 javascript 根据另一个 div 高度设置一个 div。我面临的问题是我无法使用纯 java 脚本设置高度。但我可以使用 jquery 设置它。谁能帮我把这个 jquery 改成 javascript 。给出了我正在使用的代码

 Vue.nextTick(function () {
            var offsetHeight = document.getElementById('filterSection').offsetHeight;
            $(".searchResultSection").css("max-height",`calc(100% - ${offsetHeight}px)`);
           });

我需要将 jquery 部分更改为 java 脚本。

原文由 mr. Done 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 974
1 个回答

事实上, computed properties ( https://v2.vuejs.org/v2/guide/computed.html#Computed-Properties ) 是解决您的问题的完美选择:

声明一个将返回 filterSectionHeight 的计算属性

export default {
  name: "App",

  computed: {
    filterSectionHeight() {
      const filterSectionDOM = document.getElementById("filterSection");
      return filterSectionDOM ? filterSectionDOM.offsetHeight : 0;
    },
  }
};

Define your div filterSection and searchResultsSection in your component (or App component), don’t forget to add a :style attribute who handle the dynamic max-height 在您的模板中提供给 .searchResultsSection

 <div id="filterSection"></div>
<div class="searchResultsSection"
     :style="{
            'max-height': `calc(100% - ${filterSectionHeight}px)`
     }">
</div>

在你的 CSS 中将 每个 div 的高度设置为 100%

 #app {
  height: 600px;
  width: 100%;
}

#filterSection {
  height: 100%;
  background: rebeccapurple; //https://en.wikipedia.org/wiki/Eric_A._Meyer
}

.searchResultsSection{
  height: 100%;
  background: rebeccapurple;
  opacity: 0.6;
}

您将在此处找到完整的演示 > https://codesandbox.io/s/1rkmwo1wq

原文由 Thomas Brd 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题