小程序自定义组件之页面框架

本文详细介绍了如何在微信小程序中自定义页面框架,包括WXML结构、CSS样式以及JavaScript逻辑处理,重点展示了页面布局、顶部导航栏和事件处理等内容。
摘要由CSDN通过智能技术生成

小程序自定义组件之页面框架

引言

本文主要是讲述对整个小程序页面框架的自定义

页面布局

<!--components/frame/frame.wxml-->
<view class="parent">
  <!-- bg -->
  <view class="header_bg">
    <image class="top-image" mode="widthFix" src="../images/wd_bg.png" />
  </view>
  <!-- wrap -->
  <view class="content-wrap">
    <!-- top-bar -->
    <view class="top-bar {{position == 0 ? '' : 'top-bar-title-middle'}}" style="margin-top: {{marginTop}}px;">
      <image wx:if="{{visiable}}" class="top-bar-image" src="../images/top_return.png" mode="aspectFit" bindtap="onClick"/>
      <view class="title {{position == 0 ? 'top-bar-title-left' : ''}}">{{title}}</view>
      <!-- 右边的无效的view,为了标题居中 -->
      <view wx:if="{{visiable}}" class="top-bar-image top-bar-image-right" />
    </view>
      
    <!-- content -->
    <slot></slot>
  </view>
</view>

页面CSS

/* components/frame/frame.wxss */
.parent {
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative;
}
.title {
  /* width: 100%; */
  height: 70rpx;
  font-size: var(--fs42);
  font-weight: 500;
  /* color: var(--defaultColor); */
  color: #fff;
  /* margin-top: 80rpx; */
  /* position: relative; */
  text-align: center;
  line-height: 70rpx;
}
.header_bg {
  width: 100vw;
  height: 583rpx;
  /* height: 100%; */
  position: absolute;
}
.top-image {
  width: 100%;
  height: 100%;
}
.content-wrap {
  position: relative;
  width: 100%;
}

.top-bar {
  position: relative;
  display: flex;
  flex: column;
  /* justify-items: center; */
  /* 垂直居中 */
  align-items: center; 
  width: 100%;
  /* height: 70rpx; */
  height: 32px;
  /* background-color: brown; */
}
.top-bar-title-left {
  margin-left: 30rpx;
}
.top-bar-title-middle {
  justify-content: center;
}

.top-bar-image {
  width: 34rpx;
  height: 34rpx;
  margin-left: 20rpx;
}

.top-bar-image-right {
  margin-right: 20rpx;
  /* background-color: chartreuse; */
}

页面逻辑处理

// components/frame/frame.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    /**
     * 标题栏标题的内容,不显示可以不设置值。
     */
    title: {
      type: String,
      value: ''
    },
    /** 返回按钮是否可见 */
    visiable: {
      type: Boolean,
      value: true
    },
    /**
     * 标题的位置,0:靠左;其他值居中
     */
    position: {
      type: Number,
      value: 0
    },
    distributed: {
      type: Boolean,
      value: false
    }
    // onBackListener: {
    //   type: 'Function',
    //   value: () => {}
    // }
  },

  /**
   * 组件的初始数据
   */
  data: {
    marginTop: getApp().globalData.marginTop
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onClick() {
      console.log('frame#onClick start!!!');
      if (this.data.distributed) {
        this.triggerEvent('onBackListener');
      } else {
        wx.navigateBack({
          delta: 0,
        })
      }      
    }
  }
})

如需转载请注明原地址

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jjr_1984

谢谢您的鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值