CustomToast.vue 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <template>
  2. <view class="custom-toast" v-if="show" id="CustomToast">
  3. <view class="toast-content">
  4. <image class="imgbox" src="/static/image/new/icon_warning.svg"></image>
  5. <text class="toast-text">{{ text }}</text>
  6. </view>
  7. </view>
  8. </template>
  9. <script>
  10. export default {
  11. name: 'CustomToast',
  12. data() {
  13. return {
  14. show: false,
  15. text: '',
  16. duration: 1500, // 默认时间
  17. timer: null
  18. };
  19. },
  20. methods: {
  21. // 显示提示
  22. showToast(options) {
  23. // 清除之前的定时器
  24. if (this.timer) clearTimeout(this.timer);
  25. this.text = options.title || '';
  26. this.duration = options.duration || 1500;
  27. this.show = true;
  28. // 自动隐藏
  29. this.timer = setTimeout(() => {
  30. this.show = false;
  31. }, this.duration);
  32. },
  33. // 手动隐藏
  34. hideToast() {
  35. this.show = false;
  36. if (this.timer) clearTimeout(this.timer);
  37. }
  38. },
  39. beforeDestroy() {
  40. if (this.timer) clearTimeout(this.timer);
  41. }
  42. };
  43. </script>
  44. <style scoped>
  45. .custom-toast {
  46. position: fixed;
  47. top: 0;
  48. left: 0;
  49. width: 100%;
  50. height: 100%;
  51. display: flex;
  52. justify-content: center;
  53. align-items: center;
  54. pointer-events: none; /* 不阻挡底层点击 */
  55. z-index: 99999;
  56. }
  57. .toast-content {
  58. background: rgba(0, 0, 0, 0.7);
  59. color: #fff;
  60. padding: 28px 38px;
  61. border-radius: 20px;
  62. max-width: 70%;
  63. text-align: center;
  64. display: flex;
  65. align-items: center;
  66. }
  67. .imgbox{
  68. margin-right: 40px;
  69. width: 56px;
  70. height: 56px;
  71. }
  72. /* 核心:自定义文字大小(按需调整) */
  73. .toast-text {
  74. font-family: PingFang SC, PingFang SC;
  75. font-weight: 400;
  76. font-size: 28px;
  77. color: #FFFFFF;
  78. line-height: 42px;
  79. }
  80. </style>