common.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. showBaiduShare();
  24. $('#bulletin').bulletin();
  25. }
  26. function initLocale() {
  27. var $locale = $('#locale');
  28. if (!localStorage) {
  29. $locale.hide();
  30. return;
  31. }
  32. if (getLocale() === 'zh') {
  33. $locale.find('.language').text('简体中文').end()
  34. .find('[data-locale="en"]').removeClass('active').end()
  35. .find('[data-locale="zh"]').addClass('active').end()
  36. .find('.dropdown-toggle img').removeClass('flag-en').end()
  37. .find('.dropdown-toggle img').addClass('flag-zh').end();
  38. $('[data-zh]').each(function () {
  39. $(this).html($(this).data('zh'));
  40. });
  41. }
  42. $('[data-locale]').click(function () {
  43. localStorage.locale = $(this).data('locale');
  44. location.reload(true);
  45. });
  46. }
  47. function initScrollspy() {
  48. var $window = $(window),
  49. $body = $(document.body),
  50. html = [];
  51. $('.page-header').find('h1, h2').each(function (i) {
  52. var $this = $(this),
  53. parent = $this.is('h1'),
  54. link = '<a href="#' + $this.attr('id') + '">' + $.trim($this.text()) + '</a>';
  55. if (parent) {
  56. if (i > 0) {
  57. html.push('</ul></li>');
  58. }
  59. html.push('<li>', link, '<ul class="nav">');
  60. } else {
  61. html.push('<li>', link, '</li>');
  62. }
  63. });
  64. html.push('</ul></li>');
  65. $('.bs-sidenav').html(html.join(''));
  66. $body.scrollspy({
  67. target: '.bs-sidebar',
  68. offset: $('.navbar').outerHeight(true) + 10
  69. });
  70. $body.scrollspy('refresh');
  71. // affix
  72. setTimeout(function () {
  73. var $sideBar = $('.bs-sidebar');
  74. $sideBar.affix({
  75. offset: {
  76. top: function () {
  77. var offsetTop = $sideBar.offset().top;
  78. var sideBarMargin = parseInt($sideBar.children(0).css('margin-top'), 10);
  79. var navOuterHeight = $('.bs-docs-nav').height();
  80. return (this.top = offsetTop - navOuterHeight - sideBarMargin);
  81. },
  82. bottom: function () {
  83. return (this.bottom = $('.bs-footer').outerHeight(true));
  84. }
  85. }
  86. });
  87. }, 100);
  88. }
  89. function showGotoTop() {
  90. var $gotoTop = $('.goto-top'),
  91. $bdshare = $('#bdshare');
  92. if ($(document).scrollTop() > 0) {
  93. $gotoTop.fadeIn('slow');
  94. $bdshare.fadeOut('slow');
  95. } else {
  96. $gotoTop.fadeOut('slow');
  97. $bdshare.fadeIn('slow');
  98. }
  99. }
  100. function showBaiduShare() {
  101. $('#bdshell_js').attr('src', 'http://bdimg.share.baidu.com/static/js/shell_v2.js?cdnversion=" + Math.ceil(new Date()/3600000');
  102. }
  103. main();
  104. });