layout.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. $(document).ready(function () {
  2. var AFFIX_TOP_LIMIT = 300;
  3. var AFFIX_OFFSET = 49;
  4. $('#menu-left').localScroll({hash:true, onAfterFirst:function(){$('html, body').scrollTo( {top:'-=25px'}, 'fast' );}});
  5. var $menu = $("#menu"),
  6. $btn = $("#menu-toggle");
  7. $("#menu-toggle").on("click", function () {
  8. $menu.toggleClass("open");
  9. return false;
  10. });
  11. $(".docs-nav").each(function () {
  12. var $affixNav = $(this),
  13. $container = $affixNav.parent(),
  14. affixNavfixed = false,
  15. originalClassName = this.className,
  16. current = null,
  17. $links = $affixNav.find("a");
  18. function getClosestHeader(top) {
  19. var last = $links.first();
  20. if (top < AFFIX_TOP_LIMIT) {
  21. return last;
  22. }
  23. for (var i = 0; i < $links.length; i++) {
  24. var $link = $links.eq(i),
  25. href = $link.attr("href");
  26. if (href.charAt(0) === "#" && href.length > 1) {
  27. var $anchor = $(href).first();
  28. if ($anchor.length > 0) {
  29. var offset = $anchor.offset();
  30. if (top < offset.top - AFFIX_OFFSET) {
  31. return last;
  32. }
  33. last = $link;
  34. }
  35. }
  36. }
  37. return last;
  38. }
  39. $(window).on("scroll", function (evt) {
  40. var top = window.scrollY,
  41. height = $affixNav.outerHeight(),
  42. max_bottom = $container.offset().top + $container.outerHeight(),
  43. bottom = top + height + AFFIX_OFFSET;
  44. if (affixNavfixed) {
  45. if (top <= AFFIX_TOP_LIMIT) {
  46. $affixNav.removeClass("fixed");
  47. $affixNav.css("top", 0);
  48. affixNavfixed = false;
  49. } else if (bottom > max_bottom) {
  50. $affixNav.css("top", (max_bottom - height) - top);
  51. } else {
  52. $affixNav.css("top", AFFIX_OFFSET);
  53. }
  54. } else if (top > AFFIX_TOP_LIMIT) {
  55. $affixNav.addClass("fixed");
  56. affixNavfixed = true;
  57. }
  58. var $current = getClosestHeader(top);
  59. if (current !== $current) {
  60. $affixNav.find(".active").removeClass("active");
  61. $current.addClass("active");
  62. current = $current;
  63. }
  64. });
  65. });
  66. prettyPrint();
  67. });