侧边栏在距离顶部 400 像素后变得固定。因此我使用在堆栈上找到的代码:
<div id="gettop"></div>
<script type="text/javascript">
jQuery(function($) {
function fixDiv() {
var $cache = $('#gettop');
if ($(window).scrollTop() > 400)
$cache.css({
'position': 'fixed',
'top': '8px',
'margin-left': '20px',
'border': '1px solid #ccc'
});
else
$cache.css({
'position': 'relative',
'top': 'auto'
});
}
$(window).scroll(fixDiv);
fixDiv();
});
该代码效果很好。但侧边栏与页脚重叠。我怎样才能防止这种情况发生?
我尝试了一些在这里找到的代码,但没有成功。
Fixed sidebar on the scroll stop at div
我很确定,我在混合代码时犯了一个错误。
感谢您的帮助。
最佳答案
尝试为您想要位于顶部的元素提供更大的 z-index。
关于javascript - 将固定侧边栏停止在页脚处 - 防止重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37167881/