tips.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <template>
  2. <block v-if="position=='top'">
  3. <view class='tui-tips-class tui-toptips' :class="['tui-'+type,show?'tui-top-show':'']">{{msg}}</view>
  4. </block>
  5. <block v-else>
  6. <view class='tui-tips-class tui-toast' :class="[position=='center'?'tui-centertips':'tui-bottomtips',show?'tui-toast-show':'']">
  7. <view class="tui-tips-content" :class="['tui-'+type]">
  8. {{msg}}
  9. </view>
  10. </view>
  11. </block>
  12. </template>
  13. <script>
  14. export default {
  15. name: "tuiTips",
  16. props: {
  17. //top bottom center
  18. position: {
  19. type: String,
  20. default: "top"
  21. }
  22. },
  23. data() {
  24. return {
  25. timer: null,
  26. show: false,
  27. msg: "无法连接到服务器~",
  28. //translucent,primary,green,warning,danger
  29. type: "translucent"
  30. };
  31. },
  32. methods: {
  33. showTips: function(options) {
  34. const {
  35. type = 'translucent',
  36. duration = 2000
  37. } = options;
  38. clearTimeout(this.timer);
  39. this.show = true;
  40. // this.duration = duration < 2000 ? 2000 : duration;
  41. this.type = type;
  42. this.msg = options.msg;
  43. this.timer = setTimeout(() => {
  44. this.show = false;
  45. clearTimeout(this.timer);
  46. this.timer = null;
  47. }, duration);
  48. }
  49. }
  50. }
  51. </script>
  52. <style>
  53. /*顶部消息提醒 start*/
  54. .tui-toptips {
  55. width: 100%;
  56. padding: 18upx 30upx;
  57. box-sizing: border-box;
  58. position: fixed;
  59. z-index: 9999;
  60. color: #fff;
  61. font-size: 30upx;
  62. left: 0;
  63. top: 0;
  64. display: flex;
  65. align-items: center;
  66. justify-content: center;
  67. word-break: break-all;
  68. opacity: 0;
  69. transform: translateZ(0) translateY(-100%);
  70. transition: all 0.3s ease-in-out;
  71. }
  72. .tui-top-show {
  73. transform: translateZ(0) translateY(0);
  74. opacity: 1;
  75. }
  76. /*顶部消息提醒 end*/
  77. /*toast消息提醒 start*/
  78. /*注意问题:
  79. 1、fixed 元素宽度无法自适应,所以增加了子元素
  80. 2、fixed 和 display冲突导致动画效果消失,暂时使用visibility替代
  81. */
  82. .tui-toast {
  83. width: 80%;
  84. box-sizing: border-box;
  85. color: #fff;
  86. font-size: 28upx;
  87. position: fixed;
  88. visibility: hidden;
  89. opacity: 0;
  90. left: 50%;
  91. transition: all 0.3s ease-in-out;
  92. z-index: 9999;
  93. display: flex;
  94. align-items: center;
  95. justify-content: center;
  96. }
  97. .tui-toast-show {
  98. visibility: visible;
  99. opacity: 1;
  100. }
  101. .tui-tips-content {
  102. word-wrap: break-word;
  103. word-break: break-all;
  104. border-radius: 8upx;
  105. padding: 18upx 30upx;
  106. }
  107. /*底部消息提醒 start*/
  108. .tui-bottomtips {
  109. bottom: 120upx;
  110. -webkit-transform: translateX(-50%);
  111. transform: translateX(-50%);
  112. }
  113. /*底部消息提醒 end*/
  114. /*居中消息提醒 start*/
  115. .tui-centertips {
  116. top: 50%;
  117. -webkit-transform: translate(-50%, -50%);
  118. transform: translate(-50%, -50%);
  119. }
  120. /*居中消息提醒 end*/
  121. /*toast消息提醒 end*/
  122. /*背景颜色 start*/
  123. .tui-primary {
  124. background: #C74547;
  125. }
  126. .tui-green {
  127. background: #19be6b;
  128. }
  129. .tui-warning {
  130. background: #ff7900;
  131. }
  132. .tui-danger {
  133. background: #ed3f14;
  134. }
  135. .tui-translucent {
  136. background: rgba(0, 0, 0, 0.7);
  137. }
  138. /*背景颜色 end*/
  139. /*消息提醒 end*/
  140. </style>