【发布时间】:2021-06-19 08:56:13
【问题描述】:
我正在使用 Bootstrap 进行布局,并且我一直在尝试在视口到达后将侧边栏固定在滚动上。现在滚动部分工作正常,但问题出现在底部。
到达底部后,侧边栏会移到页脚上方并与它重叠。我已经尝试了至少 4 种 SO 解决方案,但似乎没有任何效果。
简而言之,我一直试图实现的是将侧边栏停止在其主要父容器的末尾,而不是与页脚部分重叠。
这是检查问题的代码笔: https://codepen.io/zakero/pen/yLgBGqq
这是sn-p:
// FIXED SCROLL
const main = $("#main");
const aside = $('#aside-content');
const asideInner = $('#aside-content .aside-content');
const sticky = main.offset().top;
$(window).scroll(() => {
if ($(window).scrollTop() >= sticky) {
$(aside).addClass("aside-fixed")
} else {
$(aside).removeClass("aside-fixed");
}
});
// PREVENT OVERLAP ISSUE
// const topOfFooter = $('.footer-bs').position().top;
// const scrollDistanceFromTopOfWin = $(window).scrollTop() + // $(tableOfContentsInner).height();
// const scrollDistanceFromTopOfFooter = scrollDistanceFromTopOfWin - topOfFooter;
// if (scrollDistanceFromTopOfWin > topOfFooter) {
// $(tableOfContents).css('margin-top', 0 - scrollDistanceFromTopOfFooter - 48);
// } else {
// $(tableOfContents).css('margin-top', 0);
// }
.header-content {
background-color: blue;
height: 800px;
margin: 20px 0;
}
.main-content {
background-color: cyan;
height: 2000px;
margin: 20px 0;
}
.aside-content {
background-color: red;
height: 400px;
margin: 20px 0;
}
.aside-fixed {
position: fixed;
top: 0;
right: 0;
}
footer {
background-color: brown;
height: 400px;
}
<link href="https://stackpath.bootstrapcdn.com/bootswatch/4.5.2/litera/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Test</a>
</nav>
<section class="container">
<div class="row">
<div class="col-md-12">
<div class="header-content">
<h3>header content</h3>
</div>
</div>
</div>
</section>
<section class="container" id="main">
<div class="row">
<div class="col-sm-12 col-md-8">
<div class="main-content">
<h3>main content</h3>
</div>
</div>
<div class="col-sm-12 col-md-4" id="aside-content">
<div class="aside-content">
<h3>aside content</h3>
</div>
</div>
</div>
</section>
<footer>
<h3>footer</h3>
</footer>
【问题讨论】:
标签: javascript html jquery css twitter-bootstrap