WeChat applet implements night mode for skin
This article mainly introduces you to the relevant information about using WeChat applet to realize skin function, that is, to realize night mode. The article introduces it in detail through sample code, which has certain reference and learning value for everyone. Friends who need it Let’s take a look together below.
Old rule, show the renderings first
<switch bindchange="switchChange" color ="#F39C89" class="switch"/>
Page({ data: { skinStyle: "" }, onLoad: function (options) { }, switchChange:function(e){ var that =this var style //如果开启 if(e.detail.value == true){ style="dark" }else{ //否则 style.skin = "" } //保存信息 that.setData({ skinStyle: style }) } })The button function is OK, now let’s go Write style
/*夜间模式*/ /****个人信息页面****/ .dark-box{ background: #000 !important; } /*用户信息部分*/ .dark-box .user-box{ background: #333 !important; color: #999; } /*列表部分*/ .dark-box .extra-box{ background: #333 !important; } .dark-box .extra-box .extra-item{ border-bottom: 1px solid #000 !important; } .dark-box .extra-box .item-head{ color: #999; } .dark-box .between-box{ background: #333 !important; } .dark-box .between-left{ background: #333 !important; } .dark-box .between-left .item-head{ color: #999; } /****个人信息页面结束****/Everyone noticed that my style names all have dark-box
This dark-box is the outermost and largest box (except the default page)
<view class="my-box {{skinStyle}}-box">Of course you can also You can write a skin style, yellow, red, or blue. . .
Now with this way of writing, we can change the skin style just by controlling the value of the variable skinStyle
We can also write a blue-box skin and then set the variable Just set skinStyle to blue
@import "../../skin/dark.wxss";Continue Come to the second step, it's easy. .
To set it to a global variable, just getApp() first and then pass it over
var app=getApp() Page({ data: { skinStyle: "" }, onLoad: function (options) { }, switchChange:function(e){ var that =this //设置全局变量 if(e.detail.value == true){ app.globalData.skin="dark" }else{ app.globalData.skin = "" } that.setData({ skinStyle: app.globalData.skin }) } })Now I am visiting other pages When the time comes, the dark skin will also be transferred in
I only wrote one page, so only this page will change
var app=getApp() Page({ data: { skinStyle: "" }, onLoad: function (options) { }, switchChange:function(e){ var that =this //设置全局变量 if(e.detail.value == true){ app.globalData.skin="dark" }else{ app.globalData.skin = "" } that.setData({ skinStyle: app.globalData.skin }) //保存到本地 wx.setStorage({ key: "skin", data: app.globalData.skin }) } })Are you done? not at all. .
We need to get the skin settings when the program is opened
So we need to get the skin-related information in app.js
getSkin:function(){ var that =this wx.getStorage({ key: 'skin', success: function (res) { that.globalData.skin=res.data } }) }Now we set the black skin, and then exit. After entering, it is not black
Because we did not set it when the page was loaded
onLoad: function (options) { var that =this that.setData({ skinStyle: app.globalData.skin }) }
Now let’s take a look
The skin is OK
Because the switch has been reset
This is left to everyone to solve. Just make a judgment when starting up
Introduction to vidao’s video playback and barrage functions in WeChat Mini Program
WeChat Mini Program Develop a circular menu (imitation of the CCB circular menu)
The above is the detailed content of WeChat applet implements night mode for skin. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use