面试题答案
一键面试1. 变量命名规范
采用 BEM(Block - Element - Modifier)类似的命名风格,以明确变量所属的组件或模块。例如:
- 全局变量:
--global - color - primary
- 组件变量:
--button - color - normal
2. 组织变量文件
- 全局变量文件:创建
styles/global/variables.css
,存放整个项目通用的变量,如主色调、字体等。 - 组件变量文件:在每个组件的目录下创建
variables.css
。例如components/button/variables.css
,存放该组件独有的变量。
3. 避免变量命名冲突
- 使用命名空间:如前文命名规范,每个变量名带上所属组件或模块的前缀。
- 局部作用域:组件变量在组件内部作用域使用,通过
:host
(对于自定义元素)或局部选择器限制变量作用范围。
4. 实现主题切换的高效性和可维护性
- 使用媒体查询和 CSS 自定义属性:通过
media - query
或 JavaScript 动态切换根元素(:root
)上的 CSS 变量值。 - 预处理工具:使用 Sass 或 Less 进行变量管理和主题切换逻辑封装,便于维护和复用。
项目结构示例
project - root
├── src
│ ├── components
│ │ ├── button
│ │ │ ├── button.css
│ │ │ ├── button.js
│ │ │ └── variables.css
│ │ └──...
│ ├── styles
│ │ ├── global
│ │ │ └── variables.css
│ │ └── main.css
│ └── index.html
└──...
关键代码示例
- 全局变量文件
styles/global/variables.css
:root {
--global - color - primary: #007bff;
--global - font - family: Arial, sans - serif;
}
- 组件变量文件
components/button/variables.css
:host {
--button - color - normal: var(--global - color - primary);
--button - color - hover: #0056b3;
}
- 主题切换(通过 JavaScript 动态切换
:root
变量值)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<link rel="stylesheet" href="styles/main.css">
</head>
<body>
<button onclick="switchTheme()">切换主题</button>
<script>
function switchTheme() {
const root = document.documentElement;
if (root.style.getPropertyValue('--global - color - primary') === '#007bff') {
root.style.setProperty('--global - color - primary', '#ff0000');
// 其他深色主题变量切换
} else {
root.style.setProperty('--global - color - primary', '#007bff');
// 其他浅色主题变量切换
}
}
</script>
</body>
</html>