MyNavBar.vue 2.6 KB
<template>
  <div class="app-nav-bar" v-if="show" :style="{ height: 46 + offsetTop + 'px', paddingTop: offsetTop + 'px', backgroundColor: background }">
    <slot v-if="$slots.leftArrow" name="leftArrow"></slot>
    <div v-else-if="leftArrow" class="left-arrow" @click="leftClick" :style="leftArrowStyle">
      <van-icon name="arrow-left" />
      <span class="l-text">{{ leftText }}</span>
    </div>
    <div class="nav-title" :style="navTitleStyle">{{ title }}</div>
    <slot v-if="$slots.rightArrow" name="rightArrow"></slot>
    <div v-else-if="rightArrow" class="right-arrow" @click="rightClick" :style="rightArrowStyle">
      <span class="r-text">{{ rightText }}</span>
    </div>
  </div>
</template>

<script>
export default {
  name: 'MyNavBar',
  props: {
    show: {
      type: Boolean,
      default: () => {
        return false
      }
    },
    title: {
      type: String,
      default: ''
    },
    navTitleStyle: {
      type: Boolean,
      default: () => {
        return false
      }
    },
    offsetTop: {
      type: Number,
      default: 0
    },
    background: {
      type: String,
      default: '#fff'
    },
    leftArrow: {
      type: Boolean,
      default: () => {
        return false
      }
    },
    leftText: {
      type: String,
      default: ''
    },
    leftArrowStyle: {
      type: Object,
      default: () => {
        return {}
      }
    },
    rightArrow: {
      type: Boolean,
      default: () => {
        return false
      }
    },
    rightText: {
      type: String,
      default: ''
    },
    rightArrowStyle: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {}
  },
  methods: {
    leftClick() {
      this.$emit('left-callback')
    },
    rightClick() {
      this.$emit('right-click')
    }
  }
}
</script>

<style lang="scss" scoped>
.app-nav-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  height: 46px;
  background-color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  border-bottom: 1px solid #ebedf0;

  .nav-title {
    font-size: 16px;
    font-weight: bold;
    color: #323233;
  }
  .left-arrow {
    position: absolute;
    z-index: 10;
    top: 0;
    left: 10px;
    min-width: 46px;
    height: 46px;
    display: flex;
    align-items: center;
    color: #333;
    font-size: 14px;
    .l-text {
      margin-left: 4px;
    }
  }
  .right-arrow {
    position: absolute;
    z-index: 10;
    top: 0;
    right: 10px;
    min-width: 46px;
    height: 46px;
    display: flex;
    align-items: center;
    color: #333;
    font-size: 14px;
  }
}
</style>