简体   繁体   English

滚动时如何制作消失的导航栏(带有漂亮的动画)

[英]How to make a navigation bar which disappears (with a nice animation) when you scroll

I am trying to do a navigation bar which disappears when scrolling, with a nice animation. 我试图做一个导航栏,滚动时消失,动画效果很好。 Here is what i made so far. 这是我到目前为止所做的。

HTML: HTML:

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="css/style.css" type="text/css">
        <link rel="icon" href="favicon.PNG" type="image/gif">
        <title>Top News</title> 
    </head>
    <body>
        <div class = "fixedbc">
            <div class="bwbutton">Welcome to Top News</div>
            <header>asdasd</header>
        </div>
    </body>
</html>

CSS: CSS:

    /* ===================   Needs   =================== */
html, body {
      width: 100%;
      height: 100%;
      background: white;
      margin:0;
      padding:0;
      border:0px;
    }

/* ===================   Buttons   =================== */

.bwbutton {
    background-color:transparent;
    border:6px solid #ffffff;
    display:inline-block;
    cursor:pointer;
    color:#ffffff;
    font-family:Georgia;
    font-size:45px;
    padding:13px 60px;
    text-decoration:none;
    position:absolute;
    top:30%;
    left:29%;
    transition: all .1s ease-in;
}
.bwbutton:hover {
    background-color:transparent;
    border:6px solid black;
    color:black;
    transition: all 0.2s ease-in;
}
.bwbutton:active {

}

/* ===================   LAYOUT   =================== */

.fixedbc {
    min-height:100%;
    background-image: url("../bc.jpg");
    background-attachment: fixed;
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
}
marquee{
    text-decoration: none;
    margin-top:1.5%;
    color:white;
}

/* ===================   Header // Nav   =================== */

header {
  background: #f5b335;
  height: 40px;
  position: fixed;
  top: 0;
  transition: top 0.2s ease-in-out;
  width: 100%;
}
// we'll add this class using javascript
.nav-up {
  top: -40px; // same as header height. use variables in LESS/SASS
}

Javascript: Javascript:

var didScroll;
var lastScrollTop = 0;
var delta = 5;
var navbarHeight = $('header').outerHeight();

$(window).scroll(function(event){
    didScroll = true;
});

setInterval(function() {
    if (didScroll) {
        hasScrolled();
        didScroll = false;
    }
}

function hasScrolled() {
    var st = $(this).scrollTop();

    if(Math.abs(lastScrollTop - st) <= delta)
        return;
    if (st > lastScrollTop && st > navbarHeight){
        $('header').removeClass('nav-down').addClass('nav-up');
    } else {
        if(st + $(window).height() < $(document).height()) {
            $('header').removeClass('nav-up').addClass('nav-down');
        }
    }

    lastScrollTop = st;
}

Check this fiddle here 在这里检查这个小提琴

If you wanted to hide navigation bar on scroll with some animation, then make its position fixed and hide it on scroll. 如果要隐藏带有某些动画的滚动条,请固定其位置,然后将其隐藏在滚动条上。 (Need to add Jquery for this demo) (需要为此演示添加Jquery)

Like, Sample HTML 喜欢, 示例HTML

<header>Header</header>

Sample CSS CSS样本

body {
    margin: 0;
    padding: 0;
    height: 1000px
}

header {
    position:fixed;
    background: #111111;
    margin: 0px;
    padding: 0px;
    width: 100%;
    height:50px;
    color:#FFFFFF;
    -webkit-transition: all 0.35s;
       -moz-transition: all 0.35s;
            transition: all 0.35s;
    overflow: hidden
}

header.hide {
    margin-top: -50px;
}

Sample Jquery 样本jQuery

$(window).scroll(function() {
    if ($("header").offset().top > 50) {
        $("header").addClass("hide");
    } 
    else {
        $("header").removeClass("hide");
    }
});

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

相关问题 隐藏/显示导航栏时,刷新页面时将显示导航栏,然后在滚动时消失 - When hiding/showing navigation bar, the nav bar is visible when the page is refreshed then disappears when I scroll 如何使导航栏随页面滚动? - How to make navigation bar scroll with the page? 如何使导航栏固定在滚动顶部 - How to make the navigation bar fixed on top on scroll 哪个选择器可以固定导航栏并随页面滚动? - which selector to make navigation bar fixed and scroll with the page? 如何创建一个动态导航栏,当您到达某个位置时会跟随您 - How to create a dynamic navigation bar which follows you when you reach certain location 向下滚动页面时,如何使导航栏下方出现阴影? - How do I make a shadow appear under the navigation bar when I scroll down the page? 使用钛合金动画在滚动条上隐藏导航栏 - Hide navigation bar on scroll with animation Titanium 导航栏应该只在我滚动时显示,但当我刷新页面时已经可见,然后在滚动时消失 - The navigation bar should only show when I scroll, but is already visible when I refresh the page, then disappears when scrolling 如何使导航栏在滚动时变为半透明? - How to make my navigation bar turn semitransparent on scroll? 如何制作一个<li>仅在滚动时出现在导航栏中 - How to make a <li> appear in navigation bar only on scroll
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM