简体   繁体   English

导航栏位于标题的前面,滚动时始终位于屏幕顶部

[英]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. 我希望它看起来像http://www.w3schools.com/css/default.asp网站,用于计算机屏幕。

My example (not working correctly): [censored] 我的示例(无法正常工作):[已审查]

CSS code: CSS代码:

* {
  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: HTML代码:

<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. 我试图了解w3schools网站的资源,但是我缺少一些东西,我不知道它是什么。 I don't want to use Bootstrap. 我不想使用Bootstrap。 The best solution would be to use only HTML and CSS. 最好的解决方案是仅使用HTML和CSS。

You can do like this see the example below: 您可以这样做,请参见以下示例:

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

CSS: 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: 对于滚动情况,我们可以使用如下所示的javascript:

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

For more details refer this: http://jsfiddle.net/cc48t/ 有关更多详细信息,请参见: http : //jsfiddle.net/cc48t/

Add js on project of you 在您的项目上添加js

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

and css below 和下面的CSS

.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 将代码与注释中提供的JSFiddle Rahul S合并, 这是最终代码

Please also note that I removed the z-index property from the .content class of your css. 另请注意,我从css的.content类中删除了z-index属性。

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

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