只要加上jquery,就可以达到你想要的输出了。
<head>
<title>Nikola tesla</title>
<style type="text/css">
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
nav{
background-color: #000;
text-align: center;
padding: 8px;
}
nav li{
display: inline-block;
margin: 5px 40px 5px 40px;
padding: 3px;
font-size: 40px;
}
body{
padding: 16px;
margin: 10px;
background-color: #FFE4C4;
}
nav li a{
color: white;
text-decoration: none;
font-family: sans-serif;
}
.paragraph{
text-align:justify;
}
.top-nav-collapse {
background: white;
padding: 0;
}
</style>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script>
$(window).scroll(function() {
if ($(".navbar").offset().top > 50) {
$(".navbar-fixed-top").removeClass("top-nav-collapse");
} else {
$(".navbar-fixed-top").addClass("top-nav-collapse");
}
});
</script>
</head>
<body>
<div id="header">
<nav class="navbar navbar-fixed-top">
<ul>
<li><a href="#early">Early Life</a></li>
<li><a href="#achieve">Achievements</a></li>
<li><a href="#quote">Quotes</a></li>
</ul>
</nav>
</div>
<img src="https://s20.postimg.org/o8qt69nyl/nikolatesla.jpg" class="img-responsive .img-rounded center-block" alt="Nikola Tesla" title="Nikola Tesla">
<div class="row"><h1 id="early">Early life</h1>
<div class="col-md-8 col-lg-8 col-sm-12 col-xs-12 paragraph">
Nikola Tesla (Serbian Cyrillic: Никола Тесла; 10 July 1856 – 7 January 1943) was a Serbian American[3][4][5][6] inventor, electrical engineer, mechanical engineer, physicist, and futurist best known for his contributions to the design of the modern alternating current (AC) electricity supply system.[7]
Tesla gained experience in telephony and electrical engineering before emigrating to the United States in 1884 to work for Thomas Edison in New York City. He soon struck out on his own with financial backers, setting up laboratories and companies to develop a range of electrical devices. His patented AC induction motor and transformer were licensed by George Westinghouse, who also hired Tesla for a short time as a consultant. His work in the formative years of electric-power development was involved in a corporate alternating current/direct current "War of Currents" as well as various patent battles. He became a naturalized US citizen in 1891.[8]
Tesla went on to pursue his ideas of wireless lighting and electricity distribution in his high-voltage, high-frequency power experiments in New York and Colorado Springs and made early (1893) pronouncements on the possibility of wireless communication with his devices. He tried to put these ideas to practical use in an ill-fated attempt at intercontinental wireless transmission, his unfinished Wardenclyffe Tower project.[9] In his lab, he also conducted a range of experiments with mechanical oscillators/generators, electrical discharge tubes, and early X-ray imaging. He also built a wireless controlled boat, one of the first ever exhibited.
</div>
</div>
</body>
</html>
JQUERY 部分
<script>
$(window).scroll(function() {
if ($(".navbar").offset().top > 50) {
$(".navbar-fixed-top").removeClass("top-nav-collapse");
} else {
$(".navbar-fixed-top").addClass("top-nav-collapse");
}
});
</script>
添加的 CSS 类
.top-nav-collapse {
background: white;
padding: 0;
}
更新
试试这个
查询
<script>
$(window).scroll(function() {
if ($(".navbar").offset().top > 50) {
$(".navbar-fixed-top").removeClass("top-nav-collapse").fadeTo(50,0.8);
} else {
$(".navbar-fixed-top").addClass("top-nav-collapse").fadeTo(50,0.4);
}
});
</script>
使用fadeTo(speed,opacity)
CSS 类
.top-nav-collapse {
padding: 0;
}
您可以使用两种给定技术中的任何一种。