微信原生小程序自定义封装组件(以导航navbar为例)

封装

topnav.js

javascript 复制代码
const App = getApp();
Component({
  // 组件的属性列表
  properties: {
    pageName: String, //中间的title
    showNav: { //判断是否显示左上角的按钮    
      type: Boolean,
      value: true
    },
    showHome: { //判断是否显示左上角的home按钮
      type: Boolean,
      value: true
    },
    showLocation:{
      type: Boolean,
      value: false      
    },
    showColor:String, //颜色
    showStore:String,
    showWhite:{
      type: Boolean,
      value: false      
    },
    titlecolor:String,//title
    titleleft:String,//title
    //中间是否是定位
  },
  // 组件的初始数据
  data: {
    // showNav: true, //判断是否显示左上角的home按钮
    // showHome: true, //判断是否显示左上角的按钮
  },
  lifetimes: {
    // 生命周期函数,可以为函数,或一个在methods段中定义的方法名
    attached: function() {
      this.setData({
        navHeight: App.globalData.navHeight, //导航栏高度
        navTop: App.globalData.navTop, //胶囊按钮与顶部的距离
        jnheight: App.globalData.jnheight, //胶囊高度
        jnwidth: App.globalData.jnwidth //胶囊宽度
      })
    }
  },
  // 组件的方法列表
  methods: {
    //回退
    navBack: function() {
      let pages = getCurrentPages();
      let prevpage = pages[pages.length - 2];
      prevpage.setData({
        isflag:true
      })
      wx.navigateBack()
    },
    //回主页
    navHome: function() {
      wx.reLaunch({
        url: '/pages/index/index'
      })
    },
    //跳转定位页面
    golocation:function(){
      wx.navigateTo({
        url: '/pages/switchcity/switchcity'
      })
    }
  }
})

topnav.json

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

topnav.wxml

html 复制代码
<view class="navbar" style="height:{{navHeight}}px;background:{{showColor}};">
  <!-- 左上角 返回按钮 和 home按钮 wx:if="{{showNav}}" 是控制左上角按钮的显示隐藏,首页不显示 -->
  <view class="navbar_left"
  style="top:{{navTop}}px;height:{{jnheight}}px;width:{{jnwidth}}px;background:{{showWhite?'rgba(0,0,0,0.2)':''}};"
  wx:if="{{showNav}}">
    <!-- 控制返回按钮的显示 -->
    <view bindtap="navBack">
      <!-- <image src="../../images/back.png" mode="widthFix" style="width:40%"></image> -->
      <i class="iconfont iconfanhui" style="color:{{showWhite?'#fff':''}};"></i>
    </view>
    <!-- home按钮 wx:if="{{showHome}}" 是控制左上角 home按钮的显示隐藏-->
    <view class="nav_line" bindtap="navHome" wx:if="{{showHome}}">
      <!-- <image src="../../images/backhome.png" mode="widthFix" style="width:50%"></image> -->
      <i class="iconfont iconshouye" style="color:{{showWhite?'#fff':''}};"></i>
    </view>
  </view>
  <!-- 中间标题 -->
  <!-- <view wx:if="{{showLocation}}" style="top:{{navTop}}px;"  class="navbar_title">
    <view bindtap="golocation">{{pageName}}<i class="iconfont iconarrowtriangle_down_fill"></i></view>
  </view> -->
  <view class="navbar_title {{showWhite?'':''}}" style="top:{{navTop}}px;padding-left: {{titleleft}}px;color:{{titlecolor}}"><view>{{pageName}}</view></view>
  <!-- white_title  白色 -->
</view>

topnav.wxss

css 复制代码
@import "/static/css/icon.wxss";

.navbar {
  width: 100%;
  overflow: hidden;
  top: 0;
  left: 0;
  flex-shrink: 0;
  position: fixed !important;
  z-index: 9999;
  background: white;
}

.navbar_left {
  display: -webkit-flex;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  -webkit-align-items: center;
  align-items: center;
  position: absolute;
  left: 20rpx;
  z-index: 11;
  line-height: 1;
  border: 1rpx solid #f0f0f0;
  border-radius: 40rpx;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.6);
  box-sizing: border-box;
}

.navbar_left view {
  width: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.nav_line {
  border-left: 1rpx solid #f0f0f0;
}

.navbar_title {
  width: 100%;
  box-sizing: border-box;
  text-align: center;
  height: 64rpx;
  line-height: 64rpx;
  position: absolute;
  left: 0;
  z-index: 10;
  font-size: 32rpx;
  font-weight: bold;
  text-overflow: ellipsis;
  overflow: hidden;
}

.white_title {
  color: #fff !important;
}

.iconfont {
  font-size: 34rpx;
  font-weight: 600;
}

.iconarrowtriangle_down_fill {
  font-size: 20rpx;
  margin-left: 6rpx;
}

icon.wxss

css 复制代码
@font-face {
  font-family: 'iconfont';  /* Project id 2408657 */
  src: url('//at.alicdn.com/t/c/font_2408657_h16zp03rdqu.woff2?t=1705321879152') format('woff2'),
       url('//at.alicdn.com/t/c/font_2408657_h16zp03rdqu.woff?t=1705321879152') format('woff'),
       url('//at.alicdn.com/t/c/font_2408657_h16zp03rdqu.ttf?t=1705321879152') format('truetype');
}

.iconfont {
  font-family: "iconfont" !important;
  font-size: 16px;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.iconxuanzhong1:before {
  content: "\e631";
}

.iconweixuanzhong:before {
  content: "\e633";
}

.iconcaigoufapiao:before {
  content: "\e6b7";
}

.iconshangsheng1:before {
  content: "\e737";
}

.iconkefu:before {
  content: "\e625";
}

.iconFrame:before {
  content: "\e636";
}

.iconxiajiantou:before {
  content: "\eb6d";
}

.iconshangjiantou:before {
  content: "\eb6e";
}

.iconshangxiaqiehuan:before {
  content: "\e9cd";
}

.iconxiala2:before {
  content: "\e634";
}

.iconwenduxiajiang:before {
  content: "\e622";
}

.icondiandiandian:before {
  content: "\e6f5";
}

.iconliuyan1:before {
  content: "\e748";
}

.icondizhi:before {
  content: "\e63e";
}

.iconyunshu:before {
  content: "\e637";
}

.iconshang1:before {
  content: "\e64e";
}

.iconxia1:before {
  content: "\e64f";
}

.iconcha:before {
  content: "\e679";
}

.icona-xuanzhong:before {
  content: "\e602";
}

.iconbitian:before {
  content: "\e669";
}

.iconzuobiaofill:before {
  content: "\e768";
}

.icondianhua:before {
  content: "\e635";
}

.iconzuobiao:before {
  content: "\e615";
}

.iconshanchu:before {
  content: "\e601";
}

.icon4Sdian:before {
  content: "\e740";
}

.iconxiala1:before {
  content: "\e756";
}

.iconzhankaishangxia:before {
  content: "\e7b2";
}

.iconshang:before {
  content: "\e610";
}

.iconxia:before {
  content: "\e61b";
}

.iconxiala:before {
  content: "\e7b1";
}

.iconxuanzhong:before {
  content: "\e60d";
}

.iconarrowtriangle_down_fill:before {
  content: "\e620";
}

.iconshangsheng:before {
  content: "\e609";
}

.iconxiajiang:before {
  content: "\e738";
}

.iconchacha:before {
  content: "\e62f";
}

.iconright:before {
  content: "\eb1b";
}

.iconfanhui1:before {
  content: "\e61e";
}

.iconsousuo:before {
  content: "\e600";
}

.iconfanhui:before {
  content: "\e63b";
}

.iconshouye:before {
  content: "\e659";
}

使用

index.wxml

html 复制代码
  <topnav pageName="导航"></topnav>

index.json

json 复制代码
{
  "usingComponents": {
    "topnav": "/component/topnav/topnav",
  },
  "navigationStyle":"custom",
  "navigationBarTitleText": "text"
}
相关推荐
码起来呗2 小时前
基于微信小程序的旅游攻略分享互动平台设计与实现-项目分享
微信小程序·小程序·旅游
2501_915106324 小时前
App HTTPS 抓包 工程化排查与工具组合实战
网络协议·ios·小程序·https·uni-app·php·iphone
2501_916008895 小时前
金融类 App 加密加固方法,多工具组合的工程化实践(金融级别/IPA 加固/无源码落地/Ipa Guard + 流水线)
android·ios·金融·小程序·uni-app·iphone·webview
2501_915921436 小时前
Fastlane 结合 开心上架(Appuploader)命令行版本实现跨平台上传发布 iOS App 免 Mac 自动化上架实战全解析
android·macos·ios·小程序·uni-app·自动化·iphone
游戏开发爱好者88 小时前
iOS 上架要求全解析,App Store 审核标准、开发者准备事项与开心上架(Appuploader)跨平台免 Mac 实战指南
android·macos·ios·小程序·uni-app·iphone·webview
00后程序员张8 小时前
混淆 iOS 类名与变量名的实战指南,多工具组合把混淆做成工程能力(混淆 iOS 类名变量名/IPA 成品混淆Ipa/Guard CLI 实操)
android·ios·小程序·https·uni-app·iphone·webview
陈思杰系统思考Jason9 小时前
面对未来:企业决策与适应力
百度·微信·微信公众平台·新浪微博·微信开放平台
码起来呗11 小时前
基于Spring Boot的乡村拼车小程序的设计与实现-项目分享
spring boot·后端·小程序
2501_9160074712 小时前
iOS文件管理工具深度剖析,从系统沙盒到跨平台文件操作的多工具协同实践
android·macos·ios·小程序·uni-app·cocoa·iphone
wapchief12 小时前
微信小程序camera相机帧转图片base64
微信小程序·小程序