[英]sticky navigation and scroll to top functions work individually, but don't work together
I have two scripts on my website only, single they work, together they don't... what am I missing here?我的网站上只有两个脚本,它们单独工作,一起工作,他们不......我在这里错过了什么?
first script:第一个脚本:
window.onscroll = function() {myFunction()};
var navigation = document.getElementById("navigation");
var sticky = navigation.offsetTop;
function myFunction() {
if (window.pageYOffset > sticky) {
navigation.classList.add("sticky");
} else {
navigation.classList.remove("sticky");
}
}
second script:第二个脚本:
mybutton = document.getElementById("tothetop");
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
if (document.body.scrollTop > 98 || document.documentElement.scrollTop > 98) {
mybutton.style.display = "block";
} else {
mybutton.style.display = "none";
}
}
function topFunction() {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}
I don't see why they don't work together.我不明白他们为什么不一起工作。
You override onscroll function by doing your double declaration您通过执行双重声明来覆盖 onscroll function
window.onscroll = function() {myFunction()}; // oh no, I'll be replaced
window.onscroll = function() {scrollFunction()}; // I'll be called only
try:尝试:
window.onscroll = function() {
myFunction();
scrollFunction();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.