简体   繁体   中英

Navigation bar in front of the header and always on top of the screen when scrolling

I'm trying to create a website, where after load the navigation bar is under the header and when it is scrolled down - navigation bar should go to the top of the screen and be attached to it, and everything that is under the navigation bar should be behind it. I want it to look like http://www.w3schools.com/css/default.asp website for computer screens.

My example (not working correctly): [censored]

CSS code:

* {
  box-sizing: border-box;
}
body {
  margin: 0;
  padding: 0;
}
.header {
  width: 100%;
  height: 100px;
  padding: 15px;
  color: #2A5282;
  background-color: #E8FF79;
  position: fixed;
  top: 0;
  z-index: -1;
}
.nav {
  width: 100%;
  height: 40px;
  color: white;
  background: #2A5282;
  position: absolute; 
  top: 100px;
  z-index: 1;
}
.nav ul {
  margin: 0;
  padding: 0;
  overflow: hidden;
}
.nav ul li {
  list-style-type: none;
  float: left;
}
.nav ul li a {
  color: #E8FF79;
  text-align: center;
  text-decoration: none;
  font: bold 16px verdana;
  width: 150px;
  margin: 0;
  padding: 10px 0;
  display: block;
}
.nav ul li a:hover {
  background-color: #2B5D9C;
}
.content {
  width: 100%;
  padding: 0 20px;
  color: black;
  background-color: white;
  font: bold 14px verdana;
  position: absolute;
  top: 140px;
  z-index: 1;
}

HTML code:

<div class="header">
  <h1>This is a header</h1>
</div>
<div class="nav">
  <ul>
    <li><a href="#">Option 1</a></li>
    <li><a href="#">Option 2</a></li>
    <li><a href="#">Option 3</a></li>
    <li><a href="#">Option 4</a></li>
  </ul>
</div>
<div class="content">
  <p>This is a paragraph.</p>
  <p>This is a paragraph.</p>
  <p>This is a paragraph.</p>
  <p>This is a paragraph.</p>
  <p>This is a paragraph.</p>
  <!-->...<-->
</div>

I was trying to understand w3schools website source, but there's something I'm missing and I don't know what it is. I don't want to use Bootstrap. The best solution would be to use only HTML and CSS.

You can do like this see the example below:

<div id="scroller">Some controls</div>

CSS:

body {
    height: 3000px;
    top: 0;
    position: relative;
}
#scroller {
    position: relative;
    top: 100px;
    width: 100%;
    background: #CCC;
    height: 100px;
}

We can use javascript like this for scroll case:

$(window).scroll(function () {
    if ($(window).scrollTop() > 100) {
        $('#scroller').css('top', $(window).scrollTop());
    }
}
);

For more details refer this: http://jsfiddle.net/cc48t/

Add js on project of you

$(function () {
    $(window).scroll(function () {
        if ($(this).scrollTop() > 20) {
            $('.nav').addClass('nav-pin').fadeIn();
        } else {
            $('.nav').removeClass('nav-pin');
        }
    });
});

and css below

.nav-pin{position:fixed; top:0; left:0; z-index:9999;}

Merging your code with the JSFiddle Rahul S provided in the comment, this is the final code

Please also note that I removed the z-index property from the .content class of your css.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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