我想在bootstrap的导航菜单上创建一个scrollspy效果,页面顶部有一个轮播,导航菜单位于轮播的底部。因此,当某人向下滚动时,在他通过导航菜单后,导航菜单将具有导航栏固定顶级类,并且查看器将看到菜单。 我今天在这个问题上工作,但我找不到解决方案来解决这个问题。 这是我的HTML代码:
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap 101 Template</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,700' rel='stylesheet' type='text/css'>
<script src="https://code.jquery.com/jquery.js"></script>
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
</head>
<body class="blog">
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner">
<div class="item active">
<img src="images/3.jpg" alt="slide1" style="width:100%">
<div class="carousel-caption">
<h3>What therefore god has joined together, Let no man seperate.</h3>
<p>– The Bible : Mark 10:9 –</p>
</div>
</div>
<div class="item">
<img src="images/4.jpg" alt="slide2" style="width:100%">
<div class="carousel-caption">
<h3>Lorem Ipsum Header Dolor Sit Amet</h3>
<p>– John Doe –</p>
</div>
</div>
<div class="item">
<img src="images/5.jpg" alt="slide3" style="width:100%">
<div class="carousel-caption">
<h3>Header Slider 3 Caption Text</h3>
<p>– Holy Themeforest : Wordpress Category –</p>
</div>
</div>
</div>
<!-- Controls -->
<a class="left carousel-control" href="#carousel-example-generic" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
</div>
<header>
<nav class="navbar navbar-default" role="navigation">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Brand</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li class="active"><a href="#">Our Story</a></li>
<li><a href="#">Time</a></li>
<li><a href="#">Events</a></li>
<li><a href="#">Photos</a></li>
<li><a href="#">Gifts</a></li>
<li><a href="#">From the Blog</a></li>
<li><a href="#">RSVP</a></li>
</ul>
</div><!-- /.navbar-collapse -->
</nav>
</header>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p><p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="js/functions.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
</body>
</html>
和functions.js:
// =============== Menu setup =============== //
// checks if menu should be fixed
function setup_menu(){
if( !jQuery('body').hasClass('blog') && !jQuery('body').hasClass('single') && !jQuery('body').hasClass('archive') && !jQuery('body').hasClass('search') ){
if(!$menu) $menu = jQuery(".navbar");
if ( jQuery(window).scrollTop() > 200 && !$menu.hasClass("navbar-fixed-top") ){
$menu.addClass("navbar-fixed-top");
} else if(jQuery(window).scrollTop() <= 200 && $menu.hasClass("navbar-fixed-top")) {
$menu.removeClass("navbar-fixed-top");
}
}
// =============== Menu setup END =============== //
另外,我想创建一个用于滚动到部分的scrollspy效果,如下所示:http://bit.ly/1hrjDZB我该怎么做?
答案 0 :(得分:2)
1)在导航元素中添加ID
<nav id="yourID" class="navbar navbar-default" role="navigation">
2)将以下脚本添加到您的站点。
<script>
function stickyNav() {
if (window.pageYOffset > 20) {
var x = document.getElementById('yourID');
x.className = "navbar navbar-default navbar-fixed-top";
}
else if (window.pageYOffset <= 20) {
var x = document.getElementById('navigate');
x.className = "navbar navbar-default";
}
}
window.onload = stickyNav;
</script>
3)在你的身体元素上使用onscroll事件调用该函数。
<body onscroll="stickyNav(this)">
4)将window.pageYOffset的值调整为需要。
*注意:这并没有考虑到您的其他功能&#39; if / then语句。根据您的需要调整上述脚本。