nav - 停止导航/侧边栏滚动
问题描述
我希望导航栏在接近底部时停止滚动,这样我仍然可以看到页脚。我仍在学习其中的一些东西,我确信有更好的方法来做到这一点,但这就是我现在所拥有的。
$(window).scroll(function() {
if ($(window).scrollTop() >= 1000) {
$('#sticky').addClass('sticky');
}
else {
$('#sticky').removeClass('sticky');
}
});
这是CSS:
.sticky {
position: fixed;
top: 0;
width: 292.5px;
background-color: #fff;
box-shadow: 0px 0px 80px 0px rgba(0, 0, 0, 0.15);
z-index: 9;
padding-top: 0;
padding-bottom: 0;
}
HTML 在此链接中。(抱歉混淆) http://xmjvn.mdnyd.servertrust.com/
解决方案
尝试这个:
$(window).scroll(function() {
const totalHeight = $(document).height();
const windowHeight = $(window).height();
const navHeight = $('#nav').height();
const footerHeight = $('#footer').height();
if (($(window).scrollTop() + navHeight) < (totalHeight - footerHeight)) {
$('#nav').addClass('sticky');
} else {
$('#nav').removeClass('sticky');
}
});
body {
margin-top: 100px;
}
#nav {
height: 100px;
background: yellow;
position: absolute;
left: 0;
top: 0;
width: 100%;
}
#nav.sticky {
position: fixed;
width: 100%;
}
#footer {
margin-top: 1500px;
background: tomato;
height: 600px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="nav">nav</div>
<div id="footer">footer</div>
推荐阅读
- python - PYTHON 安装在 PC 上,(有 PYTHON 图标)没有 LINUX:我如何编译我的第一个程序?
- elasticsearch - 为什么filebeat无法连接到logstash
- python - API调用限制
- karate - 可以使用空手道使用数据表进行数据库驱动测试吗?
- asp.net-core - 如何基于子域 MVC Core 制作 Route
- docker - 如何在 Windows 上访问 minikube docker daemon?
- r - 基于数据框R将数据添加到列表
- amazon-web-services - lambda函数抱怨文件名
- swift - UITableViewCell 宽度不会在旋转到横向时扩展
- reactjs - React Redux 从后端方法获取数据