Animation.coffee 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. class Animation
  2. slideDown: (elem, props) ->
  3. if (elem.offsetTop > 1000 and elem.getBoundingClientRect().top > 1000) or props.animate_disable
  4. return
  5. h = elem.offsetHeight
  6. cstyle = window.getComputedStyle(elem)
  7. margin_top = cstyle.marginTop
  8. margin_bottom = cstyle.marginBottom
  9. padding_top = cstyle.paddingTop
  10. padding_bottom = cstyle.paddingBottom
  11. transition = cstyle.transition
  12. elem.style.boxSizing = "border-box"
  13. elem.style.overflow = "hidden"
  14. elem.style.transform = "scale(0.6)"
  15. elem.style.opacity = "0"
  16. elem.style.height = "0px"
  17. elem.style.marginTop = "0px"
  18. elem.style.marginBottom = "0px"
  19. elem.style.paddingTop = "0px"
  20. elem.style.paddingBottom = "0px"
  21. elem.style.transition = "none"
  22. setTimeout (->
  23. elem.className += " animate-inout"
  24. elem.style.height = h+"px"
  25. elem.style.transform = "scale(1)"
  26. elem.style.opacity = "1"
  27. elem.style.marginTop = margin_top
  28. elem.style.marginBottom = margin_bottom
  29. elem.style.paddingTop = padding_top
  30. elem.style.paddingBottom = padding_bottom
  31. ), 1
  32. elem.addEventListener "transitionend", ->
  33. elem.classList.remove("animate-inout")
  34. elem.style.transition = elem.style.transform = elem.style.opacity = elem.style.height = null
  35. elem.style.boxSizing = elem.style.marginTop = elem.style.marginBottom = null
  36. elem.style.paddingTop = elem.style.paddingBottom = elem.style.overflow = null
  37. elem.removeEventListener "transitionend", arguments.callee, false
  38. slideUp: (elem, remove_func, props) ->
  39. if elem.offsetTop > 1000 and elem.getBoundingClientRect().top > 1000
  40. return remove_func()
  41. elem.className += " animate-back"
  42. elem.style.boxSizing = "border-box"
  43. elem.style.height = elem.offsetHeight+"px"
  44. elem.style.overflow = "hidden"
  45. elem.style.transform = "scale(1)"
  46. elem.style.opacity = "1"
  47. elem.style.pointerEvents = "none"
  48. setTimeout (->
  49. elem.style.height = "0px"
  50. elem.style.marginTop = "0px"
  51. elem.style.marginBottom = "0px"
  52. elem.style.paddingTop = "0px"
  53. elem.style.paddingBottom = "0px"
  54. elem.style.transform = "scale(0.8)"
  55. elem.style.borderTopWidth = "0px"
  56. elem.style.borderBottomWidth = "0px"
  57. elem.style.opacity = "0"
  58. ), 1
  59. elem.addEventListener "transitionend", (e) ->
  60. if e.propertyName == "opacity" or e.elapsedTime >= 0.6
  61. elem.removeEventListener "transitionend", arguments.callee, false
  62. remove_func()
  63. slideUpInout: (elem, remove_func, props) ->
  64. elem.className += " animate-inout"
  65. elem.style.boxSizing = "border-box"
  66. elem.style.height = elem.offsetHeight+"px"
  67. elem.style.overflow = "hidden"
  68. elem.style.transform = "scale(1)"
  69. elem.style.opacity = "1"
  70. elem.style.pointerEvents = "none"
  71. setTimeout (->
  72. elem.style.height = "0px"
  73. elem.style.marginTop = "0px"
  74. elem.style.marginBottom = "0px"
  75. elem.style.paddingTop = "0px"
  76. elem.style.paddingBottom = "0px"
  77. elem.style.transform = "scale(0.8)"
  78. elem.style.borderTopWidth = "0px"
  79. elem.style.borderBottomWidth = "0px"
  80. elem.style.opacity = "0"
  81. ), 1
  82. elem.addEventListener "transitionend", (e) ->
  83. if e.propertyName == "opacity" or e.elapsedTime >= 0.6
  84. elem.removeEventListener "transitionend", arguments.callee, false
  85. remove_func()
  86. showRight: (elem, props) ->
  87. elem.className += " animate"
  88. elem.style.opacity = 0
  89. elem.style.transform = "TranslateX(-20px) Scale(1.01)"
  90. setTimeout (->
  91. elem.style.opacity = 1
  92. elem.style.transform = "TranslateX(0px) Scale(1)"
  93. ), 1
  94. elem.addEventListener "transitionend", ->
  95. elem.classList.remove("animate")
  96. elem.style.transform = elem.style.opacity = null
  97. show: (elem, props) ->
  98. delay = arguments[arguments.length-2]?.delay*1000 or 1
  99. elem.style.opacity = 0
  100. setTimeout (->
  101. elem.className += " animate"
  102. ), 1
  103. setTimeout (->
  104. elem.style.opacity = 1
  105. ), delay
  106. elem.addEventListener "transitionend", ->
  107. elem.classList.remove("animate")
  108. elem.style.opacity = null
  109. elem.removeEventListener "transitionend", arguments.callee, false
  110. hide: (elem, remove_func, props) ->
  111. delay = arguments[arguments.length-2]?.delay*1000 or 1
  112. elem.className += " animate"
  113. setTimeout (->
  114. elem.style.opacity = 0
  115. ), delay
  116. elem.addEventListener "transitionend", (e) ->
  117. if e.propertyName == "opacity"
  118. remove_func()
  119. addVisibleClass: (elem, props) ->
  120. setTimeout ->
  121. elem.classList.add("visible")
  122. window.Animation = new Animation()