静态顶部导航栏仅在滚动后显示

Static top navigation bar shows only after scrolling

本文关键字:滚动 显示 顶部 导航 静态      更新时间:2023-09-26

我有一个导航栏,滚动后会留在顶部,但是如果您点击后退按钮或类似的链接......com/index#down下方的导航栏在您滚动之前不会显示。

无论如何,如何让导航栏显示?

var num = 60; 
$(window).bind('scroll', function () {
    if ($(window).scrollTop() > num) {
        $('.menutop').addClass('fixed');
    } else {
        $('.menutop').removeClass('fixed');
    }
});

斯菲德尔

在 url 中,当有 # 未触发滚动事件时。 因此菜单不会被修复。将此代码添加到 my-jquery 的顶部.js

var ssss=$(document).scrollTop();
if(ssss>=60)  $('.menutop').addClass('fixed');

如果您希望始终修复它,只需删除该JavaScript并在HTML中添加fixed类即可。

<nav class="menutop fixed">

您可以在 DOM 就绪上运行一次,然后绑定以滚动:

var toggleMenuVisibility = function() {
  var num = 60; 
  if ($(window).scrollTop() > num) {
    $('.menutop').addClass('fixed');
  } else {
    $('.menutop').removeClass('fixed');
  }
}
$(function() {
  toggleMenuVisibility();
  $(window).bind('scroll', toggleMenuVisibility)
})