blob: 98d58df3f0e0c42da013c73db25356a9d9c2549c [file] [log] [blame]
guangchao.xu070005a2020-12-07 09:56:40 +08001<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
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
70<style lang="scss" scoped>
71 @import "../../libs/css/style.components.scss";
72
73 view {
74 box-sizing: border-box;
75 }
76
77 // 顶部弹出类型样式
78 .u-tips {
79 width: 100%;
80 position: fixed;
81 z-index: 1;
82 padding: 20rpx 30rpx;
83 color: #FFFFFF;
84 font-size: 28rpx;
85 left: 0;
86 right: 0;
87 @include vue-flex;
88 align-items: center;
89 justify-content: center;
90 opacity: 0;
91 // 此处为最核心点,translateY(-100%)意味着将其从Y轴隐藏(隐藏到顶部(h5)或者说导航栏(app)下面)
92 transform: translateY(-100%);
93 transition: all 0.35s linear;
94 }
95
96 .u-tip-show {
97 transform: translateY(0);
98 opacity: 1;
99 z-index: 99;
100 }
101
102 .u-primary {
103 background: $u-type-primary;
104 }
105
106 .u-success {
107 background: $u-type-success;
108 }
109
110 .u-warning {
111 background: $u-type-warning;
112 }
113
114 .u-error {
115 background: $u-type-error;
116 }
117
118 .u-info {
119 background: $u-type-info;
120 }
121</style>