2017-03-03 120 views
-1

我想製作一個粘貼標題的網頁,就像在stackoverflow本身的標題。當我們滾動頁面時,頁眉仍然處於其位置。我怎樣才能做到這一點?如何添加粘貼標題

+1

[置頂頭 - 滾動 - CSS/jQuery的]的可能的複製(HTTP://計算器。 COM /問題/ 8214380 /粘集管渦旋CSS-jquery的) – bpeterson76

回答

3

這可以通過css屬性position: fixed完成。

1
<nav class="navbar navbar-default navbar-fixed-top"> 

如果您使用的是引導程序,那麼您可以使用.navbar-fixed-top類來解決問題。

1

這可能是你正在尋找的!

注:Jqueryjavascript僅用於滾動效果,而「只是堅持」效應可以通過只使用HTMLCSS來實現。

var mn = $(".main-nav"); 
 
mns = "main-nav-scrolled"; 
 
hdr = $('header').height(); 
 

 
$(window).scroll(function() { 
 
    if($(this).scrollTop() > hdr) { 
 
     mn.addClass(mns); 
 
    } else { 
 
     mn.removeClass(mns); 
 
    } 
 
});
*{ 
 
    box-sizing: border-box; 
 
} 
 
body { 
 
    margin: 0; 
 
    padding-top: 250px; 
 
} 
 
header { 
 
    height: 300px; 
 
    padding-top: 50px; 
 
    background: #f07057; 
 
} 
 
.main-nav, 
 
.main { 
 
    position: relative; 
 
} 
 
.main-nav { 
 
    background: #fff; 
 
    height: 80px; 
 
    z-index: 150; 
 
    margin-bottom: -80px; 
 
    box-shadow: 0 2px 3px rgba(0,0,0,.4); 
 
} 
 
header, 
 
.main-nav-scrolled { 
 
    position: fixed; 
 
    width: 100%; 
 
    top: 0; 
 
} 
 
.main { 
 
    background: #f2f2e8; 
 
    padding: 110px 50px 50px; 
 
    column-count: 2; 
 
    column-gap: 40px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<header> 
 
    <h1>This is a Sticky Nav</h1> 
 
</header> 
 
<nav class="main-nav"> 
 
    <a href="#">Nav Link 1</a> 
 
    <a href="#">Nav Link 2</a> 
 
    <a href="#">Nav Link 3</a> 
 
    <a href="#">Nav Link 4</a> 
 
    </nav> 
 
    <div class="main"> 
 
    <h2>Lorem ipsum dolor sit amet</h2> 
 
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean fringilla, purus in porttitor pellentesque, erat arcu tincidunt diam, dapibus faucibus leo mauris at sapien. In porttitor vehicula sodales. Vivamus massa neque, facilisis eu felis ut, aliquet convallis nisi. Nam elementum tellus vitae gravida fermentum. Nullam et imperdiet leo. Integer ut euismod lorem, in placerat lacus. Curabitur bibendum arcu ut feugiat commodo. Suspendisse ut mi vel orci ullamcorper tincidunt. Nam vitae fringilla nibh. Nullam hendrerit blandit velit eu hendrerit.</p> 
 

 
    <p>Praesent eu enim non massa pellentesque lobortis. In in sagittis dolor. Aliquam non massa erat. Ut aliquet gravida tellus, sed volutpat nibh condimentum et. Nunc quam purus, vehicula quis venenatis et, porttitor vel dolor. Cras facilisis dui id elit bibendum, in ullamcorper leo ultricies. Praesent rutrum lacus sit amet sem convallis, ut interdum dolor vestibulum.</p> 
 

 
    <p>Tum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. In iaculis pharetra odio, sit amet consectetur elit facilisis ac. Praesent eget tristique ipsum. Donec malesuada bibendum lacinia. Praesent non purus sodales, pulvinar mi vitae, tincidunt leo. Phasellus vitae elit ut nisl semper fringilla id rutrum dolor. Donec a massa adipiscing, cursus risus vitae, porttitor tortor. Nullam sagittis est sapien, sit amet pharetra turpis imperdiet vel. Etiam sit amet ligula pretium, vulputate eros ac, bibendum velit. Aenean convallis ante purus, ac bibendum orci laoreet ac. Donec a convallis mauris. Nulla non lacus non ipsum pretium tempor. In hac habitasse platea dictumst. Aenean at ipsum vulputate, sagittis dui ut, pharetra neque. Nam eget sodales orci. Aliquam pharetra nunc at nisl pellentesque, nec fringilla enim iaculis.</p> 
 
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean fringilla, purus in porttitor pellentesque, erat arcu tincidunt diam, dapibus faucibus leo mauris at sapien. In porttitor vehicula sodales. Vivamus massa neque, facilisis eu felis ut, aliquet convallis nisi. Nam elementum tellus vitae gravida fermentum. Nullam et imperdiet leo. Integer ut euismod lorem, in placerat lacus. Curabitur bibendum arcu ut feugiat commodo. Suspendisse ut mi vel orci ullamcorper tincidunt. Nam vitae fringilla nibh. Nullam hendrerit blandit velit eu hendrerit.</p> 
 

 
    <p>Praesent eu enim non massa pellentesque lobortis. In in sagittis dolor. Aliquam non massa erat. Ut aliquet gravida tellus, sed volutpat nibh condimentum et. Nunc quam purus, vehicula quis venenatis et, porttitor vel dolor. Cras facilisis dui id elit bibendum, in ullamcorper leo ultricies. Praesent rutrum lacus sit amet sem convallis, ut interdum dolor vestibulum.</p> 
 

 
    <p>Tum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. In iaculis pharetra odio, sit amet consectetur elit facilisis ac. Praesent eget tristique ipsum. Donec malesuada bibendum lacinia. Praesent non purus sodales, pulvinar mi vitae, tincidunt leo. Phasellus vitae elit ut nisl semper fringilla id rutrum dolor. Donec a massa adipiscing, cursus risus vitae, porttitor tortor. Nullam sagittis est sapien, sit amet pharetra turpis imperdiet vel. Etiam sit amet ligula pretium, vulputate eros ac, bibendum velit. Aenean convallis ante purus, ac bibendum orci laoreet ac. Donec a convallis mauris. Nulla non lacus non ipsum pretium tempor. In hac habitasse platea dictumst. Aenean at ipsum vulputate, sagittis dui ut, pharetra neque. Nam eget sodales orci. Aliquam pharetra nunc at nisl pellentesque, nec fringilla enim iaculis.</p> 
 
</div>

0

只需HTML & CSS

body {margin:0;} 
 

 
ul { 
 
    list-style-type: none; 
 
    margin: 0; 
 
    padding: 0; 
 
    overflow: hidden; 
 
    background-color: #333; 
 
    position: fixed; 
 
    top: 0; 
 
    width: 100%; 
 
} 
 

 
li { 
 
    float: left; 
 
} 
 

 
li a { 
 
    display: block; 
 
    color: white; 
 
    padding: 16px; 
 
    text-decoration: none; 
 
} 
 

 
.main { 
 
    padding: 16px; 
 
    margin-top: 30px; 
 
    height: 1500px; /* Used in this example to enable scrolling */ 
 
}
<ul> 
 
    <li><a href="#home">Home</a> 
 
    </li> 
 
    <li><a href="#news">News</a> 
 
    </li> 
 
    <li><a href="#contact">Contact</a> 
 
    </li> 
 
</ul> 
 

 
<div class="main"> 
 
    <h1>Fixed Top Menu</h1> 
 
    <h2>Scroll this page to see the effect</h2> 
 
    <h2>The navigation bar will stay at the top of the page while scrolling</h2> 
 

 
    <p>Some text some text some text some text..</p> 
 
    <p>Some text some text some text some text..</p> 
 
    <p>Some text some text some text some text..</p> 
 
    <p>Some text some text some text some text..</p> 
 
    <p>Some text some text some text some text..</p> 
 
    <p>Some text some text some text some text..</p> 
 
    <p>Some text some text some text some text..</p> 
 
    <p>Some text some text some text some text..</p> 
 
    <p>Some text some text some text some text..</p> 
 
    <p>Some text some text some text some text..</p> 
 
    <p>Some text some text some text some text..</p> 
 
    <p>Some text some text some text some text..</p> 
 
    <p>Some text some text some text some text..</p> 
 
    <p>Some text some text some text some text..</p> 
 
    <p>Some text some text some text some text..</p> 
 
    <p>Some text some text some text some text..</p> 
 
    <p>Some text some text some text some text..</p> 
 
    <p>Some text some text some text some text..</p> 
 
    <p>Some text some text some text some text..</p> 
 
    <p>Some text some text some text some text..</p> 
 
</div>