toast.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <template>
  2. <view class="tui-toast" :class="[visible?'tui-toast-show':'',content?'tui-toast-padding':'',icon?'':'tui-unicon-padding']" :style="{width:getWidth(icon,content)}">
  3. <image :src="imgUrl" class="tui-toast-img" v-if="icon"></image>
  4. <view class="tui-toast-text" :class="[icon?'':'tui-unicon']">{{title}}</view>
  5. <view class="tui-toast-text tui-content-ptop" v-if="content && icon">{{content}}</view>
  6. </view>
  7. </template>
  8. <script>
  9. export default {
  10. name: "tuiToast",
  11. props: {
  12. },
  13. data() {
  14. return {
  15. timer: null,
  16. //是否显示
  17. visible: false,
  18. //显示标题
  19. title: "操作成功",
  20. //显示内容
  21. content: "",
  22. //是否有icon
  23. icon:false,
  24. imgUrl:""
  25. };
  26. },
  27. methods: {
  28. show: function(options) {
  29. let {
  30. duration = 2000,
  31. icon=false
  32. } = options;
  33. clearTimeout(this.timer);
  34. this.visible = true;
  35. this.title = options.title || "";
  36. this.content = options.content || "";
  37. this.icon=icon;
  38. if(icon && options.imgUrl){
  39. this.imgUrl=options.imgUrl
  40. }
  41. this.timer = setTimeout(() => {
  42. this.visible = false;
  43. clearTimeout(this.timer);
  44. this.timer = null;
  45. }, duration);
  46. },
  47. getWidth(icon,content){
  48. let width="auto";
  49. if(icon){
  50. width=content?'420rpx':'360rpx'
  51. }
  52. return width
  53. }
  54. }
  55. }
  56. </script>
  57. <style>
  58. .tui-toast {
  59. background: rgba(0, 0, 0, 0.75);
  60. border-radius: 10rpx;
  61. position: fixed;
  62. visibility: hidden;
  63. opacity: 0;
  64. left: 50%;
  65. top: 48%;
  66. -webkit-transform: translate(-50%, -50%);
  67. transform: translate(-50%, -50%);
  68. transition: 0.3s ease-in-out;
  69. transition-property:opacity,visibility;
  70. z-index: 9999;
  71. display: flex;
  72. align-items: center;
  73. justify-content: center;
  74. flex-direction: column;
  75. padding: 60rpx 20rpx 54rpx 20rpx;
  76. box-sizing: border-box;
  77. }
  78. .tui-toast-padding {
  79. padding-top: 50rpx !important;
  80. padding-bottom: 50rpx !important;
  81. }
  82. .tui-unicon-padding {
  83. padding: 24rpx 40rpx !important;
  84. word-break: break-all;
  85. }
  86. .tui-toast-show {
  87. visibility: visible;
  88. opacity: 1;
  89. }
  90. .tui-toast-img {
  91. width: 120rpx;
  92. height: 120rpx;
  93. display: block;
  94. margin-bottom: 28rpx;
  95. }
  96. .tui-toast-text {
  97. font-size: 30rpx;
  98. line-height: 30rpx;
  99. font-weight: 400;
  100. color: #fff;
  101. text-align: center;
  102. }
  103. .tui-unicon{
  104. line-height: 40rpx !important;
  105. font-size: 32rpx !important;
  106. }
  107. .tui-content-ptop {
  108. padding-top: 10rpx;
  109. font-size: 26rpx !important;
  110. }
  111. </style>