Toast.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <template>
  2. <div id='toast-container'></div>
  3. </template>
  4. <script>
  5. import $ from 'jquery';
  6. import 'jquery-ui';
  7. /*
  8. * Calculates opacity of Toast
  9. * @param {number} left
  10. * @return {number}
  11. */
  12. const calcOpacity = left => {
  13. let opacity = 0;
  14. if (left > -200) opacity = (1 - (left * (1 / 50)));
  15. else opacity = (left * (1 / 350)) + 1;
  16. if (opacity < 0) opacity = 0;
  17. return opacity;
  18. }
  19. export default {
  20. data() {
  21. return {
  22. toasts: {}
  23. }
  24. },
  25. methods: {
  26. addToast: (text, time) => {
  27. if (typeof time !== 'number' || time < 0) {
  28. time = 10000;
  29. }
  30. let toast = $(
  31. '<div class=\'toast\'>' +
  32. '<strong>' +
  33. text +
  34. '</strong>' +
  35. '</div>'
  36. );
  37. $('#toast-container').append(toast);
  38. let revertInterval;
  39. toast.draggable({
  40. axis: 'x',
  41. revert: () => {
  42. let position = toast.draggable('instance').position;
  43. let revert = !(position.left >= 150 || position.left <= -150);
  44. if (revert) {
  45. let timePassed = 0;
  46. revertInterval = setInterval(() => {
  47. if (timePassed < 500) {
  48. timePassed += 25;
  49. let left = toast.prop('style').left;
  50. left = Number((left + '').replace('px', ''));
  51. toast.css({opacity: calcOpacity(left)});
  52. } else {
  53. clearInterval(revertInterval);
  54. }
  55. }, 25);
  56. }
  57. return revert;
  58. },
  59. stop: (event, ui) => {
  60. if (ui.position.left > -200 || ui.position.left < -150) {
  61. toast.draggable('destroy').remove();
  62. }
  63. },
  64. revertDuration: 500,
  65. drag: (event, ui) => {
  66. ui.helper.css({opacity: calcOpacity(ui.position.left)});
  67. }
  68. });
  69. setTimeout(() => {
  70. toast.draggable('destroy').remove();
  71. clearInterval(revertInterval);
  72. }, time);
  73. }
  74. }
  75. }
  76. </script>
  77. <style>
  78. #toast-container {
  79. display: block;
  80. width: 0;
  81. top: 64px;
  82. right: 100px;
  83. position: fixed;
  84. }
  85. .toast:first-child {
  86. top: 0;
  87. }
  88. .toast {
  89. z-index: 10000 !important;
  90. animation-duration: 0.60s;
  91. animation-fill-mode: both;
  92. border-radius: 2px;
  93. cursor: pointer;
  94. top: 20px;
  95. /* right: 200px; */
  96. min-width: 225px;
  97. clear: both;
  98. margin-top: 10px;
  99. /* position: static; */
  100. height: auto;
  101. background-color: #323232;
  102. padding: 10px 25px;
  103. font-size: 1rem;
  104. font-weight: 300;
  105. justify-content: space-between;
  106. flex-direction: column;
  107. box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.16), 0 2px 10px 0 rgba(0, 0, 0, 0.12);
  108. }
  109. .toast strong {
  110. color: #fff;
  111. }
  112. </style>