简体   繁体   English

我如何在 javascript 或 jquery 中显示和隐藏站点 header

[英]how can i show and hide site header in javascript or jquery

I am working on a Bootstrap use case.我正在研究一个引导用例。 I have designed a menu using Bootstrap.我使用 Bootstrap 设计了一个菜单。 I want the menu to be hidden when I scroll down, and the menu to be displayed if I scroll up.我希望向下滚动时隐藏菜单,向上滚动时显示菜单。 (slide-down / slide-up). (向下滑动/向上滑动)。

I want to use jQuery to for this functionality.我想使用 jQuery 来实现此功能。

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

function scrollFunction() {
  if (document.body.scrollTop > 150 || document.documentElement.scrollTop > 150) {

    $('.center-menu').slideUp();

    $(bottomDiv).fadeIn();
  } else {
    $('.center-menu').slideDown();
    $(bottomDiv).fadeOut();
  }
}

This use case can be performed using JQuery.可以使用 JQuery 执行此用例。 You can hide a div using a call like this:您可以使用如下调用隐藏 div:

 $('#menu').hide();

To show a div, use this call:要显示 div,请使用以下调用:

$('#menu').show()

Of course, you need to put in logic to handle the scroll.当然,您需要输入逻辑来处理滚动。 Something like this will work.像这样的东西会起作用。

var lastScrollTop=150;
$(window).scroll(function(event){
   var st = $(this).scrollTop();
   if (st > lastScrollTop){
       // downscroll code
   } else {
      // upscroll code
   }
   lastScrollTop = st;
});

More information here .更多信息在这里

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

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