小编I W*_*ers的帖子

如何阻止滚动事件重复发生?

有人可以帮助我处理网站上的活动吗?问题是,每次触发滚动事件时它们都会重复出现。导航菜单位于标题下方,但在滚动时粘在顶部,但我希望当我们一直向上滚动时它回到标题下方。我不知道我做错了什么。这是我的代码:

var navigation = document.querySelector("nav");
var borderBottom = navigation.getAttribute("border-bottom");
var currentDistance = (document.documentElement.scrollHeight);

addEventListener ("scroll", function() {

if (currentDistance > "90px") {
navigation.style.position = "fixed";
navigation.style.top = "0";
navigation.style.borderBottom = "3px solid rgb(157,0,53)";
}

else removeEventListener ("scroll", function() {
navigation.style.position = "fixed";
navigation.style.top = "0";
navigation.style.borderBottom = "3px solid rgb(157,0,53)";
}
);
});
Run Code Online (Sandbox Code Playgroud)

此外,当页面加载并触发滚动事件时,一个 div 飞入,这就是我想要的。但我的问题是,每次触发滚动事件时,盒子都会不断飞入,而不是事件停止,即不断重新发生。这是我使用的代码:

var flyInDiv = document.getElementById("flyInDiv");

addEventListener ("scroll", function() {
var programMenu = document.createElement("DIV");

programMenu.className = "programMenu";
programMenu.appendChild(flyInDiv);

document.body.appendChild(programMenu);

programMenu.style.position = "fixed";

});
Run Code Online (Sandbox Code Playgroud)

我网站的 URL 是 …

javascript dom scroll dom-events

2
推荐指数
1
解决办法
2427
查看次数

标签 统计

dom ×1

dom-events ×1

javascript ×1

scroll ×1