script.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. function $() {
  2. return document.querySelector.apply(document, arguments);
  3. }
  4. if (navigator.userAgent.indexOf("Xcode") != -1) {
  5. document.documentElement.classList.add("xcode");
  6. }
  7. var jumpTo = $("#jump-to");
  8. if (jumpTo) {
  9. jumpTo.addEventListener("change", function(e) {
  10. location.hash = this.options[this.selectedIndex].value;
  11. });
  12. }
  13. function hashChanged() {
  14. if (/^#\/\/api\//.test(location.hash)) {
  15. var element = document.querySelector("a[name='" + location.hash.substring(1) + "']");
  16. if (!element) {
  17. return;
  18. }
  19. element = element.parentNode;
  20. element.classList.remove("hide");
  21. fixScrollPosition(element);
  22. }
  23. }
  24. function fixScrollPosition(element) {
  25. var scrollTop = element.offsetTop - 150;
  26. document.documentElement.scrollTop = scrollTop;
  27. document.body.scrollTop = scrollTop;
  28. }
  29. [].forEach.call(document.querySelectorAll(".section-method"), function(element) {
  30. element.classList.add("hide");
  31. element.querySelector(".method-title a").addEventListener("click", function(e) {
  32. var info = element.querySelector(".method-info"),
  33. infoContainer = element.querySelector(".method-info-container");
  34. element.classList.add("animating");
  35. info.style.height = (infoContainer.clientHeight + 40) + "px";
  36. fixScrollPosition(element);
  37. element.classList.toggle("hide");
  38. setTimeout(function() {
  39. element.classList.remove("animating");
  40. info.style.height = "auto";
  41. }, 300);
  42. });
  43. });
  44. window.addEventListener("hashchange", hashChanged);
  45. hashChanged();