common.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. $(function() {
  2. 'use strict';
  3. function main() {
  4. $(window).scroll(showGotoTop);
  5. $(window).resize(showGotoTop);
  6. $('.goto-top').click(function() {
  7. document.body.scrollTop = 0;
  8. document.documentElement.scrollTop = 0;
  9. return false;
  10. });
  11. $('[data-zh]').each(function () {
  12. $(this).html($(this).data('zh'));
  13. });
  14. initScrollspy();
  15. showGotoTop();
  16. showBaiduShare();
  17. $('#bulletin').bulletin();
  18. }
  19. function initScrollspy() {
  20. var $window = $(window),
  21. $body = $(document.body),
  22. html = [];
  23. $('.page-header').find('h1, h2').each(function (i) {
  24. var $this = $(this),
  25. parent = $this.is('h1'),
  26. link = '<a href="#' + $this.attr('id') + '">' + $.trim($this.text()) + '</a>';
  27. if (parent) {
  28. if (i > 0) {
  29. html.push('</ul></li>');
  30. }
  31. html.push('<li>', link, '<ul class="nav">');
  32. } else {
  33. html.push('<li>', link, '</li>');
  34. }
  35. });
  36. html.push('</ul></li>');
  37. $('.bs-sidenav').html(html.join(''));
  38. $body.scrollspy({
  39. target: '.bs-sidebar',
  40. offset: $('.navbar').outerHeight(true) + 10
  41. });
  42. $body.scrollspy('refresh');
  43. // affix
  44. setTimeout(function () {
  45. var $sideBar = $('.bs-sidebar');
  46. $sideBar.affix({
  47. offset: {
  48. top: function () {
  49. var offsetTop = $sideBar.offset().top;
  50. var sideBarMargin = parseInt($sideBar.children(0).css('margin-top'), 10);
  51. var navOuterHeight = $('.bs-docs-nav').height();
  52. return (this.top = offsetTop - navOuterHeight - sideBarMargin);
  53. },
  54. bottom: function () {
  55. return (this.bottom = $('.bs-footer').outerHeight(true));
  56. }
  57. }
  58. });
  59. }, 100);
  60. }
  61. function showGotoTop() {
  62. var $gotoTop = $('.goto-top'),
  63. $bdshare = $('#bdshare');
  64. if ($(document).scrollTop() > 0) {
  65. $gotoTop.fadeIn('slow');
  66. $bdshare.fadeOut('slow');
  67. } else {
  68. $gotoTop.fadeOut('slow');
  69. $bdshare.fadeIn('slow');
  70. }
  71. }
  72. function showBaiduShare() {
  73. $('#bdshell_js').attr('src', 'http://bdimg.share.baidu.com/static/js/shell_v2.js?cdnversion=" + Math.ceil(new Date()/3600000');
  74. }
  75. main();
  76. });