小程序自定义顶部组件uniapp

customNav.vue

javascript 复制代码
<template>

    <view>

        <view class="navbar-wrapper" :style="{

            paddingTop: statusBarHeight,

            paddingBottom: paddingBottom+'px',

            backgroundColor: `rgba(255,255,255,${topOpacity})`,

        }">

            <view :style="{height: titleHeight+'px',lineHeight:  titleHeight+'px',color: topOpacity > 0.9 ? '#000' : ''}">

               

                <view v-if="$slots.default">

                    <slot></slot>

                </view>

                <text v-else> {{ title }}</text>

            </view>

        </view>

        <view v-if="isBlockHeight" :style="{

            height: totalHeight+'px'

        }"></view>

    </view>

   

  </template>

   

  <script>

    export default {

      name: 'customNav',

      props: ['title','isBlockHeight'],

      data() {

        return {

          // 像素单位

          pxUnit: 'px',

          // 默认状态栏高度

          statusBarHeight: 'var(--status-bar-height)',

          // 微信小程序右上角的胶囊菜单宽度

          rightSafeArea: 0,

          titleHeight: 40,

          totalHeight: 0,

          topOpacity: 0,

          paddingBottom: 0

        }

      },

      mounted() {

        const px = this.pxUnit

        // #ifndef H5

        // 获取窗口信息

        const windowInfo = uni.getWindowInfo()

        this.statusBarHeight = windowInfo.statusBarHeight

        // #endif

         

        // #ifdef MP-WEIXIN

        // 获取胶囊左边界坐标

        const { left,top,height } = uni.getMenuButtonBoundingClientRect()

        const info = uni.getSystemInfoSync()

        this.statusBarHeight = top;

        this.titleHeight = height;

        // 计算胶囊(包括右边距)占据屏幕的总宽度:屏幕宽度-胶囊左边界坐标

        this.rightSafeArea = windowInfo.windowWidth - left + px

    this.paddingBottom = top-info.statusBarHeight;

        // #endif

        // #ifndef H5

        this.totalHeight = this.titleHeight + this.statusBarHeight + this.paddingBottom;

        // #endif

        // #ifdef H5

        this.totalHeight = this.titleHeight

        // #endif

        this.statusBarHeight = this.statusBarHeight+'px';

      },

      methods:{

        pageScroll(scrollTop){

            let topOpacity = scrollTop / this.totalHeight

            if (scrollTop < 10) {

                topOpacity = 0

            } else if (topOpacity >= 1) {

                topOpacity = 1

            }

            this.topOpacity = topOpacity;

        }

      }

    }

  </script>

   

  <style scoped>

    .navbar-wrapper {

      box-sizing: border-box;

      background-color: transparent;

      position: relative;

      font-size: 16px;

      font-weight: 700;

      color: #fff;

      text-align: center;

      position: fixed;

      top: 0;

      width: 100%;

      z-index: 10;

    }

  </style>

使用

javascript 复制代码
<template>

   <customNavBarVue ref="customNavRef" title="拾光机"></customNavBarVue>

</template>

<script>

let timer = null

export default {

    data() {

        return {}

    },

    onPageScroll(e){

        this.$refs.customNavRef.pageScroll(e.scrollTop)

    },

    methods: {}

</script>

<style lang="scss" scoped>

</style>
相关推荐
样子20181 小时前
Uniapp 之renderjs解决swiper+多个video卡顿问题
前端·javascript·css·uni-app·html
fakaifa3 小时前
点大餐饮独立版系统源码v1.0.3+uniapp前端+搭建教程
小程序·uni-app·php·源码下载·点大餐饮·扫码点单
Bug改不动了5 小时前
React Native 与 UniApp 对比
react native·react.js·uni-app
anyup8 小时前
🔥🔥 10 天 Star 破百!uView Pro 文档也开源啦:完全免费、无广告、高效上手
前端·vue.js·uni-app
说私域9 小时前
基于开源 AI 大模型 AI 智能名片 S2B2C 商城小程序视角下的企业组织能力建设与破圈升级
人工智能·小程序
fakaifa17 小时前
【最新版】CRMEB Pro版v3.4系统源码全开源+PC端+uniapp前端+搭建教程
人工智能·小程序·uni-app·php·crmeb·源码下载·crmebpro
2501_915918411 天前
iOS 应用上架全流程实践,从开发内测到正式发布的多工具组合方案
android·ios·小程序·https·uni-app·iphone·webview
上海云盾第一敬业销售1 天前
小程序被爬虫攻击,使用waf能防护吗?
爬虫·小程序
suncentwl1 天前
做一个答题pk小程序多少钱?
小程序·答题小程序·知识竞赛·答题pk软件
说私域1 天前
基于开源链动2+1模式AI智能名片S2B2C商城小程序的流量转化策略研究
人工智能·小程序