CustomNav.vue 3.04 KB
Newer Older
李嘉林 committed
1
<template>
2
  <div v-if="!isPcPlatform" class="CustomNav" :style="zIndexStyle">
侯体倬 committed
3 4 5
    <div class="navFixedBg" :style="navFixedBgStyle"></div>
    <div class="navBg" :style="navBgStyle">
      <div class="content" :style="contentStyle">{{showShopName}}</div>
李嘉林 committed
6 7 8 9 10
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
侯体倬 committed
11
let navHeight, navTop, navConHeight;
李嘉林 committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25
export default {
  name: "CustomNav",
  props: {
    title: {
      type: String,
      default: "",
    },
    customBgOpacity: {
      type: Boolean,
      default: false,
    }
  },
  data() {
    return {
侯体倬 committed
26
      shopName: ' ',
李嘉林 committed
27
      bgOpacity: 0,
侯体倬 committed
28 29 30 31
      
      zIndexStyle: '',
      navFixedBgStyle: '',
      navBgStyle: '',
32 33
      contentStyle: '',
      isPcPlatform: false, // 是否是PC平台
李嘉林 committed
34 35
    }
  },
侯体倬 committed
36
  created() {
37 38 39 40 41 42
    const deviceInfo = wx.getDeviceInfo()
    this.isPcPlatform = ['mac', 'windows'].includes(deviceInfo.platform);
    if (!this.isPcPlatform) {
      this.getNavbarInfo();
      this.initialize();
    }
李嘉林 committed
43 44
  },
  onPageScroll(el) {
45 46 47
    if (!this.isPcPlatform) {
      this.getScroll(el);
    }
李嘉林 committed
48
  },
侯体倬 committed
49 50 51 52 53 54 55 56 57 58 59 60 61
  watch: {
    customBgOpacity() {
      wx.nextTick(() => {
        this.initialize();
        this.$forceUpdate();
      })
    }
  },
  computed: {
    showShopName() {
      return this.shopName;
    }
  },
李嘉林 committed
62
  methods: {
侯体倬 committed
63 64 65 66 67 68 69
    initialize() {
      this.setShopName();
      this.setZIndexStyle();
      this.setNavFixedBgStyle();
      this.setNavBgStyle();
      this.setContentStyle();
    },
李嘉林 committed
70 71 72
    getScroll(el) {
      let opacity = (el.scrollTop / 100).toFixed(1) - 0;
      this.bgOpacity = opacity > 1 ? 1 : opacity;
侯体倬 committed
73 74
      this.setNavBgStyle();
      this.setContentStyle();
李嘉林 committed
75 76
    },
    getNavbarInfo() {
侯体倬 committed
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
      const { height, top } = wx.getMenuButtonBoundingClientRect();
      const { statusBarHeight } = wx.getSystemInfoSync()
      navTop = statusBarHeight;
      navHeight = (statusBarHeight + height + (top - statusBarHeight) * 2);
      navConHeight = (height + (top - statusBarHeight) * 2);
    },
    setShopName() {
      this.shopName = this.title || wx.getStorageSync("shopName");
    },
    setZIndexStyle() {
      this.zIndexStyle = `z-index: ${this.customBgOpacity ? 9991 : 9999};`;
    },
    setNavFixedBgStyle() {
      this.navFixedBgStyle = `height:${this.customBgOpacity ? navTop : navHeight}px;`;
    },
    setNavBgStyle() {
      this.navBgStyle = `
        height: ${navHeight}px;
        background:rgba(255, 255, 255, ${this.customBgOpacity ? this.bgOpacity : 1});
      `;
    },
    setContentStyle() {
      this.contentStyle = `
        top: ${navTop}px;
        height: ${navConHeight}px;
        opacity: ${this.customBgOpacity ? this.bgOpacity : 1};
      `;
    },
李嘉林 committed
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
  }
}
</script>

<style lang="scss" scoped>
.CustomNav {
  position: relative;

  .navFixedBg {
    width: 100%;
  }

  .navBg {
    position: fixed;
    width: 100%;
    left: 0;
    top: 0;
  }

  .content {
    position: absolute;
    left: 0;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
李嘉林 committed
131
    font-size: 1em;
李嘉林 committed
132
    font-family: "PingFangSC-Medium, PingFang SC";
李嘉林 committed
133 134 135
  }
}
</style>