common.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. $(function() {
  2. 'use strict';
  3. window.getLocale = function () {
  4. if (!localStorage) {
  5. return 'en';
  6. }
  7. if (location.search === '?locale=zh') {
  8. localStorage.locale = 'zh';
  9. }
  10. return localStorage.locale;
  11. };
  12. function main() {
  13. $(window).scroll(showGotoTop);
  14. $(window).resize(showGotoTop);
  15. $('.goto-top').click(function() {
  16. document.body.scrollTop = 0;
  17. document.documentElement.scrollTop = 0;
  18. return false;
  19. });
  20. initLocale();
  21. initScrollspy();
  22. showGotoTop();
  23. $('#bulletin').bulletin();
  24. }
  25. function initLocale() {
  26. var $locale = $('#locale');
  27. if (!localStorage) {
  28. $locale.hide();
  29. return;
  30. }
  31. if (getLocale() === 'zh') {
  32. $locale.find('.language').text('简体中文').end()
  33. .find('[data-locale="en"]').removeClass('active').end()
  34. .find('[data-locale="zh"]').addClass('active').end()
  35. .find('.dropdown-toggle img').removeClass('flag-en').end()
  36. .find('.dropdown-toggle img').addClass('flag-zh').end();
  37. $('[data-zh]').each(function () {
  38. $(this).html($(this).data('zh'));
  39. });
  40. }
  41. $('[data-locale]').click(function () {
  42. localStorage.locale = $(this).data('locale');
  43. location.reload(true);
  44. });
  45. }
  46. function initScrollspy() {
  47. var $window = $(window),
  48. $body = $(document.body),
  49. html = [];
  50. $('.page-header').find('h1, h2').each(function (i) {
  51. var $this = $(this),
  52. parent = $this.is('h1'),
  53. link = '<a href="#' + $this.attr('id') + '">' + $.trim($this.text()) + '</a>';
  54. if (parent) {
  55. if (i > 0) {
  56. html.push('</ul></li>');
  57. }
  58. html.push('<li>', link, '<ul class="nav">');
  59. } else {
  60. html.push('<li>', link, '</li>');
  61. }
  62. });
  63. html.push('</ul></li>');
  64. $('.bs-sidenav').html(html.join(''));
  65. $body.scrollspy({
  66. target: '.bs-sidebar',
  67. offset: $('.navbar').outerHeight(true) + 10
  68. });
  69. $body.scrollspy('refresh');
  70. // affix
  71. setTimeout(function () {
  72. var $sideBar = $('.bs-sidebar');
  73. $sideBar.affix({
  74. offset: {
  75. top: function () {
  76. var offsetTop = $sideBar.offset().top;
  77. var sideBarMargin = parseInt($sideBar.children(0).css('margin-top'), 10);
  78. var navOuterHeight = $('.bs-docs-nav').height();
  79. return (this.top = offsetTop - navOuterHeight - sideBarMargin);
  80. },
  81. bottom: function () {
  82. return (this.bottom = $('.bs-footer').outerHeight(true));
  83. }
  84. }
  85. });
  86. }, 100);
  87. }
  88. function showGotoTop() {
  89. var $gotoTop = $('.goto-top'),
  90. $bdshare = $('#bdshare');
  91. if ($(document).scrollTop() > 0) {
  92. $gotoTop.fadeIn('slow');
  93. $bdshare.fadeOut('slow');
  94. } else {
  95. $gotoTop.fadeOut('slow');
  96. $bdshare.fadeIn('slow');
  97. }
  98. }
  99. main();
  100. });