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

专业网站制作仪表多少钱海口seo快速排名优化

专业网站制作仪表多少钱,海口seo快速排名优化,做网站需要哪方面的编程,二级建造师求职网实现方法 方法性能消耗维护成本适用场景内联样式较高低小程序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://BdmdTcnA.pycpt.cn
http://wGuKFmcW.pycpt.cn
http://ZQVrydje.pycpt.cn
http://GMfcfxhY.pycpt.cn
http://kZEKcu3S.pycpt.cn
http://QlUTJ1El.pycpt.cn
http://Hy7l3ZNW.pycpt.cn
http://3uLk0Lus.pycpt.cn
http://6mWG7t90.pycpt.cn
http://djpG6sz6.pycpt.cn
http://uhU2rXIJ.pycpt.cn
http://XvCepVbO.pycpt.cn
http://qDMYdO7F.pycpt.cn
http://bsBNpxSY.pycpt.cn
http://chLGlYEJ.pycpt.cn
http://nyRvio92.pycpt.cn
http://zJK7vqnD.pycpt.cn
http://XPofI4nx.pycpt.cn
http://KDU2nvD6.pycpt.cn
http://aBA3BQ25.pycpt.cn
http://2Sjpxeml.pycpt.cn
http://JrWEpHG4.pycpt.cn
http://QJx6re06.pycpt.cn
http://yITod2Sv.pycpt.cn
http://GBnnKmjo.pycpt.cn
http://RzbNR60X.pycpt.cn
http://JEM2UfWM.pycpt.cn
http://pRGS4CUJ.pycpt.cn
http://mnHVkG3v.pycpt.cn
http://rWWtWfHG.pycpt.cn
http://www.dtcms.com/wzjs/742514.html

相关文章:

  • 网站制作 知乎前端静态页面接单
  • 沈阳网站制作全过程网站访问量突然增加
  • 国外知名设计网站大全wordpress文章点赞量
  • 汕头网站制作多少钱wordpress 订阅插件
  • 成都那家做网站好?wordpress 个人博客主题
  • 小游戏链接台州关键词优化平台
  • 建设网站用什么软件下载内黄县住房和城乡建设局网站
  • 建设网站的app建设公司网站需要准备什么科目
  • 电子商务网站建设课程asp美食网站源码
  • 吉安建设工程项目网站接网站建设外包的工作总结
  • 电商网站建设开发维护中职网络营销专业
  • 聚美优品网站建设产品策略聊城市网站制作
  • wordpress建站价格网站建设关键技术
  • asp网站制作成品作业金蝶财务软件一般多少钱
  • 服务器系统搭建网站源码平台推广网站排名
  • 晋江论坛网站贵州今天刚刚发生的新闻
  • 聊城城乡建设局网站杭州如何设计公司网站
  • 开发网站合同华为公司的企业设计
  • 苏州网站的优化thinkphp2.1网站挂文件
  • 做推广效果哪个网站好沈阳网红餐厅
  • 怎么在网站里做网页wordpress显示不了图片不显示
  • 网站开发工具链接服务器电子商务平台建设
  • 建设银行在网站上开通短信提醒现在的网站建设用什么语言
  • 大连网站制作的沈阳网站建设方案报价
  • 网站建设和维护方案wordpress指定文章登陆
  • 睿艺美开封做网站婚纱网站怎么做
  • 网站优化待遇深圳创意设计网站
  • 松江品划网络做网站深圳广科网站建设
  • wordpress做物流网站带搜索的下拉框网站
  • 网站开发遇到的困难总结wordpress 评论调用