animation.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Animations
  2. var animationBar = document.getElementById("animationBar");
  3. var dropdownBtn = document.getElementById("dropdownBtn");
  4. var chevronUp = document.getElementById("chevronUp");
  5. var chevronDown = document.getElementById("chevronDown");
  6. var dropdownLabel = document.getElementById("dropdownLabel");
  7. var dropdownContent = document.getElementById("dropdownContent");
  8. var playBtn = document.getElementById("playBtn");
  9. var slider = document.getElementById("slider");
  10. var clickInterceptor = document.getElementById("click-interceptor");
  11. clickInterceptor.addEventListener("mousedown", function() {
  12. displayDropdownContent(false);
  13. displayDropdownContentEnv(false);
  14. });
  15. // event on the dropdown
  16. function formatId(name) {
  17. return "data-" + name.replace(/\s/g, '');
  18. }
  19. function displayDropdownContent(display) {
  20. if (display) {
  21. dropdownContent.style.display = "block";
  22. chevronDown.style.display = "inline";
  23. chevronUp.style.display = "none";
  24. dropdownBtn.classList.add("open");
  25. clickInterceptor.classList.remove("hidden");
  26. }
  27. else {
  28. dropdownContent.style.display = "none";
  29. chevronDown.style.display = "none";
  30. chevronUp.style.display = "inline";
  31. dropdownBtn.classList.remove("open");
  32. clickInterceptor.classList.add("hidden");
  33. }
  34. }
  35. dropdownBtn.addEventListener("click", function() {
  36. if (dropdownContent.style.display === "block") {
  37. displayDropdownContent(false);
  38. }
  39. else {
  40. displayDropdownContent(true);
  41. }
  42. });
  43. function selectCurrentGroup(group, index, animation) {
  44. if (currentGroupIndex !== undefined) {
  45. document.getElementById(formatId(currentGroup.name + "-" + currentGroupIndex)).classList.remove("active");
  46. }
  47. playBtn.classList.remove("play");
  48. playBtn.classList.add("pause");
  49. // start the new animation group
  50. currentGroup = group;
  51. currentGroupIndex = index;
  52. animation.classList.add("active");
  53. dropdownLabel.innerHTML = currentGroup.name;
  54. dropdownLabel.title = currentGroup.name;
  55. // set the slider
  56. slider.setAttribute("min", currentGroup.from);
  57. slider.setAttribute("max", currentGroup.to);
  58. currentSliderValue = currentGroup.from;
  59. slider.value = currentGroup.from;
  60. }
  61. function createDropdownLink(group, index) {
  62. var animation = document.createElement("a");
  63. animation.innerHTML = group.name;
  64. animation.title = group.name;
  65. animation.setAttribute("id", formatId(group.name + "-" + index));
  66. animation.addEventListener("click", function() {
  67. // stop the current animation group
  68. currentGroup.reset();
  69. currentGroup.stop();
  70. group.play(true);
  71. // hide the content of the dropdown
  72. displayDropdownContent(false);
  73. });
  74. dropdownContent.appendChild(animation);
  75. group.onAnimationGroupPlayObservable.add(function(grp) {
  76. selectCurrentGroup(grp, index, animation);
  77. });
  78. group.onAnimationGroupPauseObservable.add(function(grp) {
  79. playBtn.classList.add("play");
  80. playBtn.classList.remove("pause");
  81. });
  82. }
  83. // event on the play/pause button
  84. playBtn.addEventListener("click", function() {
  85. // click on the button to run the animation
  86. if (this.classList.contains("play")) {
  87. currentGroup.play(true);
  88. }
  89. // click on the button to pause the animation
  90. else {
  91. currentGroup.pause();
  92. }
  93. });
  94. // event on the slider
  95. slider.addEventListener("input", function() {
  96. var value = parseFloat(this.value);
  97. if (playBtn.classList.contains("play")) {
  98. currentGroup.play(true);
  99. currentGroup.goToFrame(value);
  100. currentGroup.pause();
  101. } else {
  102. currentGroup.goToFrame(value);
  103. }
  104. });
  105. var sliderPause = false;
  106. slider.addEventListener("mousedown", function() {
  107. if (playBtn.classList.contains("pause")) {
  108. sliderPause = true;
  109. playBtn.click();
  110. }
  111. });
  112. slider.addEventListener("mouseup", function() {
  113. if (sliderPause) {
  114. sliderPause = false;
  115. playBtn.click();
  116. }
  117. });