u-top-tips.vue 2.8 KB

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