微信小程序适配暗黑模式

1.配置theme.json


在theme.json中定义相关变量,在app.json中以@开头引用

//theme.json
{
 "light": {
   "navigationBarTextStyle": "black",
   "navBackgroundColor": "#ffffff",
   "backgroundColor": "#ebeaef",
   "backgroundTextStyle": "dark",
   "tabFontColor":"#bfbfbf",
   "tabSelectedColor":"#515151",
   "tabBgColor":"#ffffff",
   "tabBorderStyle":"black",
   "iconPath1": "images/ghome.png",
   "iconPath2": "images/gzb.png",
   "iconPath3": "images/guser.png",
   "selectedIconPath1": "images/bhome.png",
   "selectedIconPath2": "images/bzb.png",
   "selectedIconPath3": "images/buser.png"
 },
 "dark": {
   "navigationBarTextStyle": "white",
   "navBackgroundColor": "#000000",
   "backgroundColor": "#000000",
   "backgroundTextStyle": "light",
   "tabFontColor":"#bfbfbf",
   "tabSelectedColor":"#ffffff",
   "tabBgColor":"#191919",
   "tabBorderStyle":"white",
   "iconPath1": "images/ghome.png",
   "iconPath2": "images/gzb.png",
   "iconPath3": "images/guser.png",
   "selectedIconPath1": "images/whome.png",
   "selectedIconPath2": "images/wzb.png",
   "selectedIconPath3": "images/wuser.png"
 }
}

2.配置app.json


app.json中配置darkmode为ture
配置themeLocation
引用相关配置

//app.json

"darkmode": true,
"themeLocation": "theme.json",
"window": {
    "navigationBarTitleText": "番茄记账VIP",
    "backgroundColor": "@backgroundColor",
    "backgroundTextStyle": "@backgroundTextStyle",
    "navigationBarBackgroundColor": "#FE6853",
    "navigationBarTextStyle": "@navigationBarTextStyle",
    "navigationStyle": "custom"
  },
"tabBar": {
    "color": "@tabFontColor",//tab未选中字体颜色
    "selectedColor": "@tabSelectedColor",//tab选中字体颜色
    "backgroundColor": "@tabBgColor",//tab背景色
    "borderStyle": "@tabBorderStyle",//tab border颜色
    "list": [
      {
        "pagePath": "pages/index/index",
        "text": "首页",
        "iconPath": "@iconPath1",
        "selectedIconPath": "@selectedIconPath1"
      },
      {
        "pagePath": "pages/notebooks/notebooks",
        "text": "账本",
        "iconPath": "@iconPath2",
        "selectedIconPath": "@selectedIconPath2"
      },
      {
        "pagePath": "pages/user/user",
        "text": "我",
        "iconPath": "@iconPath3",
        "selectedIconPath": "@selectedIconPath3"
      }
    ]
  },

3.配置wxss

我这边只做了page背景适配,和主要的内容区背景的适配,工作量比较少

//app.wxss
page {
  background: #ebeaef;
  font-size: 30rpx;
  /* font-family: Microsoft YaHei; */
}
.bg_white_gray {
  background: white;
}
.page_bg_change {
  background: #ebeaef;
}

.op_bg {
  background-color: rgba(0, 0, 0, 0.2);
}

.hover-class {
  opacity: 0.8 !important;
  background: rgba(0, 0, 0, 0.1) !important;
}
/* 暗黑模式下应用的样式 */
@media (prefers-color-scheme: dark) {
  page {
    background: #000000;
  }
  .bg_white_gray {
    background: #191919;
  }
  .page_bg_change {
    background: #000000;
  }
  .op_bg {
    background-color: rgba(255, 255, 255, 0.2);
  }

  .hover-class {
    opacity: 0.8 !important;
    background: rgba(255, 255, 255, 0.2) !important;
  }
}

官方文档点这里

上效果图
⬇️
在这里插入图片描述

在这里插入图片描述

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值