Novecane
2025-02-15 a12e5f63a90446525b2f322fb2bbd21d5285cd6d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<template>
    <view class="uni-navbar" :class="{'uni-dark':dark, 'uni-nvue-fixed': fixed}">
        <view class="uni-navbar__content" :class="{ 'uni-navbar--fixed': fixed, 'uni-navbar--shadow': shadow, 'uni-navbar--border': border }"
            :style="{ 'background-color': themeBgColor, 'border-bottom-color':themeColor }" >
            <status-bar v-if="statusBar" />
            <view :style="{ color: themeColor,backgroundColor: themeBgColor ,height:navbarHeight}"
                class="uni-navbar__header">
                <view @tap="onClickLeft" class="uni-navbar__header-btns uni-navbar__header-btns-left"
                    :style="{width:leftIconWidth}">
                    <slot name="left">
                        <view class="uni-navbar__content_view" v-if="leftIcon.length > 0">
                            <uni-icons :color="themeColor" :type="leftIcon" size="20" />
                        </view>
                        <view :class="{ 'uni-navbar-btn-icon-left': !leftIcon.length > 0 }" class="uni-navbar-btn-text"
                            v-if="leftText.length">
                            <text :style="{ color: themeColor, fontSize: '12px' }">{{ leftText }}</text>
                        </view>
                    </slot>
                </view>
                <view class="uni-navbar__header-container " @tap="onClickTitle">
                    <slot>
                        <view class="uni-navbar__header-container-inner" v-if="title.length>0">
                            <text class="uni-nav-bar-text uni-ellipsis-1"
                                :style="{color: themeColor }">{{ title }}</text>
                        </view>
                    </slot>
                </view>
                <view @click="onClickRight" class="uni-navbar__header-btns uni-navbar__header-btns-right"
                    :style="{width:rightIconWidth}">
                    <slot name="right">
                        <view v-if="rightIcon.length">
                            <uni-icons :color="themeColor" :type="rightIcon" size="22" />
                        </view>
                        <view class="uni-navbar-btn-text" v-if="rightText.length && !rightIcon.length">
                            <text class="uni-nav-bar-right-text" :style="{ color: themeColor}">{{ rightText }}</text>
                        </view>
                    </slot>
                </view>
            </view>
        </view>
        <!-- #ifndef APP-NVUE -->
        <view class="uni-navbar__placeholder" v-if="fixed">
            <status-bar v-if="statusBar" />
            <view class="uni-navbar__placeholder-view" :style="{ height:navbarHeight}" />
        </view>
        <!-- #endif -->
    </view>
</template>
 
<script>
    import statusBar from "./uni-status-bar.vue";
    const getVal = (val) => typeof val === 'number' ? val + 'px' : val;
 
    /**
     * 
     * 
     * NavBar 自定义导航栏
     * @description 导航栏组件,主要用于头部导航
     * @tutorial https://ext.dcloud.net.cn/plugin?id=52
     * @property {Boolean} dark 开启黑暗模式
     * @property {String} title 标题文字
     * @property {String} leftText 左侧按钮文本
     * @property {String} rightText 右侧按钮文本
     * @property {String} leftIcon 左侧按钮图标(图标类型参考 [Icon 图标](http://ext.dcloud.net.cn/plugin?id=28) type 属性)
     * @property {String} rightIcon 右侧按钮图标(图标类型参考 [Icon 图标](http://ext.dcloud.net.cn/plugin?id=28) type 属性)
     * @property {String} color 图标和文字颜色
     * @property {String} backgroundColor 导航栏背景颜色
     * @property {Boolean} fixed = [true|false] 是否固定顶部
     * @property {Boolean} statusBar = [true|false] 是否包含状态栏
     * @property {Boolean} shadow = [true|false] 导航栏下是否有阴影
     * @property {Boolean} stat 是否开启统计标题上报
     * @event {Function} clickLeft 左侧按钮点击时触发
     * @event {Function} clickRight 右侧按钮点击时触发
     * @event {Function} clickTitle 中间标题点击时触发
     */
    export default {
        name: "UniNavBar",
        components: {
            statusBar
        },
        emits: ['clickLeft', 'clickRight', 'clickTitle'],
        props: {
            dark: {
                type: Boolean,
                default: false
            },
            title: {
                type: String,
                default: ""
            },
            leftText: {
                type: String,
                default: ""
            },
            rightText: {
                type: String,
                default: ""
            },
            leftIcon: {
                type: String,
                default: ""
            },
            rightIcon: {
                type: String,
                default: ""
            },
            fixed: {
                type: [Boolean, String],
                default: false
            },
            color: {
                type: String,
                default: ""
            },
            backgroundColor: {
                type: String,
                default: ""
            },
            statusBar: {
                type: [Boolean, String],
                default: false
            },
            shadow: {
                type: [Boolean, String],
                default: false
            },
            border: {
                type: [Boolean, String],
                default: true
            },
            height: {
                type: [Number, String],
                default: 44
            },
            leftWidth: {
                type: [Number, String],
                default: 60
            },
            rightWidth: {
                type: [Number, String],
                default: 60
            },
            stat: {
                type: [Boolean, String],
                default: ''
            }
        },
        computed: {
            themeBgColor() {
                if (this.dark) {
                    // 默认值
                    if (this.backgroundColor) {
                        return this.backgroundColor
                    } else {
                        return this.dark ? '#333' : '#FFF'
                    }
                }
                return this.backgroundColor || '#FFF'
            },
            themeColor() {
                if (this.dark) {
                    // 默认值
                    if (this.color) {
                        return this.color
                    } else {
                        return this.dark ? '#fff' : '#333'
                    }
                }
                return this.color || '#333'
            },
            navbarHeight() {
                return getVal(this.height)
            },
            leftIconWidth() {
                return getVal(this.leftWidth)
            },
            rightIconWidth() {
                return getVal(this.rightWidth)
            }
        },
        mounted() {
            if (uni.report && this.stat && this.title !== '') {
                uni.report('title', this.title)
            }
        },
        methods: {
            onClickLeft() {
                this.$emit("clickLeft");
            },
            onClickRight() {
                this.$emit("clickRight");
            },
            onClickTitle() {
                this.$emit("clickTitle");
            }
        }
    };
</script>
 
<style lang="scss" scoped>
    $nav-height: 44px;
 
    .uni-nvue-fixed {
        /* #ifdef APP-NVUE */
        position: sticky;
        /* #endif */
    }
    .uni-navbar {
        // box-sizing: border-box;
    }
 
    .uni-nav-bar-text {
        /* #ifdef APP-PLUS */
        font-size: 34rpx;
        /* #endif */
        /* #ifndef APP-PLUS */
        font-size: 14px;
        /* #endif */
    }
 
    .uni-nav-bar-right-text {
        font-size: 12px;
    }
 
    .uni-navbar__content {
        position: relative;
        // background-color: #fff;
        // box-sizing: border-box;
        background-color: transparent;
    }
 
    .uni-navbar__content_view {
        // box-sizing: border-box;
    }
 
    .uni-navbar-btn-text {
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        flex-direction: column;
        justify-content: flex-start;
        align-items: center;
        line-height: 12px;
    }
 
    .uni-navbar__header {
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        padding: 0 10px;
        flex-direction: row;
        height: $nav-height;
        font-size: 12px;
    }
 
    .uni-navbar__header-btns {
        /* #ifndef APP-NVUE */
        overflow: hidden;
        display: flex;
        /* #endif */
        flex-wrap: nowrap;
        flex-direction: row;
        width: 120rpx;
        // padding: 0 6px;
        justify-content: center;
        align-items: center;
        /* #ifdef H5 */
        cursor: pointer;
        /* #endif */
    }
 
    .uni-navbar__header-btns-left {
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        width: 120rpx;
        justify-content: flex-start;
        align-items: center;
    }
 
    .uni-navbar__header-btns-right {
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        flex-direction: row;
        // width: 150rpx;
        // padding-right: 30rpx;
        justify-content: flex-end;
        align-items: center;
    }
 
    .uni-navbar__header-container {
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        flex: 1;
        padding: 0 10px;
        overflow: hidden;
    }
 
    .uni-navbar__header-container-inner {
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        flex: 1;
        flex-direction: row;
        align-items: center;
        justify-content: center;
        font-size: 12px;
        overflow: hidden;
        // box-sizing: border-box;
    }
 
 
    .uni-navbar__placeholder-view {
        height: $nav-height;
    }
 
    .uni-navbar--fixed {
        position: fixed;
        z-index: 99;
        /* #ifdef H5 */
        left: var(--window-left);
        right: var(--window-right);
        /* #endif */
        /* #ifndef H5 */
        left: 0;
        right: 0;
        /* #endif */
 
    }
 
    .uni-navbar--shadow {
        box-shadow: 0 1px 6px #ccc;
    }
 
    .uni-navbar--border {
        border-bottom-width: 1rpx;
        border-bottom-style: solid;
        border-bottom-color: #eee;
    }
 
    .uni-ellipsis-1 {
        overflow: hidden;
        /* #ifndef APP-NVUE */
        white-space: nowrap;
        text-overflow: ellipsis;
        /* #endif */
        /* #ifdef APP-NVUE */
        lines: 1;
        text-overflow: ellipsis;
        /* #endif */
    }
 
    // 暗主题配置
    .uni-dark {}
</style>