码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 使用 document.documentElement.style 实现主题换肤


    目录

    1. document.documentElement 是什么?

    1.1 document.documentElement

    1.2 document.documentElement.style

    1.3 打印效果

    2. document.documentElement.style 相关的 JavaScript 方法

    2.1 判断浏览器是否支持指定的 css 属性

    2.2 判断浏览器是否支持指定的 css3 属性(带前缀)

    2.3 使用 window.CSS.supports 判断 css 的属性和值

    3. 使用 document.documentElement.style 实现换肤功能

    3.1 原理概述

    3.2 实现一下

    3.2.1 初始化颜色变量

    3.2.2 将十六进制颜色,转换为 rgb 格式

    3.2.3 从本地获取用户设置的样式信息,根据样式信息更新全局的主题色、字号


    1. document.documentElement 是什么?

    1.1 document.documentElement

    对于任何非空 HTML 文档,调用 document.documentElement 会返回一个 元素,也就是该文档的根元素

    借助 document.documentElement 只读属性,能方便地获取到 html 文档的根元素

    注意:IE浏览器中有兼容性问题,应该使用 document.body 替代 document.documentElement

    1.2 document.documentElement.style

    document.documentElement.style 属性定义了 当前浏览器支持的 所有 css 属性

    举个栗子,获取字号属性 —— document.documentElement.style.fontSize

    1.3 打印效果

    2. document.documentElement.style 相关的 JavaScript 方法

    2.1 判断浏览器是否支持指定的 css 属性

    1. /**
    2. * 判断浏览器是否支持指定的 css 属性
    3. * @param cssName 样式属性名称
    4. **/
    5. function support(cssName) {
    6. var htmlStyle = document.documentElement.style;
    7. if (cssName in htmlStyle)
    8. return true;
    9. return false;
    10. }
    11. alert(support('animate')); // false
    12. alert(support('animation')); // true

    2.2 判断浏览器是否支持指定的 css3 属性(带前缀)

    1. /**
    2. * 判断浏览器是否支持指定的 css3 属性(带前缀)
    3. * @param style 样式属性名称
    4. */
    5. function supportCss3(style) {
    6. var prefix = ["webkit", "Moz", "ms", "o"],
    7. i,
    8. humpString = [],
    9. htmlStyle = document.documentElement.style,
    10. _toHumb = function (string) {
    11. return string.replace(/-(\w)/g, function ($0, $1) {
    12. return $1.toUpperCase();
    13. });
    14. };
    15. for (i in prefix) humpString.push(_toHumb(prefix[i] + "-" + style));
    16. humpString.push(_toHumb(style));
    17. for (i in humpString) if (humpString[i] in htmlStyle) return true;
    18. return false;
    19. }
    20. alert(supportCss3("animation")); // true

    2.3 使用 window.CSS.supports 判断 css 的属性和值

    1. console.info(window.CSS);
    2. console.info(window.CSS.supports("animation")); // false
    3. console.info(window.CSS.supports("animate")); // false
    4. console.info(window.CSS.supports("animation", "liear")); // true
    5. console.info(window.CSS.supports("border", "1px solid #F00")); // true

    打印结果如下: 

    3. 使用 document.documentElement.style 实现换肤功能

    3.1 原理概述

    • 定义一套主题色 css 变量,每种主题色对应不同的透明度
    • 系统中的样式,全部使用 css 变量进行设置
    • 系统初始化时,获取用户选择的主题色,并更新到已经声明的 css 变量中

    3.2 实现一下

    3.2.1 初始化颜色变量

    • 在 css :root 内定义主题色变量
    • 使用 css 变量替换原来的样式
    1. :root {
    2. --theme-color: #33B897;
    3. --theme-color-opacity-10: rgba(51, 184, 151, 0.1);
    4. --theme-color-opacity-15: rgba(51, 184, 151, 0.15);
    5. --theme-color-opacity-20: rgba(51, 184, 151, 0.2);
    6. --theme-color-opacity-30: rgba(51, 184, 151, 0.3);
    7. --theme-color-opacity-50: rgba(51, 184, 151, 0.5);
    8. --theme-color-opacity-60: rgba(51, 184, 151, 0.6);
    9. --theme-color-opacity-80: rgba(51, 184, 151, 0.8);
    10. --theme-color-opacity-90: rgba(51, 184, 151, 0.9);
    11. --base-font-size: 15px;
    12. }
    13. .test-parent .test-child {
    14. // border: 1px solid #33b897;
    15. border: 1px solid var(--theme-color);
    16. }

     关于 :root 选择器,可以阅读下方的文章:

    css变量赋值和取值的四种方式(js设置值、内联样式、:root选择器、html选择器) - UCloud云社区今天突然发现一个有趣的现象document.querySelector(':root') === document.documentElementhttps://www.ucloud.cn/yun/117360.html

    注意:编码格式 utf-8 

    编码格式有的时候可以解决一些特殊的问题,比如:

    • 伪元素中的 content,如果使用中文,可能会出现乱码;
    • 正确的设置方式:将文字转换为 Unicode 编码,去掉 u,填入 content 

    举个栗子~:

    1. /* 年 - 文字 */
    2. .WdateDiv #dpTitle > div:nth-child(4)::before {
    3. content: "\5e74";
    4. position: absolute;
    5. left: 46px;
    6. bottom: 0;
    7. font-size: 16px;
    8. color: #606266;
    9. }

    参考文章:

    前端 - 伪元素 content 出现中文乱码_个人文章 - SegmentFault 思否伪元素的 content 使用中文字符在某些浏览器可能会出现乱码:代码如下: {代码...} 解决方法首先,确认 charset 为 utf-8确保 HTML 的 META 属性设置为 chars...https://segmentfault.com/a/1190000042077497?utm_source=sf-similar-article

    3.2.2 将十六进制颜色,转换为 rgb 格式

    1. /**
    2. * 十六进制颜色转换 rgb
    3. * @param color 十六进制颜色
    4. */
    5. function colorRgb(color) {
    6. var sColor = color.toLowerCase();
    7. // 十六进制颜色值的正则表达式
    8. const reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
    9. // 如果是16进制颜色
    10. if (sColor && reg.test(sColor)) {
    11. if (sColor.length === 4) {
    12. var sColorNew = "#";
    13. for (var i = 1; i < 4; i += 1) {
    14. sColorNew += sColor
    15. .slice(i, i + 1)
    16. .concat(sColor.slice(i, i + 1));
    17. }
    18. sColor = sColorNew;
    19. }
    20. // 处理六位的颜色值
    21. const sColorChange = [];
    22. for (var i = 1; i < 7; i += 2) {
    23. sColorChange.push(parseInt("0x" + sColor.slice(i, i + 2)));
    24. }
    25. return sColorChange.join(",");
    26. }
    27. return sColor;
    28. }

    3.2.3 从本地获取用户设置的样式信息,根据样式信息更新全局的主题色、字号

    1. // 从本地存储中获取用户设置的样式信息 userStyle
    2. // userStyle = '{"themeColor":"#E76950","arrowOrEllipsis":"ellipsis","fontSize":14}';
    3. var userStyle = localStorage.getItem("userStyle");
    4. // 存储 本地存储信息转换的对象
    5. var userStyleJson = {};
    6. if (userStyle) {
    7. userStyleJson = JSON.parse(userStyle);
    8. // 将 十六进制的颜色 换为 rgb
    9. var themeColor = colorRgb(userStyleJson.themeColor);
    10. // 获取 当前浏览器支持的 所有 css 属性
    11. var userStyleCss = document.documentElement.style;
    12. // 设置不同透明度的主题色
    13. userStyleCss.setProperty(
    14. "--theme-color",
    15. "rgba(" + themeColor + ", 1)"
    16. );
    17. userStyleCss.setProperty(
    18. "--theme-color-opacity-10",
    19. "rgba(" + themeColor + ", .1)"
    20. );
    21. userStyleCss.setProperty(
    22. "--theme-color-opacity-15",
    23. "rgba(" + themeColor + ", .15)"
    24. );
    25. userStyleCss.setProperty(
    26. "--theme-color-opacity-20",
    27. "rgba(" + themeColor + ", .2)"
    28. );
    29. userStyleCss.setProperty(
    30. "--theme-color-opacity-30",
    31. "rgba(" + themeColor + ", .3)"
    32. );
    33. userStyleCss.setProperty(
    34. "--theme-color-opacity-50",
    35. "rgba(" + themeColor + ", .5)"
    36. );
    37. userStyleCss.setProperty(
    38. "--theme-color-opacity-60",
    39. "rgba(" + themeColor + ", .6)"
    40. );
    41. userStyleCss.setProperty(
    42. "--theme-color-opacity-80",
    43. "rgba(" + themeColor + ", .8)"
    44. );
    45. userStyleCss.setProperty(
    46. "--theme-color-opacity-90",
    47. "rgba(" + themeColor + ", .9)"
    48. );
    49. // 设置字号大小
    50. userStyleCss.setProperty("--fontSize", userStyleJson.fontSize + "px");
    51. }

  • 相关阅读:
    小公司的前端建设的一些思考
    字节面试官:“这92道 Spring Boot 面试题都答不上来?”
    Elasticsearch中的评分排序--Function score query
    K8S安装过程六:etcd 集群安装
    “蔚来杯“2022牛客暑期多校训练营8 D题: Poker Game: Decision
    pytest合集(9)— Hooks钩子函数
    2022年全国职业院校技能大赛:网络系统管理项目-模块B--Windows样题7
    28李沐动手学深度学习v2/卷积神经网络,LeNet
    本周遇到的一些问题记录
    C++11新增特性lambda表达式
  • 原文地址:https://blog.csdn.net/Lyrelion/article/details/127764935
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号