繁体   English   中英

粘性导航和滚动到顶部功能单独工作,但不能一起工作

[英]sticky navigation and scroll to top functions work individually, but don't work together

我的网站上只有两个脚本,它们单独工作,一起工作,他们不......我在这里错过了什么?

第一个脚本:

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");
  }
}

第二个脚本:

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;
}

我不明白他们为什么不一起工作。

您通过执行双重声明来覆盖 onscroll function

window.onscroll = function() {myFunction()}; // oh no, I'll be replaced

window.onscroll = function() {scrollFunction()}; // I'll be called only

尝试:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM