index.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <template>
  2. <!-- 过度动画 -->
  3. <transition name="fade">
  4. <div class="toast-container" v-if="visible">
  5. <!-- Toast 内容 -->
  6. <div class="toast" :class="type">
  7. <div class="content">
  8. <i class="iconfont" :class="'icon-' + type"></i>
  9. <span>{{ content }}</span>
  10. </div>
  11. <!-- 关闭按钮 -->
  12. <i v-if="hasClose" class="iconfont icon-close close" :class="'close-' + type" @click="visible = false">x</i>
  13. </div>
  14. </div>
  15. </transition>
  16. </template>
  17. <script>
  18. export default {
  19. name: 'toast-tip',
  20. data() {
  21. return {
  22. content: '',
  23. time: 3000,
  24. visible: false,
  25. //对应的动态显示class type四种类型:info, success, warning, error
  26. type: 'error',
  27. hasClose: true,
  28. }
  29. },
  30. mounted() {
  31. this.close();
  32. },
  33. methods: {
  34. close() {
  35. this.hasClose = true
  36. setTimeout(() => {
  37. this.visible = false;
  38. }, this.time);
  39. }
  40. }
  41. }
  42. </script>
  43. <style lang="scss" scoped>
  44. /* 动画效果 淡入淡出 */
  45. .fade-enter-active,
  46. .fade-leave-active {
  47. transition: all 0.5s ease;
  48. }
  49. .fade-enter,
  50. .fade-leave-active {
  51. opacity: 0;
  52. }
  53. .toast-container {
  54. position: fixed;
  55. top: 0;
  56. right: 0;
  57. bottom: 0;
  58. left: 0;
  59. display: flex;
  60. // 横居中
  61. justify-content: center;
  62. // 竖居中
  63. align-items: center;
  64. z-index: 99999;
  65. .toast {
  66. width: 340px;
  67. padding: 20px;
  68. border-radius: 6px;
  69. font-size: 16px;
  70. display: flex;
  71. justify-content: space-between;
  72. align-items: center;
  73. // background-color: #909399;
  74. .content {
  75. span {
  76. padding-left: 10px;
  77. }
  78. }
  79. // 不同类型下的Toast 的显示样式
  80. &.info {
  81. background: #edf2fc;
  82. border: 1px solid #e0eafc;
  83. color: #909399;
  84. }
  85. &.success {
  86. background: #f0f9eb;
  87. border: 1px solid #e7f9de;
  88. color: #67c23a;
  89. }
  90. &.warning {
  91. background: #fdf6ec;
  92. border: 1px solid #f9ecda;
  93. color: #e6a23c;
  94. }
  95. &.error {
  96. background: #fef0f0;
  97. border: 1px solid #fbdfdf;
  98. color: #f56c6c;
  99. }
  100. //关闭按钮的样式
  101. .close {
  102. cursor: pointer;
  103. color: #fff;
  104. }
  105. .close-success{
  106. color: cadetblue;
  107. }
  108. .close-error{
  109. color: brown;
  110. }
  111. .close-warning{
  112. color: burlywood;
  113. }
  114. .close-info{
  115. color: currentcolor;
  116. }
  117. }
  118. }
  119. </style>