如果你不想一切從引導這裏是什麼你需要做的
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'});
});
你檢查了Bootstrap嗎?我沒有使用它,但我打算,它聽起來像它做了一些類似的東西 - http://twitter.github.com/bootstrap/javascript.html#affix - 看看左邊的siode菜單,以及它是如何根據修改你的頁面 – 2013-04-04 12:07:34
這之後是的,我的作用在哪裏,我只是認爲這可能與代碼 – ChrisBull 2013-04-04 12:10:08
幾行我想這是可以做到的,我只是懶惰。 Bootstraps也有很多其他方便的東西,只是一個總是意識到我認爲:) – 2013-04-04 12:36:15