common.js 2.9 KB

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