Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
145 views
in Technique[技术] by (71.8m points)

html - Nav bar to remain fixed as soon as it reaches top of page

In my code there is a banner at the top of the page and a nav bar just below it. What I want to do is when I scroll down, the banner & nav bar scroll like they should but as soon as the nav bar reaches the top of the page, it remains there even when I continue scrolling. any ideas on how i can do this?

(Note: for the moment I know only HTML/CSS; I don't know Javascript yet but if Javascript is the only solution, I will try to understand).

HTML:

<body>
    <div id="wrapper">
        <div id="header">
        </div>
        <nav id="top_menu">
            <ul>
                <li class="current"><a href="#">HOME</a></li>
                <li><a href="#">MY PROFILE</a></li>
                <li><a href="#">THE FLEET</a>
                    <ul>
                        <li><a href="#">NISSAN</a></li>
                        <li><a href="#">TOYOTA</a></li>
                        <li><a href="#">HONDA</a></li>
                        <li><a href="#">MAZDA</a></li>
                    </ul>
                </li>
                <li><a href="#">OTHER SERVICES</a></li>
                <li><a href="#">TERMS & CONDITIONS</a></li>
                <li><a href="#">FREQUENTLY ASKED QUESTIONS</a></li>
                <li><a href="#">ABOUT US</a></li>
            </ul>
        </nav>
        <div class="content">
        </div>
        <div id="footer">? rentPRO 2015</div>        
    </div>
</body>

CSS:

<style> 

    #header {
        background-image: url("https://lh6.googleusercontent.com/-4epq8pWSUKQ/VNX4lGi_GrI/AAAAAAAAAIo/9gDg5CubDO8/w1044-h201-no/header%2Bfinal1.jpg");
        background-size: 100% 100%;
        width: 1075px;
        height: 200px;
        margin: 0 auto;
        position: relative;
    }

    #top_menu {
        display:block;
        position: fixed;
        top: 210px;
        left: 136px;
        border-radius: 4px;
        border-width: 1px 1px 1px;
        border-style:solid;
        background-color: white;
        font-family: 'Lato', sans-serif;
        font-size: 14.85px;
        height: 40px;
        width: 1075px;
        margin: 0 auto;
        border-color: #A4A4A4;
    }

</style>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Jolan, most of you code is right, but you need a tiny bit of Javascript into your code for the nav bar to slide up while scrolling. Hope this helps.

var header = document.querySelector("#header");

new Headroom(header, {
  tolerance: {
    down : 2,
    up : 5
  },
  offset : 100,
  classes: {
    initial: "slide",
    pinned: "slide--reset",
    unpinned: "slide--up"
  }
}).init();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...