custom-toast.vue 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <template>
  2. <view class="custom-toast" :class="[show ? 'show' : 'hide', position ? position : '']">
  3. {{ message }}
  4. </view>
  5. </template>
  6. <script>
  7. //弹出消息提示组件,咋外部通过 $refs 获取组件实例,然后调用 hover 方法传入 提示信息即可使用
  8. export default {
  9. data() {
  10. return {
  11. show: false,
  12. message: '',
  13. position: ''
  14. }
  15. },
  16. methods: {
  17. hover (message, duration = 2345, position) {
  18. if (position) {
  19. this.position = position
  20. }
  21. clearTimeout(this.timer)
  22. this.show = true
  23. this.message = message
  24. this.timer = setTimeout(() => {
  25. this.show = false
  26. this.message = ''
  27. }, duration)
  28. },
  29. hide() {
  30. this.show = false
  31. }
  32. }
  33. }
  34. </script>
  35. <style lang="scss" scoped>
  36. @keyframes show-custom-toast {
  37. 0% {
  38. opacity: 0;
  39. }
  40. 100% {
  41. opacity: 1;
  42. }
  43. }
  44. .custom-toast {
  45. position: fixed;
  46. line-height: 48rpx;
  47. font-size: 30rpx;
  48. max-width: 60vw;
  49. background: rgba(0, 0, 0, .6);
  50. padding: 12rpx 24rpx;
  51. top: 50%;
  52. left: 50%;
  53. transform: translate(-50%, -50%);
  54. z-index: 666;
  55. text-align: center;
  56. color: #FFFFFF;
  57. border-radius: 8rpx;
  58. }
  59. .show {
  60. animation: show-custom-toast .6s;
  61. animation-fill-mode: forwards;
  62. }
  63. .hide {
  64. display: none;
  65. }
  66. .top {
  67. top: 20%;
  68. }
  69. .center {
  70. top: 48%;
  71. }
  72. .bottom {
  73. top: 80%;
  74. }
  75. </style>