可用于快速实现微信小程序在不同设备类型的适配开发。包含CSS响应式样式、设备信息获取、平台兼容性处理及安全区域适配等核心代码片段,助力开发者解决多端适配问题。【此简介由AI生成】
| Files | Last commit | Last update |
|---|---|---|
| 9 months ago | ||
| 9 months ago | ||
| 9 months ago | ||
| 9 months ago | ||
| 9 months ago | ||
| 9 months ago | ||
| 9 months ago | ||
| 9 months ago |
MiniProgramCodeSnippet
本项目包含微信小程序跨设备类型适配演示文稿材料中的代码片段。
代码片段
1. adapt.css
/* 绝对值宽高 */
.block {
width: 240px;
height: 240px;
}
/* 使用视口高度单位 */
.block-vh {
width: 50vh;
height: 50vh;
}
/* 媒体查询小屏响应式样式 */
.block-rpx {
width: 300rpx;
height: 300rpx;
}
/* 媒体查询大屏响应式样式 */
@media screen and (min-width: 400px) {
.block-rpx {
width: 600rpx;
height: 600rpx;
}
}
2. app.js
// app.js
App({
onLaunch() {
// 小程序启动时直接存入全局变量
this.device_info = getDeviceInfo();
}
});
function getDeviceInfo() {
const info = wx.getDeviceInfo();
return {
type: info.platform, // Platform 的值
isAndroid: info.platform === "android", // 快速判断 Android
isIos: info.platform === "ios", // 快速判断 iOS
isOhos: info.platform === "ohos", // 快速判断 HarmonyOS Next
isPc:
info.platform === "windows" ||
info.platform === "mac" ||
info.platform === "ohos_pc", // 快速判断 PC 端
isDevtool: info.platform === "devtools" // 快速判断开发者工具 (正常用户不会访问这个,仅开发者调试用)
};
}
3. 平台兼容性处理
旧方案 (platform_before.js)
// 获取系统信息
const info = wx.getDeviceInfo();
// 基于判断结果执行逻辑
if (info.platform === "ohos") {
console.log("当前系统不支持卡券功能");
// TODO: 在此处添加禁用卡券的代码逻辑
}
新方案 (platform_after.js)
// 检测当前基础库是否支持卡券功能 (wx.openCard)
const canUseCard = wx.canIUse("openCard");
// 基于检查结果执行逻辑
if (!canUseCard) {
console.log("当前系统不支持卡券功能");
// TODO: 在此处添加禁用卡券的代码逻辑
}
4. tarbar.css
.tabbar {
position: fixed;
left: 0;
right: 0;
bottom: 0;
height: var(--tabbar-height);
box-sizing: content-box;
padding-bottom: constant(safe-area-inset-bottom);
padding-bottom: env(safe-area-inset-bottom);
}
5. temp.css
.temp {
padding-bottom: calc(100% - var(--tabbar-height) - env(safe-area-inset-bottom));
}
.temp {
height: 100%;
padding-bottom: calc(100% - var(--tabbar-height) - env(safe-area-inset-bottom));
}
.temp {
position: fixed;
left: 0;
right: 0;
bottom: calc(100rpx + var(--tabbar-height) + constant(safe-area-inset-bottom));
bottom: calc(100rpx + var(--tabbar-height) + env(safe-area-inset-bottom));
}
Introduction
可用于快速实现微信小程序在不同设备类型的适配开发。包含CSS响应式样式、设备信息获取、平台兼容性处理及安全区域适配等核心代码片段,助力开发者解决多端适配问题。【此简介由AI生成】
Customize your domain