Skip to content

Instantly share code, notes, and snippets.

@selfboot
Last active August 27, 2016 01:19
Show Gist options
  • Select an option

  • Save selfboot/a3a6fa86b4835da4b78a6039f77c0216 to your computer and use it in GitHub Desktop.

Select an option

Save selfboot/a3a6fa86b4835da4b78a6039f77c0216 to your computer and use it in GitHub Desktop.
目录滑动悬浮
$(function() {
var fix = $('#toc'); //滚动悬浮块
var fixTop = fix.offset().top, //滚动悬浮块与顶部的距离
fixHeight = fix.height(); //滚动悬浮块高度
$(window).scroll(function() {
$(":header").each(function() {
var id = $(this).attr('id');
// 跳过标题
if (typeof id != 'undefined'){
var margin_height = parseInt($(this).css('marginTop'));
if($(window).scrollTop() >= $(this).offset().top-margin_height) {
$('a.toc-link').removeClass('active');
$('a.toc-link[href="#' + id + '"]').addClass('active');
}
}
});
//页面与顶部高度
var docTop = Math.max(document.body.scrollTop, document.documentElement.scrollTop);
if (fixTop < docTop) {
fix.css({'position': 'fixed'});
fix.css({top: 0}); //滚动悬浮块未到结束块上时,top为0
} else {
fix.css({'position': 'static'});
}
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment