当前位置: 首页 > news >正文

微信小程序屏幕适配组件

使用mobx-miniprogram构建store

screenStore.js代码

import { observable, action } from "mobx-miniprogram";
export const screenStore = observable({
  statusBarHeight: 0, // 状态栏高度
  navBarHeight: 0, // 自定义导航栏高度
  safeHeight: 0, // 屏幕安全区域高度
  bottomSafeHeight: 0, // 底部安全区域高度
  menuRight: 0, // 胶囊按钮右侧到屏幕右边的间距
  menuHeight: 0, // 胶囊按钮高度
  initData: action(function () {
    const windowInfo = wx.getWindowInfo();
    const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
    this.statusBarHeight = windowInfo.statusBarHeight;
    this.navBarHeight = menuButtonInfo.height + (menuButtonInfo.top - this.statusBarHeight) * 2;
    this.safeHeight = windowInfo.safeArea.height;
    this.bottomSafeHeight = windowInfo.screenHeight - windowInfo.safeArea.height - this.statusBarHeight;
    this.menuRight = windowInfo.screenWidth - menuButtonInfo.right;
    this.menuHeight = menuButtonInfo.height;
  })
})
使用behaviors在组件和页面中自动引入
screenBehavior.js代码
import { BehaviorWithStore } from 'mobx-miniprogram-bindings';
import { screenStore } from '../screenStore';

export const screenBehavior = BehaviorWithStore({
  storeBindings: {
    store: screenStore,
    fields: ['statusBarHeight', 'navBarHeight', 'safeHeight', 'bottomSafeHeight', 'menuRight', 'menuHeight'],
    actions: ['initData']
  }
});
 
在app.js中初始化
import { screenStore } from "@/stores/screenStore"
App({
  onLaunch() {
    screenStore.initData()
  }
})
screen-header组件代码
 
wxml部分
<view
  class="page_header"
  style="height: {{navBarHeight + statusBarHeight}}px;padding: {{statusBarHeight}}px {{menuRight}}px 0;{{backgroundColor}};box-shadow: {{boxShadow}};align-items: {{alignItems}};"
>
  <view class="page_header_left">
    <view class="back" wx:if="{{canBack}}" bind:tap="goBack">
      <image src="{{imgURL}}/icon/icon-arrow-left-black.svg" mode="widthFix" />
      <text class="title">{{title}}</text>
    </view>
    <slot name="left"></slot>
  </view>
  <view class="page_header_center">
    <slot name="center"></slot>
  </view>
  <view class="page_header_right">
    <slot name="right"></slot>
  </view>
</view>
 
css部分
.page_header {
  display: flex;
  justify-content: space-between;
  width: 100vw;
  box-sizing: border-box;
  &_left {
    display: flex;
    align-items: center;
    gap: 16rpx;
    flex: 1;
    .back {
      display: flex;
      align-items: center;
      gap: 16rpx;
      image {
        width: 18rpx;
        height: 34rpx;
      }
      .title {
        font-size: 34rpx;
        font-weight: 500;
        line-height: normal;
        letter-spacing: 0;
        color: #333333;
      }
    }
  }
  &_right {
    flex: 1;
  }
}
 
js部分
import { imgURL } from '@/utils/env'
import { screenBehavior } from '@/stores/behaviors/screenBehavior';
Component({
  behaviors: [screenBehavior],
  options: {
    multipleSlots: true
  },
  properties: {
    backgroundColor: {
      type: String,
      value: '#FFFFFF'
    },
    alignItems: {
      type: String,
      value: 'center'
    },
    boxShadow: {
      type: String,
      value: '0 0 2px 0 rgba(0, 0, 0, 0.3)'
    },
    canBack: {
      type: Boolean,
      value: false
    },
    title: {
      type: String,
      value: ''
    }
  },
  data: {
    imgURL
  },
  methods: {
    goBack() {
      const pages = getCurrentPages(); // 获取当前页面栈
      const currentPageStackLength = pages.length;
      if (currentPageStackLength > 1) {
        // 存在上一页,返回上一层
        wx.navigateBack({
          delta: 1
        });
      } else {
        // 没有上一页,跳转至首页 (假设首页路径为 '/pages/index/index')
        wx.reLaunch({
          url: '/pages/home/index'
        });
      }
    }
  }
})
json无需修改
 
screen-bottom组件代码
 
wxml部分
<view class="page_bottom" style="height:{{bottomSafeHeight}}px;"></view>
 
css 部分
.page_bottom {
  width: 100vw;
  background-color: #fff;
}
 
js部分
import { screenBehavior } from '@/stores/behaviors/screenBehavior';
Component({
  behaviors: [screenBehavior],
  options: {
    multipleSlots: true
  },
  properties: {

  },
  data: {

  },
  methods: {

  }
})
json无需修改
 
在页面中引用
 
{
  "usingComponents": {
    "screen-header": "/components/screen-header",
    "screen-bottom": "/components/screen-bottom"
  }
}
 
<view class="page">
  <screen-header canBack boxShadow="none" />
  <view class="page_content"></view>
  <screen-bottom />
</view>
 
http://www.jsqmd.com/news/143157/

相关文章:

  • 从零搭建手机自动化系统,Open-AutoGLM让你效率提升300%
  • 为什么顶尖团队都在抢Open-AutoGLM源码?下载方法首次公开
  • LaWGPT实战指南:打造你的专属法律AI助手
  • CO3Dv2三维重建实战指南:从入门到精通
  • GNU Radio终极入门指南:快速掌握免费开源软件定义无线电
  • 2025 MBA必备!8个AI论文工具测评:开题报告与文献综述全攻略
  • 零基础运营创收,全功能邀请函制作系统源码核心优势列表
  • 突破三维重建技术瓶颈:CO3D数据集引领多视角物体识别革命
  • Neuro终极指南:打造本地AI语音助手的完整解决方案
  • 2025本科生必看8个降AI率工具测评榜单
  • 深圳大学前端测网速原理|JS 网速测试实现思路源码分析
  • 快速上手Qwen图像编辑工具:10分钟掌握AI绘图核心技术
  • [150页最新PPT]深度解析大模型与知识图谱的融合范式,通往AGI的必由之路?
  • Java 团队 AI 转型神器!JBoltAI 框架让 AI 应用开发少走 4-6 个月弯路
  • PaddlePaddle框架支持的知识蒸馏功能使用示例
  • 如何在32位Windows系统上快速安装FFmpeg:终极免费指南
  • 海量模板持续更新:揭秘高扩展性邀请函制作平台源码功能列表
  • 收藏必备!大模型开发者的8大核心技能,从入门到企业级应用
  • 区块链智能合约测试:不可篡改背后的质量挑战
  • 哪些外在因素条件会导致服务器的延迟过高?
  • JBoltAI 框架实测:Java AI 应用开发效率提升 80% 的秘密
  • GrasscutterTool:5分钟掌握原神开发指令的终极方案
  • 基于RK3576的智能轮椅应用解决方案
  • 【Java毕设源码分享】基于springboot+vue的教务管理系统的设计与实现(程序+文档+代码讲解+一条龙定制)
  • 从A2UI到AIGS:JBoltAI如何重塑AI时代的人机交互与系统开发
  • Windows 32位FFmpeg完整安装指南:从零开始快速配置
  • PaddlePaddle框架的批归一化(BatchNorm)实现细节
  • 模型识别靠图像还是文本,Open-AutoGLM工作原理全解析,速看!
  • 从工具到体系:JBoltAI 定义企业规模化智能的交付标准与未来方向
  • 海尔智能设备接入HomeAssistant:2025年全屋智能生态整合终极指南