base.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* Prettyify */
  2. $(document).ready(function () {
  3. prettyPrint();
  4. });
  5. /* Scrollspy */
  6. var navHeight = $('.navbar').outerHeight(true) + 10
  7. $('body').scrollspy({
  8. target: '.bs-sidebar',
  9. offset: navHeight
  10. })
  11. /* Prevent disabled links from causing a page reload */
  12. $("li.disabled a").click(function () {
  13. event.preventDefault();
  14. });
  15. /* Adjust the scroll height of anchors to compensate for the fixed navbar */
  16. window.disableShift = false;
  17. var shiftWindow = function () {
  18. if (window.disableShift) {
  19. window.disableShift = false;
  20. } else {
  21. /* If we're at the bottom of the page, don't erronously scroll up */
  22. var scrolledToBottomOfPage = (
  23. (window.innerHeight + window.scrollY) >= document.body.offsetHeight
  24. );
  25. if (!scrolledToBottomOfPage) {
  26. scrollBy(0, -60);
  27. };
  28. };
  29. };
  30. if (location.hash) { shiftWindow(); }
  31. window.addEventListener("hashchange", shiftWindow);
  32. /* Deal with clicks on nav links that do not change the current anchor link. */
  33. $("ul.nav a").click(function () {
  34. var href = this.href;
  35. var suffix = location.hash;
  36. var matchesCurrentHash = (href.indexOf(suffix, href.length - suffix.length) !== -1);
  37. if (location.hash && matchesCurrentHash) {
  38. /* Force a single 'hashchange' event to occur after the click event */
  39. window.disableShift = true;
  40. location.hash = '';
  41. };
  42. });
  43. $(document).bind('DOMNodeInserted',
  44. function (e) {
  45. if (e.target.id === 'carbonads') {
  46. $('.bs-sidebar').affix({
  47. offset: {
  48. top: $('#carbonads').height() + 10
  49. }
  50. });
  51. }
  52. });