后台系统更改主题【el-color-picker】

Element el-color-picker 颜色选择器的使用

设置的页面

复制代码
<script setup lang="ts">
import { getThemeColor } from "../../utils/colorChange";
import { useUserStore } from "@/store/index";
import { reactive } from "vue";
import { ref } from "vue";
const userStore = useUserStore();

const drawer = ref(false);
const handleClose = () => {
  drawer.value = false;
  colorCdt.color = "";
};
const openDrawer = (bool: boolean) => {
  drawer.value = bool;
};
const colorCdt = reactive({
  predefineColors: [
    "#ff4500",
    "#ff8c00",
    "#ffd700",
    "#90ee90",
    "#00ced1",
    "#1e90ff",
    "#c71585",
    "rgba(255, 69, 0, 0.68)",
    "rgb(255, 120, 0)",
    "hsv(51, 100, 98)",
    "hsva(120, 40, 94, 0.5)",
    "hsl(181, 100%, 37%)",
    "hsla(209, 100%, 56%, 0.73)",
    "#c7158577",
  ],
  color: "",
  isShowWaterMark: true,
});

const changeTheme = (val: any) => {
  console.log(val, "val");

  document.documentElement.style.setProperty("--theme_bgColor", val); // 设置变量值
  sessionStorage.setItem("theme_bgColor", val); // 缓存存一份
  userStore.theme_bgColor = getThemeColor(); // vuex更新一下
};
defineExpose({ openDrawer });
</script>
<template>
  <el-drawer
    v-model="drawer"
    title="设置"
    :before-close="handleClose"
    size="20%"
  >
    <div class="DrawItem">
      <span>主题更改:</span>
      <el-color-picker
        @change="changeTheme"
        v-model="colorCdt.color"
        show-alpha
        :predefine="colorCdt.predefineColors"
      >
      </el-color-picker>
    </div>
  </el-drawer>
</template>

<style lang="scss" >
.DrawItem {
  box-sizing: border-box;
  padding: 0 18px;
  display: flex;
  align-items: center;
  justify-content: space-around;
  justify-content: space-between;
  margin-bottom: 18px;
  font-size: 13px;
  font-weight: 600;
}
</style>

在store里面保存一个颜色变量值

获取主题颜色的方法写在colorChange.ts

复制代码
// 获取主题颜色
export function getThemeColor() {
  // 有缓存用缓存,没缓存用默认
  let cache_theme_bgColor = sessionStorage.getItem("theme_bgColor");
  if (cache_theme_bgColor) {
    document.documentElement.style.setProperty(
      "--theme_bgColor",
      cache_theme_bgColor
    );
    return cache_theme_bgColor;
  } else {
    let theme_bgColor = getComputedStyle(
      document.documentElement
    ).getPropertyValue("--theme_bgColor");
    return theme_bgColor;
  }
}

:root定义全局样式和变量

定义 :root 后,所有变量都将被保存在 :root 中,并且可以在整个页面的任何位置使用。这使得我们可以轻松地为网站设置一些通用的变量,如颜色、字体、间距等等

reset.css【在main.ts里全局引入】

复制代码
:root {
  --theme_bgColor: #fff;
}

在layout获取颜色的变量值【以防一刷新颜色值丢失】

如果需要让页面背景变可以再layout 获取一次颜色值

如果需要侧边栏背景色也变,可以sidebar获取颜色变量值

相关推荐
IT_陈寒2 小时前
SpringBoot这个自动配置坑我跳了三次
前端·人工智能·后端
kyriewen2 小时前
我用 AI 一周写完了整个项目,上线第一天就崩了——这是我踩过最贵的 5 个坑
前端·javascript·ai编程
Larcher3 小时前
AI Loop:让AI像人一样自主完成任务的核心机制
javascript·人工智能·设计模式
默_笙3 小时前
🃏 JS 只有 8 种数据类型,但我花了 2 天才搞懂 null 和 undefined 的区别
javascript
牧艺3 小时前
从零到协同:构建类飞书在线文档系统的五个技术重难点
前端·人工智能
jump_jump4 小时前
流式 HTML:从 htmx 片段装配到浏览器原生增量渲染
javascript·性能优化·前端工程化
红尘散仙4 小时前
想写一个像样的终端 App?试试把 React 的开发体验搬进 Rust TUI
前端·rust
袋鼠云数栈UED团队4 小时前
一套 Spec-First 的 AI 编程工作流
前端·人工智能
袋鼠云数栈前端4 小时前
一套 Spec-First 的 AI 编程工作流
前端·ai+
angerdream4 小时前
Android手把手编写儿童手机远程监控App之vue3 路由守卫
前端