小程序自定义顶部组件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>
相关推荐
weishuangyun1232 小时前
微信小程序怎么选公司?上线流程详解
大数据·小程序
show43311 小时前
2026微信小程序视频转文字多语种识别引擎技术选型:22种方言25种外语挑战
微信小程序·小程序·音视频
xiaopang17 小时前
一次小程序 Canvas 海报真机调试复盘:从空白导出到 iOS 保存失败
uni-app
lhldsg17 小时前
县城外卖系统开发实战:从需求分析到上线全流程指南
java·小程序·需求分析
Kingexpand_com20 小时前
旅游小程序怎么选型?模板与定制二次开发技术对比解析
小程序·旅游·小程序开发·旅行·定制开发
万岳科技系统开发20 小时前
外卖跑腿小程序如何提升本地生活服务平台运营效率?
大数据·小程序·生活
MrFlySand_飞沙20 小时前
uniapp开发微信小程序实现接入企业微信客服
微信小程序·小程序·uni-app·企业微信
Ai-_Man21 小时前
豆包智能体把对话批量导出为Word或PDF的正确顺序
人工智能·ai·小程序·pdf·word
mykj155121 小时前
打车顺风车代驾小程序的核心,远不止一个页面
小程序·代驾小程序开发·顺风车小程序·出租车app
show43321 小时前
2026微信小程序批量处理视频文件架构方案:免费批量实测
微信小程序·小程序·架构