base.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. });