utils.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * Pico's Default Theme - JavaScript helper
  3. *
  4. * Pico is a stupidly simple, blazing fast, flat file CMS.
  5. *
  6. * @author Daniel Rudolf
  7. * @link http://picocms.org
  8. * @license http://opensource.org/licenses/MIT The MIT License
  9. * @version 1.1
  10. */
  11. utils = {};
  12. /**
  13. * Checks whether the client's browser is able to slide elements or not
  14. *
  15. * @return boolean TRUE when the browser supports sliding, FALSE otherwise
  16. */
  17. utils.canSlide = function ()
  18. {
  19. return (Modernizr.classlist && Modernizr.requestanimationframe && Modernizr.csstransitions);
  20. };
  21. /**
  22. * Slides a element up (i.e. hide a element by changing its height to 0px)
  23. *
  24. * @param HTMLElement element the element to slide up
  25. * @param function finishCallback function to call when the animation has
  26. * been finished (i.e. the element is hidden)
  27. * @param function startCallback function to call when the animation starts
  28. * @return void
  29. */
  30. utils.slideUp = function (element, finishCallback, startCallback)
  31. {
  32. if (!utils.canSlide()) {
  33. if (startCallback) startCallback();
  34. element.className += (element.className !== '') ? ' hidden' : 'hidden';
  35. if (finishCallback) window.requestAnimationFrame(finishCallback);
  36. return;
  37. }
  38. element.style.height = element.clientHeight + 'px';
  39. var slideId = parseInt(element.getAttribute('data-slide-id')) || 0;
  40. element.setAttribute('data-slide-id', ++slideId);
  41. window.requestAnimationFrame(function () {
  42. element.classList.add('slide');
  43. window.requestAnimationFrame(function () {
  44. element.style.height = '0px';
  45. if (startCallback) {
  46. startCallback();
  47. }
  48. window.setTimeout(function () {
  49. if (parseInt(element.getAttribute('data-slide-id')) !== slideId) return;
  50. element.classList.add('hidden');
  51. element.classList.remove('slide');
  52. element.style.height = null;
  53. if (finishCallback) {
  54. window.requestAnimationFrame(finishCallback);
  55. }
  56. }, 500);
  57. });
  58. });
  59. };
  60. /**
  61. * Slides a element down (i.e. show a hidden element)
  62. *
  63. * @param HTMLElement element the element to slide down
  64. * @param function finishCallback function to call when the animation has
  65. * been finished (i.e. the element is visible)
  66. * @param function startCallback function to call when the animation starts
  67. * @return void
  68. */
  69. utils.slideDown = function (element, finishCallback, startCallback)
  70. {
  71. if (!utils.canSlide()) {
  72. if (startCallback) startCallback();
  73. element.className = element.className.replace(/\bhidden\b */g, '');
  74. if (finishCallback) window.requestAnimationFrame(finishCallback);
  75. return;
  76. }
  77. var cssRuleOriginalValue = element.clientHeight + 'px',
  78. slideId = parseInt(element.getAttribute('data-slide-id')) || 0;
  79. element.setAttribute('data-slide-id', ++slideId);
  80. element.style.height = null;
  81. element.classList.remove('hidden');
  82. element.classList.remove('slide');
  83. var cssRuleValue = element.clientHeight + 'px';
  84. element.style.height = cssRuleOriginalValue;
  85. window.requestAnimationFrame(function () {
  86. element.classList.add('slide');
  87. window.requestAnimationFrame(function () {
  88. element.style.height = cssRuleValue;
  89. if (startCallback) {
  90. startCallback();
  91. }
  92. window.setTimeout(function () {
  93. if (parseInt(element.getAttribute('data-slide-id')) !== slideId) return;
  94. element.classList.remove('slide');
  95. element.style.height = null;
  96. if (finishCallback) {
  97. window.requestAnimationFrame(finishCallback);
  98. }
  99. }, 500);
  100. });
  101. });
  102. };
  103. /**
  104. * Checks whether a element is visible or not
  105. *
  106. * @param HTMLElement element the element to check
  107. * @return boolean TRUE when the element is visible, FALSE otherwise
  108. */
  109. utils.isElementVisible = function (element)
  110. {
  111. return !!(element.offsetWidth || element.offsetHeight || element.getClientRects().length);
  112. };