common.js 1.9 KB

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