【发布时间】:2020-10-10 13:32:35
【问题描述】:
我有一个固定定位的导航栏和页脚,它们的高度取决于窗口大小,因此我为每个导航栏和页脚制作了一个 div,并使其高度与导航栏和页脚相同,这样主区域就不需要重叠。当我首先加载页面时它工作,当我调整窗口大小时它工作。但是如果我从 index.html.erb 到 show.hteml.erb,或者做一些分页,那些 div 的高度变成 0。有什么办法可以解决这个问题?
我的 _navbar.html.erb 如下
<nav class="fixed-top navbar navbar-expand-lg navbar-dark" style="background-color: #202020;" id="nav-id">
...
</nav>
<div id="nav-margin">
</div>
我的 application.js 如下
$(document).on('turbolinks:load', function() {
window.addEventListener('load', () => {
this.getElementById('nav-margin').setAttribute("style", `height:${this.getElementById('nav-id').offsetHeight}px`);
this.getElementById('footer-margin').setAttribute("style", `height:${this.getElementById('footer-id').offsetHeight}px`);
});
window.addEventListener('resize', () => {
this.getElementById('nav-margin').setAttribute("style", `height:${this.getElementById('nav-id').offsetHeight}px`);
this.getElementById('footer-margin').setAttribute("style", `height:${this.getElementById('footer-id').offsetHeight}px`);
});
});
【问题讨论】:
标签: javascript jquery ruby-on-rails