Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 粘性菜单-防止页面刷新时加载_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 粘性菜单-防止页面刷新时加载

Javascript 粘性菜单-防止页面刷新时加载,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我在页面的下方使用了一个粘性菜单,因此当用户滚动到它时,它会粘在顶部。当用户向上滚动时,当您到达页面上粘性菜单的位置时,它将停止粘在顶部 一切正常。唯一的问题是,如果有人在粘性菜单位于顶部时刷新页面,当您在页面上传递它时,它不会解除粘滞(无论您在哪里,它都会保持粘滞) 我能做些什么来消除它吗 var menu=document.querySelector('.menu-t') var menupposition=menu.getBoundingClientRect().top; window.

我在页面的下方使用了一个粘性菜单,因此当用户滚动到它时,它会粘在顶部。当用户向上滚动时,当您到达页面上粘性菜单的位置时,它将停止粘在顶部

一切正常。唯一的问题是,如果有人在粘性菜单位于顶部时刷新页面,当您在页面上传递它时,它不会解除粘滞(无论您在哪里,它都会保持粘滞)

我能做些什么来消除它吗

var menu=document.querySelector('.menu-t')
var menupposition=menu.getBoundingClientRect().top;
window.addEventListener('scroll',函数(){
如果(window.pageYOffset>=菜单位置){
menu.style.position='fixed';
menu.style.top='0px';
}否则{
menu.style.position='static';
menu.style.top='';
}
});
。页面部分{
边框底部:1px实心#ddd;
溢出:隐藏;
}
.page-section.page-section-center{
对齐内容:居中对齐;
文本对齐:居中;
}
.menu-t{
保证金:0;
填充:0;
宽度:100%;
背景色:#FFF;
z指数:1000;
边框底部:1px#eee点;
}
.menu-t li{
显示:内联块;
文本对齐:居中;
填充:20px;
文本转换:大写;
字体大小:14px;
}
.menu-TA{
显示:块;
填充:10px0;
颜色:#32404E!重要;
-webkit过渡:颜色轻松0.3s;
-o-过渡:颜色缓和0.3s;
过渡:色缓0.3s;
}
.menu-TA:悬停{
颜色:#2db2e9!重要;
}





  • 项目
  • 项目



























DOMContentLoaded
eventListener添加到javascript中

document.addEventListener("DOMContentLoaded", function(event) {
  var menu = document.querySelector('.menu-t')
  var menuPosition = menu.getBoundingClientRect().top;
  window.addEventListener('scroll', function () {
      if (window.pageYOffset >= menuPosition) {
          menu.style.position = 'fixed';
          menu.style.top = '0px';
      }
      else {
          menu.style.position = 'static';
           menu.style.top = '';
        }
    });
});

这似乎并没有解决问题。整个页面仍然粘着。