如果您不想要 bootstrap 中的所有内容,这就是您需要做的
http://jsfiddle.net/6t83p/1/花了一些时间,但它总是很有趣:)
$('.selectedNav').css( 'background-color', 'white');
$('.selectedNav').css( 'border-right', 'none');
$('.servicesNavItem').click(function () {
$(this).parent().css( 'background-color', 'white');
$(this).parent().css( 'border-right', 'none');
$('.selectedNav').css( 'background-color', '#eeeeee');
$('.selectedNav').css( 'border-right', '1px solid #c7c7c7');
$('.selectedNav').toggleClass("selectedNav");
$(this).parent().toggleClass("selectedNav");
});
var y = $('.servicesLeft').offset().top - 50;
$(window).scroll( function() {
if ($(window).scrollTop() > y)
$('.servicesLeft').addClass('floating');
else
$('.servicesLeft').removeClass('floating');
});
$('#sideNav li a').click(function(){
$('html, body').animate({
scrollTop: $( $.attr(this, 'href') ).offset().top
}, 500);
return false;
});
/***** ADDED THE FOLLOWING CODE ******/
// get positions of all linked href
var x = {};
$('#sideNav a').each(function(){
var href = $(this).attr('href').replace('#','');
x[href] = $('#'+href).offset().top;
});
$(document).on('scroll', function(){
var current_top = window.pageYOffset;
console.log(current_top);
var active = '';
var min_value = 9999999;
$.each(x, function(index, value) {
// some big value
if(value >= current_top && value < min_value)
{
console.log(min_value);
min_value = value;
active = index;
}
});
$('#sideNav li').removeAttr('style');
$('#sideNav a[href="#'+active+'"]').parent('li').css({'background-color': 'white', 'border-right-style': 'none'});
});