有没有人知道如何使用scrollspy而不使用bootstrap?我正在尝试使用此存储库在我的一个项目中使用它:
https://github.com/sxalexander/jquery-scrollspy
但它只是不做引导程序所做的事情.在李标签未标记为活动:(任何帮助将不胜感激.
我试过这样做:
$('#intel_nav').scrollspy({
//n: $('#nav').offset().top,
onEnter: function (element, position) {
console.log(element);
$("#intel_nav").addClass('moo');
},
onLeave: function (element, position) {
$("#intel_nav").removeClass('out');
}
});
Run Code Online (Sandbox Code Playgroud)
该元素似乎是实际的菜单,所以我不知道如何实际获取我当前悬停的元素的id.
如果有人仍然对此感兴趣,我无法让bootstrap scrollspy足够快地工作,所以我编写了自己的(技术上效率低下但很简单)解决方案.
这是一个演示:
$(window).bind('scroll', function() {
var currentTop = $(window).scrollTop();
var elems = $('.scrollspy');
elems.each(function(index){
var elemTop = $(this).offset().top;
var elemBottom = elemTop + $(this).height();
if(currentTop >= elemTop && currentTop <= elemBottom){
var id = $(this).attr('id');
var navElem = $('a[href="#' + id+ '"]');
navElem.parent().addClass('active').siblings().removeClass( 'active' );
}
})
}); Run Code Online (Sandbox Code Playgroud)
.active{
color: red;
background-color: red;
}
#nav{
position:fixed;
top:0;
right:50%;
}
section{
min-height: 500px;
}Run Code Online (Sandbox Code Playgroud)
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<nav id="nav" class="navbar navbar-template">
<div class="row col-xs-12 text-center">
<ul>
<li class="active">
<a href="#Home">Home</a>
</li>
<li>
<a href="#AboutUs">AboutUs</a>
</li>
<li>
<a href="#Images">Images</a>
</li>
<li>
<a href="#Contact">Contact</a>
</li>
</ul>
</div>
</nav>
<section class="scrollspy" id="Home">
Home
</section>
<section class="scrollspy" id="AboutUs">
AboutUs
</section>
<section class="scrollspy" id="Images">
Images
</section>
<section class="scrollspy" id="Contact">
Contact
</section>
</body>Run Code Online (Sandbox Code Playgroud)