简体   繁体   English

向下滚动时隐藏导航栏并在向上滚动时显示它

[英]Hide nav bar on scroll down and show it on scroll up

I'm using this wordpress theme http://newnotio.fuelthemes.net/space/ and I'd like the nav bar to be hidden on scroll down and to be visible on scroll up (instead of always visible).我正在使用这个 wordpress 主题http://newnotio.fuelthemes.net/space/我希望导航栏在向下滚动时隐藏并在向上滚动时可见(而不是始终可见)。

Can you help me to achieve this?你能帮我实现这个目标吗?

Edit 15/07 : I've managed to add a class to the header php script of the theme.编辑 15/07 :我已经设法将 class 添加到主题的 header php 脚本中。 I've called it nav-down as I'm trying to replicate this: http://jsfiddle.net/mariusc23/s6mLJ/31/我将其命名为 nav-down,因为我正在尝试复制它: http://jsfiddle.net/mariusc23/s6mLJ/31/

I've also copy/pasted the JS code but I'm getting an error message that "$ is not a function".我还复制/粘贴了 JS 代码,但收到“$ 不是函数”的错误消息。 Any idea what the issue is?知道问题是什么吗? Thanks谢谢

在此处输入图像描述

 .header { display: flex; align-items: center; height: 50px; position: fixed; top: 0; left: 0; background: red; width: 100%; z-index: 101; padding: 0 15px; -moz-transform: translateZ(0); -webkit-transform: translateZ(0); transform: translateZ(0); }.nav-up { top: -50px; }
 <header class="header style2 **nav-down**"> <nav id="full-menu" role="navigation"> </nav> </header>

You can achieve this without adding a class to your header, using plain javascript.您可以使用普通的 javascript 来实现这一点,而无需在标题中添加类。 Here is an example:下面是一个例子:

 window.onscroll = function(e) { var scrollY = window.pageYOffset || document.documentElement.scrollTop; var header = document.querySelector('header'); scrollY <= this.lastScroll ? header.style.visibility = 'visible' : header.style.visibility = 'hidden'; this.lastScroll = scrollY ; }
 body { height: 2000px; } header { position: fixed; top: 0; }
 <header> Sample Header (scroll up/down to show/hide) </header>

Edit: here is an updated snippet that should work for the website in question.编辑:这是一个更新的片段,应该适用于相关网站。

 window.onscroll = function(e) { var scrollY = window.pageYOffset || document.documentElement.scrollTop; var header = document.querySelector('header'); var height = -header.clientHeight; header.style.transition = 'transform 0.1s'; (scrollY <= Math.max(this.lastScroll, 50) || window.innerWidth <= 1200 || this.loaded === undefined) ? header.style.transform = 'translateY(0px)' : header.style.transform = 'translateY(' + height + 'px)' this.lastScroll = scrollY; this.loaded = true; }
 body { height: 2000px; } header { position: fixed; top: 0; }
 <header> Sample Header (scroll up/down to show/hide) </header>

First install plugin to add custom JS and CSS (although CSS can be added without this plugin), you can use "Simple Custom CSS and JS" plugin.首先安装插件添加自定义JS和CSS(虽然CSS可以不用这个插件添加),你可以使用“简单的自定义CSS和JS”插件。

Then, let's say your navbar ID is "#site-header" and is 80px high.然后,假设您的导航栏 ID 是“#site-header”并且高 80 像素。 Here you have the full code:在这里你有完整的代码:

CSS CSS

#site-header { 
    position:fixed !important; 
    top:0;
    left:0;
    width:100%;
    transition:0.5s ease-in-out;
    height:100px;
    z-index:100;
} 

.nav-show{
transform:translatey(-100px);
left:0;
}

and JS (jQuery)和 JS (jQuery)

jQuery(document).ready(function( $ ){
  var previousScroll = 0;
  $(window).scroll(function(){
       var currentScroll = $(this).scrollTop();
    
       if (currentScroll > previousScroll){
           $('#site-header').addClass('nav-show');
       } else {
           $('#site-header').removeClass('nav-show');
       }
       previousScroll = currentScroll;
    });
  
});

With JS without jQueryJSjQuery

 let navBar = document.querySelector('nav') let prevScrollPos = window.pageYOffset window.addEventListener('scroll', () => { let currentScrollPos = window.pageYOffset prevScrollPos > currentScrollPos? navBar.style.top = '0': navBar.style.top = '-40px' prevScrollPos = currentScrollPos })
 body { height: 7000px; background-color: black; margin:0; padding:0 } nav{ width: 100%; background-color: gold; color: black; text-align: center; padding: .5rem; font: bold 2rem monospace; position: fixed; top: 0; transition: .3s; z-index: 999; }
 <nav>Nav</nav>

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

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