tui-tips.vue 2.6 KB

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