微信小程序自定义头部组件封装

index.json

javascript 复制代码
{
  "component": true,
  "usingComponents": {}
}

index.scss

css 复制代码
.back{
  position: fixed;
  top: 0;
  margin-left: 24rpx;
  z-index: 100;
  // background-color: #fff;
  image{
    // width: 64rpx;
    // height: 64rpx;
    z-index: 999;
  }
}
.topTitle{
    // background-color: #f2f2f2;
    
    // z-index: 100;
    transition:0;
  width: 100%;
  position: fixed;
    top: 0;
text-align: center;
display: flex;
align-items: center;
justify-content: center;

font-size: 28rpx;
font-family: SFProText-Medium, SFProText-Medium;
font-weight: 400;
color: #333;
// height: 64rpx;
margin-bottom: 30rpx;
color: #FFF;

}

index.ts

javascript 复制代码
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    titleText: {
      type: String,
      value: ''
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    navtop: '',
    navmargin: '',
    navPicHeight: ''
  },
  ready() {
    // 1.获取屏幕可使用宽度
    let windowWidth = wx.getSystemInfoSync().windowWidth;
    // 2.获取状态栏高度
    const statusBarHeight = wx.getSystemInfoSync().statusBarHeight;
    // // 3.获取胶囊按钮位置信息
    const menuButton = wx.getMenuButtonBoundingClientRect();
    // 4.计算出导航栏高度  换算成rpx
    let navHeight = (menuButton.height + (menuButton.top - statusBarHeight) * 2) * (750 / windowWidth);
    // // 5.导航栏与状态栏拉开距离  margin-top: statusBarTop + 'rpx'; 
    let statusBarTop = statusBarHeight * (750 / windowWidth);
    this.setData({
      navtop: menuButton.top,
      navmargin: menuButton.top + menuButton.height + 10,
      navPicHeight: menuButton.height
    })


    console.log('menuButton.top88888888888888888', menuButton);
  },

  /**
   * 组件的方法列表
   */
  methods: {
    goBack() {
      console.log("0.22");

      wx.navigateBack({
        delta: 1,
        complete(e) {
          console.log('//', e);

        }
      })
    },

  },

})

index.wxml

html 复制代码
<view class="back" bindtap="goBack"  style='padding-top:{{navtop}}px;'>
  <image  src="../../static/center/back@2x.png"  style='width:{{navPicHeight}}px;height:{{navPicHeight}}px;'></image>
</view>
<view class="topTitle" style='margin-top:{{navtop}}px;height:{{navPicHeight}}px;'>
  {{titleText}}
</view>

使用

signIn.json引入

javascript 复制代码
  "usingComponents": {
    "navigationTop": "/components/navigationTop/index"
  },

signIn.wxml使用

html 复制代码
<navigationTop titleText='签到中心'></navigationTop>
相关推荐
一 乐1 天前
智慧养老|基于springboot+小程序社区养老保障系统设计与实现(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·后端·小程序
小小王app小程序开发1 天前
盈利 + 留存双驱动:分销商城小程序的增长法则与落地指南
小程序
不如摸鱼去1 天前
uni-app 也能远程调试?使用 PageSpy 打开调试的新大门!
前端·小程序·uni-app
峰兄1983051 天前
8266实现Modbus TCP协议转RTU串口通讯之旅
小程序
黑马源码库miui520861 天前
JAVA成人用品商城系统源码微信小程序+h5+安卓+ios
android·java·微信小程序
UI设计兰亭妙微1 天前
从“功能堆砌“到“体验至上“的蜕变之路:兰亭妙微如何助力“臻选生活馆“实现小程序重生与业绩倍增
小程序·小程序开发
万岳软件开发小城1 天前
开发一套私域直播 APP/Web/小程序需要哪些核心模块?完整技术清单来了
小程序·php·直播带货系统源码·直播带货软件开发·私域直播系统源码·私域直播平台搭建·私域直播软件开发
计算机毕设指导61 天前
基于微信小程序的篮球场馆预订系统【源码文末联系】
java·spring boot·mysql·微信小程序·小程序·tomcat·maven
闹小艾1 天前
制作知识付费线上课程小程序:制作平台实操指南,不用编程3分钟学会搭建
小程序
DsirNg1 天前
页面栈溢出问题修复总结
前端·微信小程序