切换主题 | 切换白天和夜晚模式

简介: 夜晚浏览网页太亮,试试夜晚模式吧,此以 vue3 为例

image.png

夜晚浏览网页太亮,试试夜晚模式吧,此以 vue3 为例

本项目自定义了两种模式,即白天模式和夜晚模式,当然可以添加更多的模式。

image.png

1.新建一个 base.scss 文件,里面写上需要的主题颜色。

// 主题
$themes: (
  // 白天模式
  light:
    (
      bgColor: #ebeaea,
      textColor: #121212,
      cardBgColor: #ffffff,
    ),
  // 夜间模式
  dark:
    (
      bgColor: #121212,
      textColor: #ebeaea,
      cardBgColor: #3a3a3a,
    )
);

2.新建一个 theme.scss 文件,文件主要是解析 theme(这里主要是采自网上写法)


//遍历主题map
@mixin themeify {
  @each $theme-name, $theme-map in $themes {
    //!global 把局部变量强升为全局变量
    $theme-map: $theme-map !global;
    //判断html的data-theme的属性值  #{}是sass的插值表达式
    //& sass嵌套里的父容器标识   @content是混合器插槽,像vue的slot
    [data-theme="#{$theme-name}"] & {
      @content;
    }
  }
}

//声明一个根据Key获取颜色的function
@function themed($key) {
  @return map-get($theme-map, $key);
}

//获取背景颜色
@mixin background_color($color) {
  @include themeify {
    background-color: themed($color) !important;
  }
}

//获取字体颜色
@mixin font_color($color) {
  @include themeify {
    color: themed($color) !important;
  }
}

//获取边框颜色
@mixin border_color($color) {
  @include themeify {
    border-color: themed($color) !important;
  }
}

3.新建 color.scss 文件,主要是自定义颜色样式,在项目任何地方即可使用。


@import "./theme.scss";

// 页面背景色
.page-bgColor {
  @include background_color("bgColor");
}

// 字体颜色
.basis-text-color {
  @include font_color("textColor");
}

// 导航卡片的颜色
.nav-card-bgColor {
  @include background_color("cardBgColor");
}

4.根据需求,切换主题。

假如这里有一个切换的按钮(这里使用两个 icon 代替)

<div class="theme-icon">
  <i
    class="iconfont icon-yueduqi_yewanmoshi-98 title"
    @click="handleChangeThemes"
    v-if="themeData.isDark"
  />
  <i
    class="iconfont icon-baitianmoshi light title"
    @click="handleChangeThemes"
    v-else
  />
</div>

点击切换按钮切换主题(这里是用 vue3,并将其 ts 代码分离)

新建一个 changeTheme.ts 文件


import { onMounted, reactive } from 'vue'

export const changeTheme = () => {
    const themeData = reactive({
        isDark: false
    })
    const handleChangeThemes = () => {
        themeData.isDark = !themeData.isDark
        if (themeData.isDark) {
            window.document.documentElement.setAttribute('data-theme', 'light')
        } else {
            window.document.documentElement.setAttribute('data-theme', 'dark')
        }
    }
    onMounted(() => {
        handleChangeThemes()
    })
    return { themeData, handleChangeThemes }
}

在需要使用的页面引用即可

export default defineComponent({
  name: "",
  setup() {
    const { themeData, handleChangeThemes } = changeTheme()
    return {
      themeData,
      handleChangeThemes
    }
  }
})
相关文章
|
9月前
|
JavaScript 前端开发
如何在Vite创建的Vue项目中全局注入SCSS变量/混合器?
如何在Vite创建的Vue项目中全局注入SCSS变量/混合器?
934 158
|
10月前
|
数据采集 供应链 监控
什么是RFID/EPC编码-SGTIN96
SGTIN是GS1标准下的商品序列化编码,结合GTIN与唯一序列号,实现单品级标识与追溯。广泛应用于零售、供应链、防伪等领域,支持RFID自动识别,提升管理效率。可通过工具实现EPC、GS1 Digital Link、条码格式间一键转换,并支持批量生成打印,助力智能化供应链管理。
953 4
|
Web App开发 iOS开发 MacOS
如何在浏览器中启用夜间模式?
【10月更文挑战第10天】
|
前端开发 JavaScript
饿了么UI中el-tree中的树节点选中高亮的两种常用方式(highlight-current属性)
饿了么UI中el-tree中的树节点选中高亮的两种常用方式(highlight-current属性)
1671 0
|
消息中间件 Docker 容器
Docker中的RabbitMQ已经启动运行,但是管理界面打不开
Docker中的RabbitMQ已经启动运行,但是管理界面打不开
2399 0
|
NoSQL Ubuntu 网络安全
在 Ubuntu 20.04 上安装和配置 Redis
在 Ubuntu 20.04 上安装和配置 Redis 的步骤如下:首先更新系统包,然后通过 `apt` 安装 Redis。安装后,启用并启动 Redis 服务,检查其运行状态。可选配置包括修改绑定 IP、端口等,并确保防火墙设置允许外部访问。最后,使用 `redis-cli` 测试 Redis 功能,如设置和获取键值对。
836 1
|
JavaScript
JS中splice与slice的区别【基础】
JS中splice与slice的区别【基础】
658 0
|
数据采集 API 数据格式
Python 原生爬虫教程:京东商品详情页面数据API
本文介绍京东商品详情API在电商领域的应用价值及功能。该API通过商品ID获取详细信息,如基本信息、价格、库存、描述和用户评价等,支持HTTP请求(GET/POST),返回JSON或XML格式数据。对于商家优化策略、开发者构建应用(如比价网站)以及消费者快速了解商品均有重要意义。研究此API有助于推动电商业务创新与发展。
|
JavaScript 前端开发 Shell
实现在vue中自定义主题色彩切换
实现在vue中自定义主题色彩切换
647 0

热门文章

最新文章