gpt4 book ai didi

javascript - 为什么 window.onscroll 函数表现得很奇怪?

转载 作者:搜寻专家 更新时间:2023-10-31 08:13:12 26 4
gpt4 key购买 nike

我对编码和所有东西都很陌生,但是通过教程和所有其他东西对所有东西都有了很好的理解。

我正试图在有人向下滚动时更改此 Logo 。它正在工作,但我试图弄清楚为什么它会在页面首次加载并且有人向下滚动时在一开始就快速切换类。它只在开始时这样做,但之后就可以正常工作了。

有什么提示或建议吗??谢谢!!

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

function navScroll() {
var logoNav = document.getElementById("bgNav");

if (document.body.scrollTop > 100 || document.documentElement.scrollTop > 100) {
logoNav.className = "play";
} else {
logoNav.className = "playBack";
}
}
@charset "UTF-8";

.container {
height: 2000px;
background-color: #c3c3c3;
}

@keyframes play60 {
0% {
background-position: 0 0;
}
100% {
background-position: -30000px 0px;
}
}

@keyframes play60Back {
0% {
background-position: -30000px 0px;
}
100% {
background-position: 0 0;
}
}

#bgNav.play {
animation-name: play60;
animation-fill-mode: forwards;
}

#bgNav.playBack {
animation-name: play60Back;
animation-fill-mode: forwards;
}

#bgNav {
height: 117px;
width: 500px;
position: fixed;
animation-duration: 2000ms;
animation-timing-function: steps(60);
background-repeat: no-repeat;
background-image: url("https://metro1.000webhostapp.com/metro-nav-animated.svg");
}
<!DOCTYPE html>
<html>
<body>
<div class="container">
<div class="" id="bgNav"></div>

</div>
</body>
</html>

最佳答案

这很好用

} else if(logoNav.className === "play") {
logoNav.className = "playBack";
}

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

function navScroll() {
var logoNav = document.getElementById("bgNav");

if (document.body.scrollTop > 100 || document.documentElement.scrollTop > 100) {
logoNav.className = "play";
} else if(logoNav.className === "play") {
logoNav.className = "playBack";
}
}
@charset "UTF-8";

.container {
height: 2000px;
background-color: #c3c3c3;
}

@keyframes play60 {
0% {
background-position: 0 0;
}
100% {
background-position: -30000px 0px;
}
}

@keyframes play60Back {
0% {
background-position: -30000px 0px;
}
100% {
background-position: 0 0;
}
}

#bgNav.play {
animation-name: play60;
animation-fill-mode: forwards;
}

#bgNav.playBack {
animation-name: play60Back;
animation-fill-mode: forwards;
}

#bgNav {
height: 117px;
width: 500px;
position: fixed;
animation-duration: 2000ms;
animation-timing-function: steps(60);
background-repeat: no-repeat;
background-image: url("https://metro1.000webhostapp.com/metro-nav-animated.svg");
}
<!DOCTYPE html>
<html>
<body>
<div class="container">
<div class="" id="bgNav"></div>

</div>
</body>
</html>

关于javascript - 为什么 window.onscroll 函数表现得很奇怪?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53826339/

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