Javascript 当页面滚动时,如何使我的下拉导航不出错?

Javascript 当页面滚动时,如何使我的下拉导航不出错?,javascript,navigation,fixed,absolute,Javascript,Navigation,Fixed,Absolute,当页面滚动时,下拉导航“bugs out”。当页面滚动时,导航将打开和关闭。然后,当您滚动回到顶部,导航回到相对位置时,下拉列表将不再打开,直到您开始向下滚动(此时第一个错误再次出现) 有没有更有效的方法来完成这项任务 我在下面列出了完整的代码,以防网站上有其他可能导致问题发生的内容 我正在寻找JavaScript的答案 /*导航滚动条*/ var startPos=-1; window.onscroll=函数(){ var bar=document.getElementById('pm-na

当页面滚动时,下拉导航“bugs out”。当页面滚动时,导航将打开和关闭。然后,当您滚动回到顶部,导航回到相对位置时,下拉列表将不再打开,直到您开始向下滚动(此时第一个错误再次出现)

有没有更有效的方法来完成这项任务

我在下面列出了完整的代码,以防网站上有其他可能导致问题发生的内容

我正在寻找JavaScript的答案

/*导航滚动条*/
var startPos=-1;
window.onscroll=函数(){
var bar=document.getElementById('pm-nav');
var ourCompany=document.getElementById(“我们的公司”);
var services=document.getElementById(“服务”);
var products=document.getElementById(“产品”);
var resources=document.getElementById(“资源”);
var goTo=document.getElementById(“转到”);
如果(startPos<0)startPos=findPosY(bar);
如果(pageYOffset>startPos){
bar.style.position='fixed';
bar.style.top=0;
toggle(“下拉内容滚动”);
services.classList.toggle(“下拉内容滚动”);
products.classList.toggle(“下拉内容滚动”);
resources.classList.toggle(“下拉内容滚动”);
toggle(“下拉内容滚动”);
}否则{
bar.style.position='相对';
ourCompany.classList.remove('dropdown-content-scroll');
services.classList.remove('dropdown-content-scroll');
products.classList.remove('dropdown-content-scroll');
resources.classList.remove('dropdown-content-scroll');
goTo.classList.remove('dropdown-content-scroll');
}
};
函数findPosY(obj){
var-curtop=0;
if(类型(对象偏移父对象)!=“未定义”和对象偏移父对象(&J){
while(对象抵销父对象){
curtop+=对象偏移量;
obj=obj.offsetParent;
}
curtop+=对象偏移量;
}
否则,如果(对象y)
curtop+=对象y;
返回curtop;
}
/*当用户单击按钮时,在隐藏和显示下拉列表内容之间切换*/
功能切换下降(下降){
开关(下降){
案例“我们公司”:
document.getElementById(“服务”).classList.remove('active-drop');
document.getElementById(“产品”).classList.remove('active-drop');
document.getElementById(“资源”).classList.remove('active-drop');
document.getElementById(“go to”).classList.remove(“active-drop”);
document.getElementById(“我们公司”).classList.toggle(“活动拖放”);
打破
案例“服务”:
document.getElementById(“我们的公司”).classList.remove('active-drop');
document.getElementById(“产品”).classList.remove('active-drop');
document.getElementById(“资源”).classList.remove('active-drop');
document.getElementById(“go to”).classList.remove(“active-drop”);
document.getElementById(“服务”).classList.toggle(“活动拖放”);
打破
案例“产品”:
document.getElementById(“服务”).classList.remove('active-drop');
document.getElementById(“我们的公司”).classList.remove('active-drop');
document.getElementById(“资源”).classList.remove('active-drop');
document.getElementById(“go to”).classList.remove(“active-drop”);
document.getElementById(“产品”).classList.toggle(“活动拖放”);
打破
案例“资源”:
document.getElementById(“服务”).classList.remove('active-drop');
document.getElementById(“产品”).classList.remove('active-drop');
document.getElementById(“我们的公司”).classList.remove('active-drop');
document.getElementById(“go to”).classList.remove(“active-drop”);
document.getElementById(“资源”).classList.toggle(“活动拖放”);
打破
案例“转到”:
document.getElementById(“服务”).classList.remove('active-drop');
document.getElementById(“产品”).classList.remove('active-drop');
document.getElementById(“资源”).classList.remove('active-drop');
document.getElementById(“我们的公司”).classList.remove('active-drop');
document.getElementById(“转到”).classList.toggle(“活动拖放”);
打破
违约:
//让这个未知。。。
}
}
var ourCompany=document.getElementById(“我们的公司”);
var services=document.getElementById(“服务”);
var products=document.getElementById(“产品”);
var resources=document.getElementById(“资源”);
var goTo=document.getElementById(“转到”);
//如果用户在下拉列表之外单击,请关闭下拉列表
window.onclick=函数(e){
如果(!e.target.matches('.dropbtn')){
if(ourCompany.classList.contains('active-drop')){
ourCompany.classList.remove('active-drop');
}
if(services.classList.contains('active-drop')){
services.classList.remove('active-drop');
}
if(products.classList.contains('active-drop')){
products.classList.remove('active-drop');
}
if(resources.classList.contains('active-drop')){
resources.classList.remove('active-drop');
}
if(goTo.classList.contains('active-drop')){
goTo.classList.remove('active-drop');
}
}
}
/*基本站点规则*/
*{边距:0;填充:0;}
.maxWidth-1280{最大宽度:1280px;边距:0自动;}
.maxWidth-1440{最大宽度:1440px;边距:0自动;}
.pm框大小*{框大小:边框框;}
/*纵队*/
.pm-col-5{
浮动:左;
宽度:20%;
}
/*清除列后的浮动