目录
1. document.documentElement 是什么?
1.2 document.documentElement.style
2. document.documentElement.style 相关的 JavaScript 方法
2.3 使用 window.CSS.supports 判断 css 的属性和值
3. 使用 document.documentElement.style 实现换肤功能
3.2.3 从本地获取用户设置的样式信息,根据样式信息更新全局的主题色、字号
对于任何非空 HTML 文档,调用 document.documentElement 会返回一个 元素,也就是该文档的根元素
借助 document.documentElement 只读属性,能方便地获取到 html 文档的根元素
注意:IE浏览器中有兼容性问题,应该使用 document.body 替代 document.documentElement
document.documentElement.style 属性定义了 当前浏览器支持的 所有 css 属性
举个栗子,获取字号属性 —— document.documentElement.style.fontSize

- /**
- * 判断浏览器是否支持指定的 css 属性
- * @param cssName 样式属性名称
- **/
- function support(cssName) {
- var htmlStyle = document.documentElement.style;
- if (cssName in htmlStyle)
- return true;
- return false;
- }
- alert(support('animate')); // false
- alert(support('animation')); // true
- /**
- * 判断浏览器是否支持指定的 css3 属性(带前缀)
- * @param style 样式属性名称
- */
- function supportCss3(style) {
- var prefix = ["webkit", "Moz", "ms", "o"],
- i,
- humpString = [],
- htmlStyle = document.documentElement.style,
- _toHumb = function (string) {
- return string.replace(/-(\w)/g, function ($0, $1) {
- return $1.toUpperCase();
- });
- };
- for (i in prefix) humpString.push(_toHumb(prefix[i] + "-" + style));
- humpString.push(_toHumb(style));
- for (i in humpString) if (humpString[i] in htmlStyle) return true;
- return false;
- }
- alert(supportCss3("animation")); // true
- console.info(window.CSS);
- console.info(window.CSS.supports("animation")); // false
- console.info(window.CSS.supports("animate")); // false
- console.info(window.CSS.supports("animation", "liear")); // true
- console.info(window.CSS.supports("border", "1px solid #F00")); // true
打印结果如下:

- :root {
- --theme-color: #33B897;
- --theme-color-opacity-10: rgba(51, 184, 151, 0.1);
- --theme-color-opacity-15: rgba(51, 184, 151, 0.15);
- --theme-color-opacity-20: rgba(51, 184, 151, 0.2);
- --theme-color-opacity-30: rgba(51, 184, 151, 0.3);
- --theme-color-opacity-50: rgba(51, 184, 151, 0.5);
- --theme-color-opacity-60: rgba(51, 184, 151, 0.6);
- --theme-color-opacity-80: rgba(51, 184, 151, 0.8);
- --theme-color-opacity-90: rgba(51, 184, 151, 0.9);
- --base-font-size: 15px;
- }
-
- .test-parent .test-child {
- // border: 1px solid #33b897;
- border: 1px solid var(--theme-color);
- }
关于 :root 选择器,可以阅读下方的文章:
注意:编码格式 utf-8

编码格式有的时候可以解决一些特殊的问题,比如:
举个栗子~:
- /* 年 - 文字 */
- .WdateDiv #dpTitle > div:nth-child(4)::before {
- content: "\5e74";
- position: absolute;
- left: 46px;
- bottom: 0;
- font-size: 16px;
- color: #606266;
- }
参考文章:
- /**
- * 十六进制颜色转换 rgb
- * @param color 十六进制颜色
- */
- function colorRgb(color) {
- var sColor = color.toLowerCase();
- // 十六进制颜色值的正则表达式
- const reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
- // 如果是16进制颜色
- if (sColor && reg.test(sColor)) {
- if (sColor.length === 4) {
- var sColorNew = "#";
- for (var i = 1; i < 4; i += 1) {
- sColorNew += sColor
- .slice(i, i + 1)
- .concat(sColor.slice(i, i + 1));
- }
- sColor = sColorNew;
- }
- // 处理六位的颜色值
- const sColorChange = [];
- for (var i = 1; i < 7; i += 2) {
- sColorChange.push(parseInt("0x" + sColor.slice(i, i + 2)));
- }
- return sColorChange.join(",");
- }
- return sColor;
- }
-
- // 从本地存储中获取用户设置的样式信息 userStyle
- // userStyle = '{"themeColor":"#E76950","arrowOrEllipsis":"ellipsis","fontSize":14}';
- var userStyle = localStorage.getItem("userStyle");
- // 存储 本地存储信息转换的对象
- var userStyleJson = {};
-
-
- if (userStyle) {
- userStyleJson = JSON.parse(userStyle);
- // 将 十六进制的颜色 换为 rgb
- var themeColor = colorRgb(userStyleJson.themeColor);
-
- // 获取 当前浏览器支持的 所有 css 属性
- var userStyleCss = document.documentElement.style;
-
- // 设置不同透明度的主题色
- userStyleCss.setProperty(
- "--theme-color",
- "rgba(" + themeColor + ", 1)"
- );
- userStyleCss.setProperty(
- "--theme-color-opacity-10",
- "rgba(" + themeColor + ", .1)"
- );
- userStyleCss.setProperty(
- "--theme-color-opacity-15",
- "rgba(" + themeColor + ", .15)"
- );
- userStyleCss.setProperty(
- "--theme-color-opacity-20",
- "rgba(" + themeColor + ", .2)"
- );
- userStyleCss.setProperty(
- "--theme-color-opacity-30",
- "rgba(" + themeColor + ", .3)"
- );
- userStyleCss.setProperty(
- "--theme-color-opacity-50",
- "rgba(" + themeColor + ", .5)"
- );
- userStyleCss.setProperty(
- "--theme-color-opacity-60",
- "rgba(" + themeColor + ", .6)"
- );
- userStyleCss.setProperty(
- "--theme-color-opacity-80",
- "rgba(" + themeColor + ", .8)"
- );
- userStyleCss.setProperty(
- "--theme-color-opacity-90",
- "rgba(" + themeColor + ", .9)"
- );
- // 设置字号大小
- userStyleCss.setProperty("--fontSize", userStyleJson.fontSize + "px");
- }