uni-navbar-lite.uvue 2.8 KB
Newer Older
M
mehaotian 已提交
1
<template>
2
  <view class="uni-navbar">
3 4 5 6 7 8
    <!-- #ifdef APP || WEB -->
    <view v-if="statusBar" class="status-bar"></view>
    <!-- #endif -->
    <!-- #ifdef MP -->
    <view v-if="statusBar" class="status-bar" :style="{ height: statusBarHeight + 'px' }"></view>
    <!-- #endif -->
9
    <view class="uni-navbar-inner">
10 11 12 13 14 15 16 17 18 19 20 21 22
      <view class="left-content" @click="back">
        <text :style="{ color: textColor }" class="uni-icons">{{
          unicode
        }}</text>
      </view>
      <view class="uni-navbar-content" :class="{ 'is-left': isLeft }">
        <text :style="{ color: textColor }">
          <slot>{{ title }}</slot>
        </text>
      </view>
      <view class="right-content">
        <slot name="right"></slot>
      </view>
M
mehaotian 已提交
23
    </view>
24
  </view>
M
mehaotian 已提交
25 26 27
</template>

<script>
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
  import iconpath from "./uniicons.ttf";
  export default {
    name: "uni-navbar",
    props: {
      title: {
        type: String,
        default: "",
      },
      isLeft: {
        type: Boolean,
        default: false,
      },
      textColor: {
        type: String,
        default: "#000",
43 44 45 46 47
      },
      statusBar: {
        type: Boolean,
        default: false,
      }
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    },
    data() {
      return {
        statusBarHeight: 0,
      };
    },
    computed: {
      unicode() : string {
        return "\ue600";
      },
    },
    created() {
      uni.loadFontFace({
        global: false,
        family: "UniIconsFontFamily",
        source: `url("${iconpath}")`,
        success() { },
        fail(err) {
          console.log(err);
M
mehaotian 已提交
67
        },
68 69 70
      });
    },
    mounted() {
71 72
      uni.setNavigationBarColor({
        frontColor: "#000000",
73
        backgroundColor: "#ffffff",
74 75 76 77 78
      });
      // #ifdef MP-WEIXIN
      // TODO 部分小程序平台不支持getWindowInfo
      this.statusBarHeight = uni.getWindowInfo().statusBarHeight
      // #endif
79 80 81 82 83 84 85
    },
    methods: {
      back() {
        uni.navigateBack({});
      },
    },
  };
M
mehaotian 已提交
86 87 88
</script>

<style>
89 90
  .uni-icons {
    font-family: "UniIconsFontFamily" !important;
91
    font-size: 26px;
92 93 94
    font-style: normal;
    color: #333;
  }
95 96 97 98

  .status-bar {
    height: var(--status-bar-height);
  }
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  .uni-navbar {
    background-color: #007aff;
  }

  .uni-navbar-inner {
    position: relative;
    flex-direction: row;
    justify-content: space-between;
    height: 45px;
  }

  .left-content,
  .right-content {
    justify-content: center;
    align-items: center;
114
    width: 40px;
115 116
    height: 100%;
  }
M
mehaotian 已提交
117

118 119 120 121 122 123 124 125 126 127 128
  .uni-navbar-content {
    position: absolute;
    height: 100%;
    top: 0;
    bottom: 0;
    left: 45px;
    right: 45px;
    flex-direction: row;
    justify-content: center;
    align-items: center;
  }
M
mehaotian 已提交
129

130 131 132
  .is-left {
    justify-content: flex-start;
  }
133
</style>