fuliqi
2024-01-24 29c1e7eb5ac16e90d8991a86c1c071bc312ec8d9
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
<template>
  <div class="nav-bar navbar--shadow" :class="{ 'navbar--fixed': fixed }">
    <div
      :class="{ 'navbar--shadow': shadow, 'navbar--border': border }"
      :style="{ 'background-color': backgroundColor }"
    >
      <div class="navbar__header">
        <div class="navbar-left" @click="onClickLeft">
          <div v-if="leftIcon.length" class="navbar-content-group navbar-icon">
            <van-icon :name="leftIcon" :color="color" />
          </div>
          <div v-if="leftText.length" class="navbar-content-group">
            <span :style="{ color: color, fontSize: '14px' }">{{ leftText }}</span>
          </div>
          <slot name="left" />
        </div>
        <div class="navbar-center">
          <div v-if="title.length">
            <span class="nav-bar-text" :style="{ color: color }">{{ title }}</span>
          </div>
          <!-- 标题插槽 -->
          <slot name="center" />
        </div>
        <div class="navbar-right" @click="onClickRight">
          <div v-if="rightIcon.length" class="navbar-content-group navbar-icon">
            <van-icon :name="rightIcon" :color="color" />
          </div>
          <!-- 优先显示图标 -->
          <div
            v-if="rightText && rightText.length && !rightIcon.length"
            class="navbar-content-group"
          >
            <span :style="{ color: color, fontSize: '14px' }">{{ rightText }}</span>
          </div>
          <slot name="right" />
        </div>
      </div>
    </div>
  </div>
</template>
<script>
/**
 * NavBar 自定义导航栏
 * @description 导航栏组件,主要用于头部导航
 * @property {String} title 标题文字
 * @property {String} backgroundColor 导航栏背景颜色
 * @property {Boolean} fixed = [true|false] 是否固定顶部
 * @property {Boolean} shadow = [true|false] 导航栏下是否有阴影
 * @property {Boolean} border = [true|false] 是否包含下边框
 * @property {String} leftIcon 左侧按钮图标(图标名称参考vant官方图标)
 * @property {String} leftText 左侧按钮文本
 * @property {String} rightIcon 右侧按钮图标(图标名称参考vant官方图标)
 * @property {String} rightText 右侧按钮文本
 * @property {String} color 图标和文字颜色
 * @event {Boolean} autoBack 点击 左侧按钮是否返回
 * @event {Function} clickLeft 左侧按钮点击时触发
 * @event {Function} clickRight 右侧按钮点击时触发
 */
export default {
  name: "NavBar",
  props: {
    title: {
      type: String,
      default: "",
    },
    fixed: {
      type: [Boolean, String],
      default: true,
    },
    shadow: {
      type: [Boolean, String],
      default: true,
    },
    border: {
      type: [Boolean, String],
      default: false,
    },
    autoBack: {
      type: [Boolean, String],
      default: true,
    },
    backgroundColor: {
      type: String,
      default: "#FFFFFF",
    },
    leftIcon: {
      type: String,
      default: "",
    },
    leftText: {
      type: String,
      default: "",
    },
    rightIcon: {
      type: String,
      default: "",
    },
    rightText: {
      type: String,
      default: "",
    },
    color: {
      type: String,
      default: "#333",
    },
  },
  methods: {
    // 默认返回上一页
    onClickLeft() {
      if (!this.autoBack) {
        this.$emit("clickLeft");
      } else {
        this.$router.go(-1);
      }
    },
    onClickRight() {
      this.$emit("clickRight");
    },
  },
};
</script>
<style lang="less" scoped>
.navbar--fixed {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  z-index: 998;
  width: 100%;
}
 
.navbar--shadow {
  box-shadow: 0 2px 20px 0 rgba(0, 0, 0, 0.1);
}
.navbar--border {
  border-bottom-width: 1px;
  border-bottom-style: solid;
  border-bottom-color: #dedede;
}
.navbar__header {
  display: flex;
  height: 88px;
  line-height: 88px;
  font-size: 28px;
}
.navbar-content-group {
  display: flex;
  align-items: center;
}
.navbar-icon {
  font-size: 36px;
}
.navbar-left,
.navbar-right {
  width: 150px;
  // 转换成行内元素
  display: flex;
  flex-direction: row;
  align-items: center;
}
.navbar-right {
  padding-right: 30px;
  justify-content: flex-end;
}
.navbar-left {
  padding-left: 30px;
}
.navbar-center {
  flex: 1;
  text-align: center;
}
</style>