toast.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <template>
  2. <view :style="{zIndex: zIndex}" class="container">
  3. <block v-for="message in messageQueue" :key="message.id">
  4. <view :style="message.type === 'toast'?'background-color: #555;color: #FFFFFF;':''" :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 :style="message.type === 'toast'?'font-size: 24rpx':''">{{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. toast(arg) {
  51. const message = {
  52. type: 'toast'
  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. info(arg) {
  68. const message = {
  69. type: 'info'
  70. }
  71. if (typeof arg === 'object' && arg) {
  72. message.content = arg.content
  73. message.duration = arg.duration
  74. message.background = arg.background
  75. } else if(typeof arg === 'string') {
  76. message.content = arg
  77. }
  78. this.fadeIn(message)
  79. },
  80. /**
  81. * 展示成功提示
  82. * @param {Object} content
  83. */
  84. success(arg) {
  85. const message = {
  86. type: 'success'
  87. }
  88. if (typeof arg === 'object' && arg) {
  89. message.content = arg.content
  90. // 显示时长会用在 settimeout的参数中,必须保证类型正确
  91. if (arg.duration && typeof arg.duration === 'number' && arg.duration >= 0) {
  92. message.duration = arg.duration
  93. }
  94. message.background = arg.background
  95. } else if(typeof arg === 'string') {
  96. message.content = arg
  97. }
  98. this.fadeIn(message)
  99. },
  100. /**
  101. * 展示警告提示
  102. */
  103. warn(arg) {
  104. const message = {
  105. type: 'warn'
  106. }
  107. if (typeof arg === 'object' && arg) {
  108. message.content = arg.content
  109. if (arg.duration && typeof arg.duration === 'number' && arg.duration >= 0) {
  110. message.duration = arg.duration
  111. }
  112. message.background = arg.background
  113. } else if(typeof arg === 'string') {
  114. message.content = arg
  115. }
  116. this.fadeIn(message)
  117. },
  118. /**
  119. * 展示错误提示
  120. * @param {Object} message
  121. */
  122. error(arg) {
  123. const message = {
  124. type: 'error'
  125. }
  126. if (typeof arg === 'object' && arg) {
  127. message.content = arg.content
  128. if (arg.duration && typeof arg.duration === 'number' && arg.duration >= 0) {
  129. message.duration = arg.duration
  130. }
  131. message.background = arg.background
  132. } else if(typeof arg === 'string') {
  133. message.content = arg
  134. }
  135. this.fadeIn(message)
  136. },
  137. fadeIn(message) {
  138. message.id = this.generateId()
  139. message.animation = 'fadeIn'
  140. this.messageQueue.push(message)
  141. // 动画执行完毕后取消动画效果,防止列表刷新时重新执行动画
  142. setTimeout(() => {
  143. message.animation = ''
  144. }, 410)
  145. // 显示一段时间后隐藏
  146. setTimeout(() => {
  147. this.fadeOut(message)
  148. }, message.duration || this.duration)
  149. },
  150. fadeOut(message) {
  151. message.animation = 'fadeOut'
  152. setTimeout(() => {
  153. let idx = 0
  154. this.messageQueue.some((msg, index) => {
  155. if (msg.id === message.id) {
  156. idx = index
  157. return true
  158. }
  159. })
  160. this.messageQueue.splice(idx, 1)
  161. }, 500)
  162. },
  163. generateId() {
  164. return (new Date()).getTime() * 1000 + (this.lastId++) % 1000
  165. }
  166. }
  167. }
  168. </script>
  169. <style lang="scss" scoped>
  170. @font-face {
  171. font-family: 'bobo-message-iconfont'; /* project id 1477381 */
  172. src: url('https://at.alicdn.com/t/font_1477381_i3ji49ios6.eot');
  173. src: url('https://at.alicdn.com/t/font_1477381_i3ji49ios6.eot?#iefix') format('embedded-opentype'),
  174. url('https://at.alicdn.com/t/font_1477381_i3ji49ios6.ttf') format('truetype'),
  175. url('https://at.alicdn.com/t/font_1477381_i3ji49ios6.svg#iconfont') format('svg');
  176. }
  177. .container {
  178. left: 0;
  179. right: 0;
  180. top: 30%;
  181. min-width: 10rpx;
  182. max-width: 550rpx;
  183. position: fixed;
  184. margin: 0 auto;
  185. display: flex;
  186. flex-direction: column;
  187. align-items: center;
  188. // 透明层允许点击穿透
  189. pointer-events: none;
  190. .message {
  191. display: flex;
  192. // 内容区域禁止点击穿透
  193. pointer-events: all;
  194. align-items: center;
  195. padding: 8px 16px;
  196. border-radius: 4px;
  197. margin-top: 5px;
  198. box-shadow: 0 1px 6px rgba(0, 0, 0, .2);
  199. background: #fff;
  200. min-height: 37px;
  201. transition: all .5s;
  202. box-sizing: border-box;
  203. .bm-icon {
  204. font-family: bobo-message-iconfont;
  205. margin-right: 10rpx;
  206. }
  207. .info {
  208. color: #288ced;
  209. }
  210. .success {
  211. color: #09be70;
  212. }
  213. .warn {
  214. color: #ff991f;
  215. }
  216. .error {
  217. color: #ef4017;
  218. }
  219. }
  220. .background-info {
  221. border: 2px solid #d4eefe;
  222. background: #f0faff;
  223. color: #288ced;
  224. box-shadow: none !important;
  225. }
  226. .background-success {
  227. border: 2px solid #baf2d1;
  228. background: #edfff4;
  229. color: #09be70;
  230. box-shadow: none !important;
  231. }
  232. .background-warn {
  233. border: 2px solid #ffe7a7;
  234. background: #fff9e7;
  235. color: #ff991f;
  236. box-shadow: none !important;
  237. }
  238. .background-error {
  239. border: 2px solid #ffcfb9;
  240. background: #ffefe6;
  241. color: #ef4017;
  242. box-shadow: none !important;
  243. }
  244. .fadeIn {
  245. animation: fadeIn 0.4s both;
  246. }
  247. @keyframes fadeIn {
  248. from {
  249. opacity: 0;
  250. transform: translate(0, -30px);
  251. }
  252. to {
  253. opacity: 1;
  254. transform: translate(0, 0);
  255. }
  256. }
  257. .fadeOut {
  258. animation: fadeOut 0.4s forwards;
  259. }
  260. @keyframes fadeOut {
  261. from {
  262. opacity: 1;
  263. transform: translate(0, 0);
  264. }
  265. to {
  266. opacity: 0;
  267. margin-top: -37px;
  268. }
  269. }
  270. }
  271. </style>