因为是一个二期项目,需要在一期的基础上做一些风格的改变,但是苦于没有产品和UI,所以这个事情就落到了前端的头上,一开始只是想的统一一下按钮和标题的颜色(这里就用到了var),顺便做些颜色改动比较方便,后面直接要求做一个动态主题切换(当然只是颜色的切换),所以就顺便研究了一下利用var来实现主题颜色切换。
var(custom-property-name, value)
:root{ --theme-color: blue; --theme-border: 1px solid var(--theme-color, #ccc);}.demo-txt{ color: var(--theme-color);}.demo-btn{ border: var(--theme-border);}
:root{
--theme-color: blue;
--theme-border: 1px solid var(--theme-color, #ccc);
}
.demo-txt{
color: var(--theme-color);
.demo-btn{
border: var(--theme-border);
<html>
:root
:root{ --theme-color: #ff6a00; --font-color: #333; --bg-color: #fff; --panel-bg-color: #eee; --border: 1px solid #ddd; /** more attribute **/}:root[theme='blue']{ --theme-color: #2563eb;}:root[theme='dark']{ filter: invert(1) brightness(1);}
--theme-color: #ff6a00;
--font-color: #333;
--bg-color: #fff;
--panel-bg-color: #eee;
--border: 1px solid #ddd;
/** more attribute **/
:root[theme='blue']{
--theme-color: #2563eb;
:root[theme='dark']{
filter: invert(1) brightness(1);
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Dynamic Theme Demo</title> <link rel="stylesheet" href="theme.css"> <link rel="stylesheet" href="index.css"></head><body> <section class="container"> <header class="header">头部标题</header> <main class="main"> <div class="con"> <p>主题说明:链接文字采用主题色,按钮背景采用主题色</p> <a href="#">链接</a><br> <button>按钮</button> </div> <div class="radio-group"> <input id="default" type="radio" name="theme" value="defalut" checked/><label for="default">默认</label> <input id="blue" type="radio" name="theme" value="blue" /><label for="blue">蓝色</label> <input id="dark" type="radio" name="theme" value="dark" /><label for="dark">暗黑</label> </div> </main> <footer class="footer">页面底部</footer> </section> <script> var themes = document.getElementsByName("theme"); Array.from(themes).forEach(themeRadio => { themeRadio.addEventListener("click", function(){ document.documentElement.setAttribute("theme", this.value); }) }) </script></body></html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dynamic Theme Demo</title>
<link rel="stylesheet" href="theme.css">
<link rel="stylesheet" href="index.css">
</head>
<body>
<section class="container">
<header class="header">头部标题</header>
<main class="main">
<div class="con">
<p>主题说明:链接文字采用主题色,按钮背景采用主题色</p>
<a href="#">链接</a><br>
<button>按钮</button>
</div>
<div class="radio-group">
<input id="default" type="radio" name="theme" value="defalut" checked/><label for="default">默认</label>
<input id="blue" type="radio" name="theme" value="blue" /><label for="blue">蓝色</label>
<input id="dark" type="radio" name="theme" value="dark" /><label for="dark">暗黑</label>
</main>
<footer class="footer">页面底部</footer>
</section>
<script>
var themes = document.getElementsByName("theme");
Array.from(themes).forEach(themeRadio => {
themeRadio.addEventListener("click", function(){
document.documentElement.setAttribute("theme", this.value);
})
</script>
</body>
</html>
添加样式文件index.html, 内容如下:
*{ padding: 0; margin: 0; border: 0; font-size: var(--font-size); color: var(--font-color);}html, body{ width: 100%; height: 100%; background-color: var(--body-bg-color);}a{ color: var(--theme-color);}button{ background-color: var(--theme-color); color: var(--body-bg-color); line-height: 20px; padding: 10px;}.container{ display: flex; flex-direction: column; width: 70%; height: 100%; margin: 0 auto; background-color: var(--body-bg-color);}.container .header{ --line-height: 40px; height: var(--line-height); line-height: var(--line-height); padding: var(--content-padding); font-size: var(--font-size-lg); box-shadow: var(--boxshadow); background-color: var(--bg-color);}.container .main{ flex: 1; line-height: 30px; padding: var(--content-padding);}.container .footer{ --line-height: 30px; height: var(--line-height); line-height: var(--line-height); padding: var(--content-padding); font-size: var(--font-size-sm); background-color: var(--bg-color-lg);}
*{
padding: 0;
margin: 0;
border: 0;
font-size: var(--font-size);
color: var(--font-color);
html, body{
width: 100%;
height: 100%;
background-color: var(--body-bg-color);
a{
button{
background-color: var(--theme-color);
color: var(--body-bg-color);
line-height: 20px;
padding: 10px;
.container{
display: flex;
flex-direction: column;
width: 70%;
margin: 0 auto;
.container .header{
--line-height: 40px;
height: var(--line-height);
line-height: var(--line-height);
padding: var(--content-padding);
font-size: var(--font-size-lg);
box-shadow: var(--boxshadow);
background-color: var(--bg-color);
.container .main{
flex: 1;
line-height: 30px;
.container .footer{
--line-height: 30px;
font-size: var(--font-size-sm);
background-color: var(--bg-color-lg);
主要就是通过切换html的属性来达到切换的效果。
使用var切换主题的方式比较简单,而且对统一页面样式以及后期维护有很大帮助,适用于对兼容性要求不高的(IE15以上支持)需求。如果需要兼容性更高的主题切换,可以参考张鑫旭老师的这篇文章《link rel=alternate网站换肤功能最佳实现》。
https://zhuanlan.zhihu.com/p/60975003
https://www.cnblogs.com/jieli/p/15806735.html
原文链接:http://www.cnblogs.com/jieli/p/15806735.html
本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728