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

专业网站制作仪表多少钱网站品牌词如何优化

专业网站制作仪表多少钱,网站品牌词如何优化,外链屏蔽逐步解除,找阿里巴巴购买做网站的软件实现方法 方法性能消耗维护成本适用场景内联样式较高低小程序CSS变量属性选择器低中H5混合方案中等低跨平台项目 优势特点 性能优化: H5端使用CSS原生变量切换小程序端使用高效样式字符串生成切换动画流畅 维护性提升 主题配置集中管理新增主题只需要拓展vars对象…

实现方法

方法性能消耗维护成本适用场景
内联样式较高小程序
CSS变量+属性选择器H5
混合方案中等跨平台项目

优势特点

  1. 性能优化:
    1. H5端使用CSS原生变量切换
    2. 小程序端使用高效样式字符串生成
    3. 切换动画流畅
  2. 维护性提升
    1. 主题配置集中管理
    2. 新增主题只需要拓展vars对象

使用pinia管理主题

第一步、定义主题和CSS变量

state: () => {return {mode: uni.getStorageSync('theme') || 'light',vars: {light: { '--primary': '#007AFF', '--bg': '#FFFFFF' },dark: { '--primary': '#0BB640', '--bg': '#1A1A1A' },},}
},

第二步、实现主题切换方法

  actions: {// 统一切换入口toggle(themeMode) {// 判断是否传递主题,如果没有则在dark和light之间切换if (themeMode) {this.mode = themeMode} else {this.mode = this.mode === 'light' ? 'dark' : 'light'}// 本地存储uni.setStorageSync('theme', this.mode)},}

第三步、实现原生组件适配

  actions: {// 原生组件适配updateNative() {// 判断终端类型const isMP = process.env.UNI_PLATFORM?.startsWith('mp-')const vars = this.vars[this.mode]if (isMP) {// 设置顶部导航栏样式uni.setNavigationBarColor({backgroundColor: vars['--bg'],frontColor: this.mode === 'dark' ? '#ffffff' : '#000000',})// code……}},}

第四步、H5根属性更新

actions: {// H5根属性更新updateRootAttribute() {const isH5 = process.env.UNI_PLATFORM === 'h5'if (isH5) {// 更新HTML中的data-theme属性标识document.documentElement.setAttribute('data-theme', this.mode)}},
},

完整代码

import { defineStore } from 'pinia'export const useThemeStore = defineStore('theme', {state: () => {return {mode: uni.getStorageSync('theme') || 'light',vars: {light: { '--primary': '#007AFF', '--bg': '#FFFFFF' },dark: { '--primary': '#0BB640', '--bg': '#1A1A1A' },},}},actions: {// 统一切换入口toggle(themeMode) {if (themeMode) {this.mode = themeMode} else {this.mode = this.mode === 'light' ? 'dark' : 'light'}uni.setStorageSync('theme', this.mode)// 多端样式更新this.updateNative()// #ifdef H5this.updateRootAttribute()// #endif},// 原生组件适配updateNative() {const isMP = process.env.UNI_PLATFORM?.startsWith('mp-')const vars = this.vars[this.mode]if (isMP) {uni.setNavigationBarColor({backgroundColor: vars['--bg'],frontColor: this.mode === 'dark' ? '#ffffff' : '#000000',})}},// H5根属性更新updateRootAttribute() {const isH5 = process.env.UNI_PLATFORM === 'h5'if (isH5) {document.documentElement.setAttribute('data-theme', this.mode)}},},
})

定义CSS变量

定义CSS变量,H5中使用

/* 多端兼容方案 */
:root {// 默认主题(编译时注入小程序)--primary: #007aff;--bg: #ffffff;
}// H5动态主题(运行时切换)
@media all {[data-theme='light'] {--primary: #007aff;--bg: #ffffff;}[data-theme='dark'] {--primary: #0bb640;--bg: #1a1a1a;}
}

在App.vue中导入使用

<style lang="scss">
@import '@/styles/index.scss';
</style>

创建一个Hook

通过Hook来管理主题的切换、样式格式化等,避免重复导入Store

import { computed } from 'vue'
import { useThemeStore } from '@/stores/theme'export const useTheme = () => {const themeStore = useThemeStore()// 响应式主题变量const themeVars = computed(() => {const result = {// 小程序端需要转换的样式mpStyle: null,// H5数据属性dataTheme: themeStore.mode,}if (isMP.value) {result.mpStyle = Object.entries(themeStore.vars[themeStore.mode]).map(([k, v]) => `${k}:${v}`).join(';')}return result})const isMP = computed(() => process.env.UNI_PLATFORM?.startsWith('mp-'))return {isMP,toggle: themeStore.toggle, // 切换方法currentMode: computed(() => themeStore.mode), // 当前模式themeVars, // 样式绑定对象}
}

在组件中使用

最主要的代码是::style="{ ...themeVars.mpStyle }",这样就可以实现在小程序主题切换时变量自动更新

<template><view class="container" :style="{ ...themeVars.mpStyle }"><view class="w-150px box"><text>主题切换测试</text><text class="iconfont icon-sousuo"></text><button type="primary" @tap="() => toggle()">切换主题</button></view></view>
</template>
<script setup>
import { useTheme } from '@/Hooks/useTheme'const { themeVars, toggle } = useTheme()
</script>
<style lang="scss" scoped>
.container {.box {background-color: var(--bg);font-size: 18px;font-weight: 500;line-height: 32px;color: var(--primary);}
}
</style>

主题初始化

在App.vue文件中添加如下代码

onLaunch(() => {// 主题初始化const savedTheme = uni.getStorageSync('theme')if (savedTheme) {themeStore.toggle(savedTheme)}
})

优化拓展

添加transition

在css或scss文件中添加如下代码,使主题切换时更加流畅的过渡,避免生硬切换

* {transition: background-color 0.3s, background 0.3s, color 0.3s;
}

监听系统主题变化

在App.vue文件中使用uni.onThemeChange监听系统主题变化,并同步小程序/H5主题变化

onLaunch(() => {// 监听系统主题变化uni.onThemeChange(({ theme }) => {const systemTheme = theme === 'dark' ? 'dark' : 'light'themeStore.toggle(systemTheme)})
})

新增主题?

如果想要新增主题,只需要在stores/theme.jsstyle/index.scss文件中添加对应主题的CSS变量,theme.js中定义小程序的主题,index.scss定义H5的主题,如:

state: () => {return {vars: {light: { '--primary': '#007AFF', '--bg': '#FFFFFF' },dark: { '--primary': '#0BB640', '--bg': '#1A1A1A' },red: {// ……新变量}},}
},
// H5动态主题(运行时切换)
@media all {[data-theme='light'] {--primary: #007aff;--bg: #ffffff;}[data-theme='dark'] {--primary: #0bb640;--bg: #1a1a1a;}[data-theme='red'] {/* ……新变量 */}
}

注意事项

  1. :root选择器:用于匹配文档的根元素(在 HTML 文档中即 标签)。它是定义全局 CSS 变量的最佳位置,尤其在主题切换场景中发挥关键作用。
  2. @media all媒体查询:所有设备上都生效
  3. 请不要将index.scss中的代码放到uni.scss中,这样可能导致切换主题时不生效

文章转载自:

http://mpV4O6BY.dpmkn.cn
http://Eb8VEnks.dpmkn.cn
http://QsYrgpZ0.dpmkn.cn
http://WEcFTRO8.dpmkn.cn
http://C1KFFQzY.dpmkn.cn
http://2HIefrP9.dpmkn.cn
http://TVKWz2DG.dpmkn.cn
http://JxhhWUjV.dpmkn.cn
http://5ul2MGt9.dpmkn.cn
http://n3tgcoq9.dpmkn.cn
http://99Qtr3fD.dpmkn.cn
http://paBuLikR.dpmkn.cn
http://j5bowvdu.dpmkn.cn
http://IL80WrHb.dpmkn.cn
http://yxvIrBw4.dpmkn.cn
http://sePJG1T1.dpmkn.cn
http://9dGWjv5G.dpmkn.cn
http://06WS3h26.dpmkn.cn
http://TIflhuK6.dpmkn.cn
http://ne7JCpVb.dpmkn.cn
http://QVuLGuNF.dpmkn.cn
http://WJFS9ofo.dpmkn.cn
http://yRumeVE2.dpmkn.cn
http://LmSSHBcm.dpmkn.cn
http://4X5fHVvA.dpmkn.cn
http://aSMPNNOx.dpmkn.cn
http://oGrhpIlz.dpmkn.cn
http://WNman7Dr.dpmkn.cn
http://CjcKKLfR.dpmkn.cn
http://xzBsZsxr.dpmkn.cn
http://www.dtcms.com/wzjs/716833.html

相关文章:

  • 成都网站建设 培训织梦网站建设选项卡教程
  • iis 网站乱码HTML发布网站
  • 学平面设计网站小说网站建站程序
  • 襄阳做网站的公司有哪些企业vi设计公司上海设计公司
  • html网站开发工具有哪些做站群的网站怎么来
  • 租赁模板建站 网站的名称归属wordpress 分类目录 菜单
  • 广州做鞋的网站做断桥铝最知名的网站
  • 网站建设立项报告二室一厅60平米装修案例
  • 搜狐快站装修网站建设厨师培训学校
  • 专业的深圳网站建设.net网站程序
  • 南宁保障住房建设管理服务中心网站深圳自己的网站建设
  • 罗湖在线seo需要会网站建设吗
  • seo网站济南做网站最好的单位
  • 艺术网站建设公司二手书店网站建设规划书
  • 快速搭建网站的工具有哪些毕业设计网站建设体会
  • 扬州市住房和建设局网站用sql做简单的博客网站
  • 完整网站开发教程哪些外贸网站可以做soho
  • 自己可以做视频网站吗贵阳做网站哪家公司好
  • 0基础1小时网站建设教程网站建设大赛海报
  • 济宁神华 网站建设升级wordpress需要ftp
  • 外贸网站建设报价杭州公司社保缴纳时间
  • 贵阳做网站kuhugz静态网站数据库
  • 连云港网站建设费用wordpress伪静态 宝塔
  • 做网站php语言用什么工具西安企业网站建设多少钱
  • 企业网站建设费用怎么入账wordpress编辑器文件大小
  • 做亚马逊和淘宝网站自动做网站的ai
  • 南通大型网站建设互联网 现代农业网站建设
  • 海报生成器在线制作推广排名seo
  • .net flash网站模板做网站比较好的软件
  • 做网站和微信公众号需要多少钱建设公司起名大全字库