animation.js 4.1 KB

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