简体   繁体   English

固定位置div,可相对于页脚调整高度

[英]Fixed position div with adjusting heights relative to footer

I am working on a website which has two sidebars and a centre wrapper for the pages content. 我正在一个网站上工作,该网站有两个侧边栏和一个用于页面内容的中心包装。 The side bars are going to have links which will jump the user to the relative part of the webpage. 侧栏将具有链接,这些链接将使用户跳至网页的相对部分。 These sidebars have a fixed position so that it follows the user as they scroll down the page but when the user reaches the footer at the bottom of the page, I would like the sidebar heights to shrink so the footer doesn't block the links in those sidebars. 这些侧边栏具有固定的位置,以便在用户向下滚动页面时跟随用户,但是当用户到达页面底部的页脚时,我希望侧边栏的高度缩小,以便页脚不会阻塞链接这些侧边栏。

It's hard to explain exactly how the page is laid out so I have put my website into jsfiddle in it's basic form, link here . 很难确切解释页面的布局方式,因此我已经将网站以其基本形式放入jsfiddle中,请链接此处

<div class="left">Lorem Ipsum ...</div>
<div class="centre">centre</div>
<div class="right">Lorem Ipsum ...</div>
<div class="footer">footer</div>

I would like to have a 20 pixel margin between the footer (in grey) and the sidebars (in red) at all times, and I haven't figured out how to get them to shrink in relation to the footer coming into the browser window. 我一直希望页脚(灰色)和侧边栏(红色)之间始终有20像素的余量,而且我还没有弄清楚如何使它们相对于进入浏览器窗口的页脚缩小。

Ideally I don't want to use JavaScript and keep the website using HTML and CSS primarily if possible. 理想情况下,如果可能的话,我不想使用JavaScript,并且主要不要使用HTML和CSS。

Thanks in advance. 提前致谢。 Matt 马特

This is how I did it: 这是我的方法:

var spacing = 50;
var defaultHeight = $(".left").outerHeight();

$(window).scroll(function() {

  var calcHeight = $(".footer").offset().top - $(window).scrollTop() - spacing;

  if (calcHeight < defaultHeight) {
    $(".left, .right").height(calcHeight);
  } else {
    $(".left, .right").height(defaultHeight);
  }

});

Here is the JSFiddle demo 这是JSFiddle演示

使用z-index或使用javascript或jquery代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM