Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. 'use strict';
  2.  
  3.  
  4. let animate = (function() {
  5.  
  6. function ActiveState() {
  7. this.propertyCount = 0;
  8. this.callbackList = [];
  9. }
  10.  
  11. /**
  12. * @return boolean True when all properties end.
  13. */
  14. ActiveState.prototype.processTransitionEnd = function() {
  15. if (--this.propertyCount > 0) return false;
  16. while (this.callbackList.length > 0) {
  17. this.callbackList.splice(0, 1)[0].call(null);
  18. }
  19. el.style.removeProperty('transition');
  20. return true;
  21. };
  22.  
  23. let activeMap = {};
  24. function animate(el, styles, callback) {
  25. let uid = getUid(el);
  26. let active = activeMap[uid] = activeMap[uid] || new ActiveState();
  27. active.callbackList.push(callback);
  28. el.style.setProperty('transition', '.4s');
  29. for (var k in styles) {
  30. el.style.setProperty(k, styles[k]);
  31. active.propertyCount++;
  32. }
  33. }
  34.  
  35. var uidCounter = 0;
  36. function getUid(el) {
  37. return el.uid_ || (el.uid_ = 'uid:' + uidCounter++);
  38. }
  39.  
  40. window.addEventListener('transitionend', e => {
  41. let uid = getUid(e.target);
  42. let active = activeMap[uid];
  43. if (!active) return;
  44. if (active.processTransitionEnd())
  45. activeMap[uid] = null;
  46. });
  47.  
  48. return animate;
  49. })();
  50.  
  51.  
  52. let item = document.getElementById('item');
  53. item.addEventListener('click', e => {
  54. animate(e.target, {
  55. 'transform': 'translate3d(0, 400px, 100px)',
  56. 'opacity': '0.4'
  57. }, () => console.log('done.'));
  58. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement