toast.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <template>
  2. <view :style="{zIndex: zIndex}" class="container">
  3. <block v-for="message in messageQueue" :key="message.id">
  4. <view :class="[message.animation, backgroundClass(message)]" class="message">
  5. <text v-if="message.type === 'info'" class="bm-icon info">&#xe671;</text>
  6. <text v-if="message.type === 'success'" class="bm-icon success">&#xe62f;</text>
  7. <text v-if="message.type === 'warn'" class="bm-icon warn">&#xe671;</text>
  8. <text v-if="message.type === 'error'" class="bm-icon error">&#xe630;</text>
  9. <text>{{message.content}}</text>
  10. </view>
  11. </block>
  12. </view>
  13. </template>
  14. <script>
  15. // 需要支持的单独配置项:显示时长、是否启用背景、类型、内容
  16. export default {
  17. name: 'bobo-message-cpt',
  18. props: {
  19. zIndex: {
  20. type: Number,
  21. default: 10000
  22. },
  23. duration: {
  24. type: Number,
  25. default: 2000
  26. },
  27. background: {
  28. type: Boolean,
  29. default: false
  30. }
  31. },
  32. data() {
  33. return {
  34. messageQueue: [],
  35. lastId: 0
  36. }
  37. },
  38. computed: {
  39. backgroundClass() {
  40. return msg => {
  41. return this.background || msg.background ? `background-${msg.type}` : ''
  42. }
  43. }
  44. },
  45. methods: {
  46. /**
  47. * 展示普通提示信息
  48. * @param {Object} content
  49. */
  50. info(arg) {
  51. const message = {
  52. type: 'info'
  53. }
  54. if (typeof arg === 'object' && arg) {
  55. message.content = arg.content
  56. message.duration = arg.duration
  57. message.background = arg.background
  58. } else if(typeof arg === 'string') {
  59. message.content = arg
  60. }
  61. this.fadeIn(message)
  62. },
  63. /**
  64. * 展示成功提示
  65. * @param {Object} content
  66. */
  67. success(arg) {
  68. const message = {
  69. type: 'success'
  70. }
  71. if (typeof arg === 'object' && arg) {
  72. message.content = arg.content
  73. // 显示时长会用在 settimeout的参数中,必须保证类型正确
  74. if (arg.duration && typeof arg.duration === 'number' && arg.duration >= 0) {
  75. message.duration = arg.duration
  76. }
  77. message.background = arg.background
  78. } else if(typeof arg === 'string') {
  79. message.content = arg
  80. }
  81. this.fadeIn(message)
  82. },
  83. /**
  84. * 展示警告提示
  85. */
  86. warn(arg) {
  87. const message = {
  88. type: 'warn'
  89. }
  90. if (typeof arg === 'object' && arg) {
  91. message.content = arg.content
  92. if (arg.duration && typeof arg.duration === 'number' && arg.duration >= 0) {
  93. message.duration = arg.duration
  94. }
  95. message.background = arg.background
  96. } else if(typeof arg === 'string') {
  97. message.content = arg
  98. }
  99. this.fadeIn(message)
  100. },
  101. /**
  102. * 展示错误提示
  103. * @param {Object} message
  104. */
  105. error(arg) {
  106. const message = {
  107. type: 'error'
  108. }
  109. if (typeof arg === 'object' && arg) {
  110. message.content = arg.content
  111. if (arg.duration && typeof arg.duration === 'number' && arg.duration >= 0) {
  112. message.duration = arg.duration
  113. }
  114. message.background = arg.background
  115. } else if(typeof arg === 'string') {
  116. message.content = arg
  117. }
  118. this.fadeIn(message)
  119. },
  120. fadeIn(message) {
  121. message.id = this.generateId()
  122. message.animation = 'fadeIn'
  123. this.messageQueue.push(message)
  124. // 动画执行完毕后取消动画效果,防止列表刷新时重新执行动画
  125. setTimeout(() => {
  126. message.animation = ''
  127. }, 410)
  128. // 显示一段时间后隐藏
  129. setTimeout(() => {
  130. this.fadeOut(message)
  131. }, message.duration || this.duration)
  132. },
  133. fadeOut(message) {
  134. message.animation = 'fadeOut'
  135. setTimeout(() => {
  136. let idx = 0
  137. this.messageQueue.some((msg, index) => {
  138. if (msg.id === message.id) {
  139. idx = index
  140. return true
  141. }
  142. })
  143. this.messageQueue.splice(idx, 1)
  144. }, 500)
  145. },
  146. generateId() {
  147. return (new Date()).getTime() * 1000 + (this.lastId++) % 1000
  148. }
  149. }
  150. }
  151. </script>
  152. <style lang="scss" scoped>
  153. @font-face {
  154. font-family: 'bobo-message-iconfont'; /* project id 1477381 */
  155. src: url('https://at.alicdn.com/t/font_1477381_i3ji49ios6.eot');
  156. src: url('https://at.alicdn.com/t/font_1477381_i3ji49ios6.eot?#iefix') format('embedded-opentype'),
  157. url('https://at.alicdn.com/t/font_1477381_i3ji49ios6.ttf') format('truetype'),
  158. url('https://at.alicdn.com/t/font_1477381_i3ji49ios6.svg#iconfont') format('svg');
  159. }
  160. .container {
  161. left: 0;
  162. right: 0;
  163. top: 30%;
  164. min-width: 10rpx;
  165. max-width: 550rpx;
  166. position: fixed;
  167. margin: 0 auto;
  168. display: flex;
  169. flex-direction: column;
  170. align-items: center;
  171. // 透明层允许点击穿透
  172. pointer-events: none;
  173. .message {
  174. display: flex;
  175. // 内容区域禁止点击穿透
  176. pointer-events: all;
  177. align-items: center;
  178. padding: 8px 16px;
  179. border-radius: 4px;
  180. margin-top: 5px;
  181. box-shadow: 0 1px 6px rgba(0, 0, 0, .2);
  182. background: #fff;
  183. min-height: 37px;
  184. transition: all .5s;
  185. box-sizing: border-box;
  186. .bm-icon {
  187. font-family: bobo-message-iconfont;
  188. margin-right: 10rpx;
  189. }
  190. .info {
  191. color: #288ced;
  192. }
  193. .success {
  194. color: #09be70;
  195. }
  196. .warn {
  197. color: #ff991f;
  198. }
  199. .error {
  200. color: #ef4017;
  201. }
  202. }
  203. .background-info {
  204. border: 2px solid #d4eefe;
  205. background: #f0faff;
  206. color: #288ced;
  207. box-shadow: none !important;
  208. }
  209. .background-success {
  210. border: 2px solid #baf2d1;
  211. background: #edfff4;
  212. color: #09be70;
  213. box-shadow: none !important;
  214. }
  215. .background-warn {
  216. border: 2px solid #ffe7a7;
  217. background: #fff9e7;
  218. color: #ff991f;
  219. box-shadow: none !important;
  220. }
  221. .background-error {
  222. border: 2px solid #ffcfb9;
  223. background: #ffefe6;
  224. color: #ef4017;
  225. box-shadow: none !important;
  226. }
  227. .fadeIn {
  228. animation: fadeIn 0.4s both;
  229. }
  230. @keyframes fadeIn {
  231. from {
  232. opacity: 0;
  233. transform: translate(0, -30px);
  234. }
  235. to {
  236. opacity: 1;
  237. transform: translate(0, 0);
  238. }
  239. }
  240. .fadeOut {
  241. animation: fadeOut 0.4s forwards;
  242. }
  243. @keyframes fadeOut {
  244. from {
  245. opacity: 1;
  246. transform: translate(0, 0);
  247. }
  248. to {
  249. opacity: 0;
  250. margin-top: -37px;
  251. }
  252. }
  253. }
  254. </style>