javascript - 到达网站底部时如何使导航可见?
问题描述
我不知道为什么,但是当我到达网站底部时,导航应该会再次出现。我该如何解决?
相反的工作=每次您到达网站顶部时,都会出现导航。如果您在开头向下滚动=导航消失+如果您将鼠标悬停在导航上=它再次出现并离开它消失。
导航出现在顶部 + 滚动时消失
$(document).ready(function() {
$(window).on('scroll', function() {
$('.nav-visibility').css("opacity", $(window).scrollTop() > 0 ? "0" : "1")
})
})
悬停时导航显示和隐藏
$('nav').mouseover(function() {
$('.nav-visibility').css("opacity", "1");
});
$('nav').mouseout(function() {
if ($(window).scrollTop() > 0) {
$('.nav-visibility').css("opacity", "0");
}
});
导航应该在到达底部时出现/不起作用?!
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() == $(document).height()) {
$('.nav-visibility').css("opacity", "1");
}
});
解决方案
滚动事件方法:
document.addEventListener('scroll', function(e) {
if((document.documentElement.scrollTop + window.innerHeight) >= document.documentElement.scrollHeight){
// Do something!
// document.getElementById('foo').setAttribute("style", "display: none");
}
}, true);
例子:
document.addEventListener('scroll', function(e) {
if((document.documentElement.scrollTop + window.innerHeight) >= document.documentElement.scrollHeight){
document.getElementById('foo').innerHTML = "Reached Bottom";
document.getElementById('foo').setAttribute("style", "height: 100px;");
}
}, true);
<div id="foo" style="height: 1800px">Scroll to Bottom</div>
推荐阅读
- server - ISPConfig 服务错误的站点
- javascript - 相互堆叠的对象
- typescript - 猫鼬模式的函数参数类型,在 Typescript 中?
- spring-batch - Spring批量读取从xml写入csv文件
- python-3.x - 安装 python 模块,然后使用 .bat 文件运行 python 脚本
- c# - 在 ASP.NET C# 中使用 SQL 查询对 Datatable 行着色
- javascript - 异步获取数据不重新渲染 ReactJS 数据表(或只是不显示新数据)
- python - 如何在 Matplotlib 中剪切长标签
- java - 我想在java中只创建内存中的rocksDB
- c# - ASP.NET - 单击图像应重定向到具有图像 url 的特定页面