common.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. initScrollspy();
  12. showGotoTop();
  13. showBaiduShare();
  14. $('#bulletin').bulletin();
  15. }
  16. function initScrollspy() {
  17. var $window = $(window);
  18. var $body = $(document.body);
  19. var navHeight = $('.navbar').outerHeight(true) + 10;
  20. $body.scrollspy({
  21. target: '.bs-sidebar',
  22. offset: navHeight
  23. });
  24. $body.scrollspy('refresh');
  25. // affix
  26. setTimeout(function () {
  27. var $sideBar = $('.bs-sidebar');
  28. $sideBar.affix({
  29. offset: {
  30. top: function () {
  31. var offsetTop = $sideBar.offset().top;
  32. var sideBarMargin = parseInt($sideBar.children(0).css('margin-top'), 10);
  33. var navOuterHeight = $('.bs-docs-nav').height();
  34. return (this.top = offsetTop - navOuterHeight - sideBarMargin);
  35. },
  36. bottom: function () {
  37. return (this.bottom = $('.bs-footer').outerHeight(true));
  38. }
  39. }
  40. });
  41. }, 100);
  42. }
  43. function showGotoTop() {
  44. var $gotoTop = $('.goto-top'),
  45. $bdshare = $('#bdshare');
  46. if ($(document).scrollTop() > 0) {
  47. $gotoTop.fadeIn('slow');
  48. $bdshare.fadeOut('slow');
  49. } else {
  50. $gotoTop.fadeOut('slow');
  51. $bdshare.fadeIn('slow');
  52. }
  53. }
  54. function showBaiduShare() {
  55. $('#bdshell_js').attr('src', 'http://bdimg.share.baidu.com/static/js/shell_v2.js?cdnversion=" + Math.ceil(new Date()/3600000');
  56. }
  57. main();
  58. });