最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Show a floating navigation bar on scroll - Stack Overflow

programmeradmin2浏览0评论

I am using blogspot, and I wanted to exactly achieve this kind of floating navigation bar:

/

As you can see, when you scroll, the floating navigation bar shows as it slides down.

All I know is make a navigation bar:

<div id="floating-nav-content">
   <div class="floating-nav">
      <ul id="menu-floating-menu" class="menu">
         <li>...</li>
         <li>...</li>
         <li>...</li>
         <li>...</li>
      </ul>
   </div>
</div>

But the rest of the jQuery or javascript stuff is still unknown to me. I have also search but they don't teach exactly what I want.

I'm only new to jQuery and I still have no idea how to implement this.

Your help is much appreciated.

I am using blogspot, and I wanted to exactly achieve this kind of floating navigation bar:

http://apairandasparediy./

As you can see, when you scroll, the floating navigation bar shows as it slides down.

All I know is make a navigation bar:

<div id="floating-nav-content">
   <div class="floating-nav">
      <ul id="menu-floating-menu" class="menu">
         <li>...</li>
         <li>...</li>
         <li>...</li>
         <li>...</li>
      </ul>
   </div>
</div>

But the rest of the jQuery or javascript stuff is still unknown to me. I have also search but they don't teach exactly what I want.

I'm only new to jQuery and I still have no idea how to implement this.

Your help is much appreciated.

Share Improve this question asked May 1, 2015 at 7:42 kaynewilderkaynewilder 8537 gold badges28 silver badges53 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

This should do what you want. (assuming you've already included the jQuery library)

    <script type="text/javascript">
        $(document).scroll(function() {
            if ($(this).scrollTop() == 0) {
                $("#floating-nav-content").slideUp(400);
            } else {
                $("#floating-nav-content").slideDown(600);
            }

        });
    </script>

The CSS is also important, because this places the navigation bar in a fixed position on top of your page.

    <style type="text/css">
        html, body {
            margin: 0;
            padding: 0;
        }

        #floating-nav-content {
            top: 0;
            width: 100%;
            height: 20px;
            background-color: #000;
            position: fixed;
            display: none;
            color: #FFF;
            padding: 5px;
        }
    </style>

And ofcourse the HTML. I placed all of the above and below in the body tag.

    <div id="floating-nav-content">
        Content
    </div>
发布评论

评论列表(0)

  1. 暂无评论