我正在建立一个网站,当他处于移动模式(> 767px)时会看到一个导航栏。我已经知道了,但我希望这个导航栏只出现在第1部分之后。
默认情况下,导航栏始终显示。我希望它只在我看到第2节时出现。
请参阅以下示例:
示例:http://jsfiddle.net/gtw7375/3zc5Ltzp/
HTML:
<nav class="navbar navbar-default navbar-fixed-bottom visible-xs">
<div class="container-fluid">
<div class="navbar-header">
<!-- <a class="navbar-brand" href="#">LOGO PSTCH</a> </div> -->
<div class="collapse navbar-collapse visible-xs" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="#" data-toggle="modal" data-target="#myModal1" data-direction="bottom"> About</a></li>
<li><a href="#" data-toggle="modal" data-target="#myModal2" data-direction='bottom'> Sobre </a></li>
<li><a href="#" data-toggle="modal" data-target="#myModal3" data-direction='bottom'> Contact </a></li>
</ul>
</div>
</div>
</div>
</nav>
<div id="logo">
<center>
<a href="#desce" class="page-scroll btn btn-xl">SECTION 1</a>
</center>
</div>
<div id="content">
<p> SECTION 2 </p>
<p> The navbar will appear here down/hereafter!</p>
</div>
CSS:
html, body {
height:100%;
padding:0;
margin:0;
}
#logo{
background: black;
width: 100%;
height: 100%;
}
#content {
border: 1px solid black;
width: 100%;
height: 50%;
}
.navbar .nav li{
float:none;
display:inline-block;
*display:inline; /* Internet Explorer 7 compatibility */
*zoom:1;
vertical-align: bottom;
}
.navbar .navbar-collapse {
text-align: center;
}
我该怎么做?
答案 0 :(得分:2)
如果您使用JQuery,则可以收听窗口的滚动事件,检查它是否已滚动超出元素的顶部偏移并相应地采取措施。以下是使用JQuery插件的Javascript代码。
$(document).ready(function(){
$(".navbar").hide(); //Hide the navigation bar first
$(window).scroll(function () { //Listen for the window's scroll event
if (isScrolledAfterElement("#content")) { //if it has scrolled beyond the #content elment
$('.navbar').fadeIn(); //Show the navigation bar
} else {
$('.navbar').fadeOut(); //Else hide it
}
});
//Function that returns true if the window has scrolled beyond the given element
function isScrolledAfterElement(elem) {
var $elem = $(elem);
var $window = $(window);
var docViewTop = $window.scrollTop();
var docViewBottom = docViewTop + $window.height();
var elemTop = $elem.offset().top;
return elemTop <= docViewBottom;
}
});
您可以在此jsfiddle中查看此操作。我从导航栏中删除了 visible-xs 类,并在 #logo 元素中添加了 margin-bottom 属性,以使效果显着对所有用户(您不必为您的项目执行此操作)。
答案 1 :(得分:0)
我真的不明白你想用你的代码做什么。
<div id="box01">
<p>This will be hidden, only visible after 767px.</p>
</div>
.box01 {
display: none;
}
@media screen and (max-width : 767px) {
.box01 {
display: block;
}
}