u-top-tips.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <template>
  2. <view class="u-tips" :class="['u-' + type, isShow ? 'u-tip-show' : '']" :style="{
  3. top: navbarHeight + 'px',
  4. zIndex: uZIndex
  5. }">{{ title }}</view>
  6. </template>
  7. <script>
  8. /**
  9. * topTips 顶部提示
  10. * @description 该组件一般用于页面顶部向下滑出一个提示,尔后自动收起的场景。
  11. * @tutorial https://www.uviewui.com/components/topTips.html
  12. * @property {String Number} navbar-height 导航栏高度(包含状态栏高度在内),单位PX
  13. * @property {String Number} z-index z-index值(默认975)
  14. * @example <u-top-tips ref="uTips"></u-top-tips>
  15. */
  16. export default {
  17. name: "u-top-tips",
  18. props: {
  19. // 导航栏高度,用于提示的初始化
  20. navbarHeight: {
  21. type: [Number, String],
  22. // #ifndef H5
  23. default: 0,
  24. // #endif
  25. // #ifdef H5
  26. default: 44,
  27. // #endif
  28. },
  29. // z-index值
  30. zIndex: {
  31. type: [Number, String],
  32. default: ''
  33. }
  34. },
  35. data() {
  36. return {
  37. timer: null, // 定时器
  38. isShow: false, // 是否显示消息组件
  39. title: '', // 组件中显示的消息内容
  40. type: 'primary', // 消息的类型(颜色不同),primary,success,error,warning,info
  41. duration: 2000, // 组件显示的时间,单位为毫秒
  42. };
  43. },
  44. computed: {
  45. uZIndex() {
  46. return this.zIndex ? this.zIndex : this.$u.zIndex.topTips;
  47. }
  48. },
  49. methods: {
  50. show(config = {}) {
  51. // 先清除定时器(可能是上一次定义的,需要清除了再开始新的)
  52. clearTimeout(this.timer);
  53. // 时间,内容,类型主题(type)等参数
  54. if (config.duration) this.duration = config.duration;
  55. if (config.type) this.type = config.type;
  56. this.title = config.title;
  57. this.isShow = true;
  58. // 倒计时
  59. this.timer = setTimeout(() => {
  60. this.isShow = false;
  61. clearTimeout(this.timer);
  62. this.timer = null;
  63. }, this.duration);
  64. }
  65. }
  66. };
  67. </script>
  68. <style lang="scss" scoped>
  69. view {
  70. box-sizing: border-box;
  71. }
  72. // 顶部弹出类型样式
  73. .u-tips {
  74. width: 100%;
  75. position: fixed;
  76. z-index: 1;
  77. padding: 20rpx 30rpx;
  78. color: #FFFFFF;
  79. font-size: 28rpx;
  80. left: 0;
  81. right: 0;
  82. display: flex;
  83. align-items: center;
  84. justify-content: center;
  85. opacity: 0;
  86. // 此处为最核心点,translateY(-100%)意味着将其从Y轴隐藏(隐藏到顶部(h5)或者说导航栏(app)下面)
  87. transform: translateY(-100%);
  88. transition: all 0.35s linear;
  89. }
  90. .u-tip-show {
  91. transform: translateY(0);
  92. opacity: 1;
  93. z-index: 99;
  94. }
  95. .u-primary {
  96. background: $u-type-primary;
  97. }
  98. .u-success {
  99. background: $u-type-success;
  100. }
  101. .u-warning {
  102. background: $u-type-warning;
  103. }
  104. .u-error {
  105. background: $u-type-error;
  106. }
  107. .u-info {
  108. background: $u-type-info;
  109. }
  110. </style>