xanimate.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * xuwei 2021-11-26 v0.1
  3. * 应用于大屏看板动画
  4. *
  5. */
  6. const animateCSS = (element, animation, prefix = 'animate__') =>
  7. new Promise((resolve, reject) => {
  8. const animationName = `${prefix}${animation}`;
  9. const node = document.querySelector(element);
  10. node.classList.add(`${prefix}animated`, animationName);
  11. function handleAnimationEnd(event) {
  12. event.stopPropagation();
  13. node.classList.remove(`${prefix}animated`, animationName);
  14. resolve('Animation ended');
  15. }
  16. node.addEventListener('animationend', handleAnimationEnd, { once: true });
  17. });
  18. //显示数量动画
  19. function animateAddNum(id) {
  20. var num = Math.round(Math.random() * 10000);
  21. $(id).after("<div id='" + id.replace('#', '') + num + "' style='color:orange;position: absolute;left:" + ($(id).offset().left + 30) + "px;top:" + ($(id).offset().top - 20) + "px;'>+1</div>");
  22. animateCSS(id + num, 'fadeInUp').then((message) => {
  23. $(id).text(parseInt($(id).text()) + 1);
  24. animateCSS(id + num, 'fadeOutUp').then((message) => {
  25. $(id + num).remove();
  26. });
  27. });
  28. }
  29. //条码正常移动动画
  30. function animateInLeftOutRight(id, title) {
  31. var num = Math.round(Math.random() * 10000);
  32. $(id).append("<div id='" + id.replace('#', '') + num + "' style='position: absolute;left:" + ($(id).offset().left + 20) + "px;top:" + ($(id).offset().top + 5) + "px;'>" + title + "</div>");
  33. animateCSS(id + num, 'fadeInLeft').then((message) => {
  34. animateCSS(id + num, 'fadeOutRight').then((message) => {
  35. $(id + num).remove();
  36. });
  37. });
  38. }
  39. //条码异常动画
  40. function animateInLeftHinge(id, title) {
  41. var num = Math.round(Math.random() * 10000);
  42. $(id).append("<div id='" + id.replace('#', '') + num + "' style='position: absolute;left:" + ($(id).offset().left + 20) + "px;top:" + ($(id).offset().top + 5) + "px;'>" + title + "</div>");
  43. animateCSS(id + num, 'fadeInLeft').then((message) => {
  44. animateCSS(id + num, 'hinge').then((message) => {
  45. $(id + num).remove();
  46. });
  47. });
  48. }