gpt4 book ai didi

javascript - 导航栏不会隐藏在滚动条上

转载 作者:行者123 更新时间:2023-11-30 09:15:33 25 4
gpt4 key购买 nike

我刚刚在网站右下角添加了一个按钮,允许用户返回页面顶部,但它似乎取消了我的导航栏在用户滚动时隐藏。我整个早上都在看它,真的很感激能有一双新的眼睛来看它,任何帮助都会很棒!

谢谢。

var prevScrollpos = window.pageYOffset;
window.onscroll = function() {
var currentScrollPos = window.pageYOffset;
if (prevScrollpos > currentScrollPos) {
document.getElementById("navigation").style.top = "0";
} else {
document.getElementById("navigation").style.top = "-70px";
}
prevScrollpos = currentScrollPos;
}

window.onscroll = function() {scrollFunction()};

function scrollFunction() {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
document.getElementById("myBtn").style.display = "block";
} else {
document.getElementById("myBtn").style.display = "none";
}
}

function topFunction() {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}
nav {
z-index: 99;
box-sizing: border-box;
position: fixed;
top: 0;
left: 0;
right: 0;
padding: 20px 60px;
text-align: center;
background-color: white;
font-size: 18px;
letter-spacing: 1px;
transition: top 0.3s;
border-bottom: #f1f1f1 2px solid;
}

nav a:first-child {
float: left;
}

.main-nav {
text-decoration: none;
display: inline-block;
float: right;
text-transform: uppercase;
color: #adb3c1;
padding: 1px 0;
margin: 0 10px;
transition: all 0.15s ease-in-out;
z-index: 10;
}

#navigation a:hover {
color: blue;
transition: 0.5s;
}

.fa-long-arrow-alt-up {
display: none;
position: fixed;
bottom: 20px;
right: 20px;
z-index: 99;
border: none;
outline: none;
background: none;
color: black;
cursor: pointer;
padding: 15px;
font-size: 18px;
}
#myBtn:hover {
color: red;
}
<!DOCTYPE html>
<html>
<head>
<title>Hector's Portfolio</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="js/javascript.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body class="layout-portfolio">
<nav id="navigation">
<a data-scroll class="main-nav" class="nav-hover" href="#home">Home</a>
<a data-scroll class="main-nav" class="nav-hover" href="#contact">Contact</a>
<a data-scroll class="main-nav" class="nav-hover" href="#about">About</a>
<a data-scroll class="main-nav" class="nav-hover" href="#projects">Projects</a>
</nav>
<!-- Button to go to top -->
<button onclick="topFunction()" id="myBtn" class="fas fa-long-arrow-alt-up"></button>
</body>
</html>

最佳答案

window.onscroll 只能设置一次,当你设置第二次时,它会取代第一次。

解决方案:将所有代码放在同一个函数中(我在第一个函数中添加了 scrollFunction();,但也可以相反):

var prevScrollpos = window.pageYOffset;
window.onscroll = function() {
var currentScrollPos = window.pageYOffset;
if (prevScrollpos > currentScrollPos) {
document.getElementById("navigation").style.top = "0";
} else {
document.getElementById("navigation").style.top = "-70px";
}
prevScrollpos = currentScrollPos;

scrollFunction();
}

function scrollFunction() {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
document.getElementById("myBtn").style.display = "block";
} else {
document.getElementById("myBtn").style.display = "none";
}
}

function topFunction() {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}
nav {
z-index: 99;
box-sizing: border-box;
position: fixed;
top: 0;
left: 0;
right: 0;
padding: 20px 60px;
text-align: center;
background-color: white;
font-size: 18px;
letter-spacing: 1px;
transition: top 0.3s;
border-bottom: #f1f1f1 2px solid;
}

nav a:first-child {
float: left;
}

.main-nav {
text-decoration: none;
display: inline-block;
float: right;
text-transform: uppercase;
color: #adb3c1;
padding: 1px 0;
margin: 0 10px;
transition: all 0.15s ease-in-out;
z-index: 10;
}

#navigation a:hover {
color: blue;
transition: 0.5s;
}

.fa-long-arrow-alt-up {
display: none;
position: fixed;
bottom: 20px;
right: 20px;
z-index: 99;
border: none;
outline: none;
background: none;
color: black;
cursor: pointer;
padding: 15px;
font-size: 18px;
}
#myBtn:hover {
color: red;
}
<!DOCTYPE html>
<html>
<head>
<title>Hector's Portfolio</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="js/javascript.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body class="layout-portfolio">
<nav id="navigation">
<a data-scroll class="main-nav" class="nav-hover" href="#home">Home</a>
<a data-scroll class="main-nav" class="nav-hover" href="#contact">Contact</a>
<a data-scroll class="main-nav" class="nav-hover" href="#about">About</a>
<a data-scroll class="main-nav" class="nav-hover" href="#projects">Projects</a>
</nav>
<div style="height: 700px;"></div>
<!-- Button to go to top -->
<button onclick="topFunction()" id="myBtn" class="fas fa-long-arrow-alt-up"></button>
</body>
</html>

关于javascript - 导航栏不会隐藏在滚动条上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55396312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com