如何在滚动时将导航栏固定在顶部

时间:2016-09-18 10:08:02

标签: javascript html css

这是我的fiddle我在本网站上看到了一些主题以实现它,但我在应用它时犯了一些错误。有人能帮我吗。

我想在滚动时将导航栏固定在顶部。



randomfunc :: Int -> Int -> String

randomFunc :: (Ord a, Num a) => a -> a -> String

body {
width: 100%;
height: 100%;
margin:0px;
 
}


.navbar {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #f2f2f2;

}

.navbar li {
float: left;

}

li a, .dropbtn {
display: inline-block;
color: #333;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}

.navbar li a:hover, .dropdown:hover .dropbtn {
background-color: #333;
color: #f2f2f2;
}

.navbar li.dropdown {
display: inline-block;
}

.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}

.dropdown-content a:hover {background-color: #333}

.navbar .show {display:block;}

.xavierslogo {
 width: 110px;
 height: 109px;
 margin-left: 20%;
 
}

.xavierstext {
 width: ;
 height: ;
 margin-left: 0%;
 
}


.topbg {
 
 background-color: #333;
}




1 个答案:

答案 0 :(得分:0)

将以下代码添加到此类

.navbar {
    list-style-type: none;
     overflow: hidden;
   position:fixed;
}

body {
    width: 100%;
    height: 100%;
	margin:0px;
 
}
.navbar {
    list-style-type: none;
     overflow: hidden;
   position:fixed;
}
.navbar li {
    float: left;
    
 
	
}

li a, .dropbtn {
    display: inline-
      block;
    color: #333;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    
}

.navbar li a:hover, .dropdown:hover .dropbtn {
    background-color: #333;
	color: #f2f2f2;
}

.navbar li.dropdown {
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    text-align: left;
}

.dropdown-content a:hover {background-color: #333}

.navbar .show {display:block;}

.xavierslogo {
 width: 110px;
 height: 109px;
 margin-left: 20%;
 
}

.xavierstext {
 width: 100;
 height: 100;
 margin-left: 0%;
 
}

.topbg {
 
       background-color: #333;
 }
<header class="topbg">
<img src="xavierslogo.png" alt="St. Xavier's college logo" class="xavierslogo">
<img src="xavierstext.png" alt="St. Xavier's college logo" class="xavierstext">
</header>
<div>
<ul class="navbar">
  <li><a  href="#home">Home</a></li>
  <li><a href="#news">Text one</a></li>
   <li><a href="#news">Text Two</a></li>
  <li class="dropdown">
    <a href="javascript:void(0)" class="dropbtn" onclick="myFunction()">Dropdown ▼</a>
    <div class="dropdown-content" id="myDropdown">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </li>

</ul>
  </div>

<div class="main">


z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>

</div>