当前位置: 首页 > news >正文

Sass实现网页背景主题切换

Sass 实现网页背景主题切换

  • 前言
  • 准备工作
  • 一、 简单的两种主题黑白切换
    • 1.定义主题
    • 2. 添加主题切换功能
    • 3. 修改 data-theme 属性
  • 二、多种主题切换
    • 1. 定义主题
    • 2. 动态生成 CSS 变量
      • 1.遍历列表
      • 2.遍历映射
      • 3.高级用法
    • 3. 设置默认主题
    • 4. 切换功能HTML
  • 三、多种主题多种样式切换
    • 1. 定义主题
    • 2. 动态生成 CSS 变量
    • 3. 设置默认主题
    • 4. HTML代码
  • ending

前言

网页实现主题切换一般有两种主流的解决办法,一种是css变量,另外一种是使用预编译器(less、sass),这里考虑到兼容性使用的是sass来实现的
原理:修改网页中html标签内的自定义属性data-theme的样式。

准备工作

  1. 如果是工程项目则需使用下面代码进行安装
 npm install sass --save-devnpm install sass-loader --save-dev
  1. 如果是html,则需要下载这个插件
    在这里插入图片描述
    使用方法也很简单在scss文件中点击这个就自动编译为css文件
    在这里插入图片描述
    这里的demo采用的是第二种

一、 简单的两种主题黑白切换

先看效果
请添加图片描述

1.定义主题

创建一个 index.scss 文件,用于定义不同的主题。
使用 Sass 的 map 和 mixin 功能来管理主题变量。

// index.scss
$themes: (light: (background-color: #ffffff,color: #000000),dark: (background-color: #000000,color: #ffffff)
);@mixin theme($name) {$theme: map-get($themes, $name);:root {background-color: map-get($theme, background-color);color: map-get($theme, color);}
}// 默认主题
@include theme(light);

map-get 方法语法:

map.get(map,key): $map: 要操作的map变量, $key: 要获取值的键。如果map中不存在该键,则该方法会返回null。

2. 添加主题切换功能

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>sass实现主题切换</title><link rel="stylesheet" href="index.css"> 
</head><body><button id="theme-toggle">切换主题</button><script>const button = document.getElementById('theme-toggle');let currentTheme = 'light';button.addEventListener('click', () => {currentTheme = currentTheme === 'light' ? 'dark' : 'light';document.documentElement.setAttribute('data-theme', currentTheme);});</script>
</body></html>

3. 修改 data-theme 属性

// index.scss
:root[data-theme='light'] {background-color: #ffffff;color: #000000;
}:root[data-theme='dark'] {background-color: #000000;color: #ffffff;
}

二、多种主题切换

假如主题不止一套…
先看效果
请添加图片描述
具体实现跟方法一差不多,只是多用了@each这个方法

1. 定义主题

每个主题都有相应的背景色和文字色。

// index.scss
$themes: (light: (background-color: #ffffff,color: #000000,),dark: (background-color: #000000,color: #ffffff,),ocean: (background-color: #00aaff,color: #ffffff,),forest: (background-color: #228b22,color: #ffffff,),
);

2. 动态生成 CSS 变量

使用 @each 循环遍历 Sass map,为每个主题生成 CSS 变量。这里使用 的是映射

// index.scss
@mixin theme($name) {$theme: map-get($themes, $name);:root[data-theme='#{$name}'] {background-color: map-get($theme, background-color);color: map-get($theme, color);}
} @each $name, $properties in $themes {@include theme($name);
}

关于@each 的使用方法

1.遍历列表

需要遍历一个列表(数组)时,可以使用 @each。每次迭代都会将当前项分配给一个指定的变量。

$colors: red, blue, green;@each $color in $colors {.text-#{$color} {color: $color;}
}

2.遍历映射

需要遍历一个映射(键值对)时,@each 可以用来遍历每个键值对。每次迭代都会将键和值分别分配给指定的变量。

$themes: (light: #ffffff,dark: #000000,ocean: #00aaff,forest: #228b22
);@each $name, $color in $themes {.theme-#{$name} {background-color: $color;}
}

3.高级用法

也可以使用 @each 来处理复杂的嵌套数据结构

$themes: (light: (background-color: #ffffff,text-color: #000000),dark: (background-color: #000000,text-color: #ffffff)
);@each $name, $properties in $themes {:root[data-theme="#{$name}"] {@each $property, $value in $properties {--#{$property}: #{$value};}}
}

3. 设置默认主题

// index.scss
@include theme(light);

4. 切换功能HTML

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>sass实现主题切换</title><link rel="stylesheet" href="index.css">
</head><body><button onclick="changeThemes('light')">切换白色</button><button onclick="changeThemes('dark')">切换黑色</button><button onclick="changeThemes('ocean')">切换蓝色</button><button onclick="changeThemes('forest')">切换绿色</button><script>function changeThemes(theme) {document.documentElement.setAttribute('data-theme', theme);}//页面初始化的时候加载,当然也可以使用其他方法changeThemes('light')</script>
</body>
</html>

三、多种主题多种样式切换

现在主题里面的变量不仅仅只有background-color,color 这两种,还可能会有其他变量,但是不可能又在root里面去加新增的代码,这样造成了冗余,维护起来也麻烦,于是有了新的思路代码…
先看效果
请添加图片描述

1. 定义主题

在这里每个主题里面自定义的css样式都是不一样的,这里只是举例

// index.scss
$themes: (light: (background-color: #ffffff,color: #000000,font-size:18px,font-weight: bold,),dark: (background-color: #000000,color: #ffffff,font-size:22px,border: 1px solid #9d11b9,),ocean: (background-color: #00aaff,color: red,font-size:26px,font-weight: bold,border: 1px solid #318b97,),forest: (background-color: #228b22,color: yellow,font-size:30px,border: 1px solid #c2557b,),
);

2. 动态生成 CSS 变量

这里使用的是@each的高级用法来处理复杂的嵌套数据结构

@each $name, $properties in $themes {:root[data-theme="#{$name}"] {@each $property, $value in $properties {#{$property}: #{$value};}}
}

3. 设置默认主题

@mixin theme($name) {:root[data-theme="#{$name}"] {@each $property, $value in map-get($themes, $name) {#{$property}: #{$value};}}
}// // 例如,应用默认主题白色
@include theme(light);

4. HTML代码

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>sass实现主题切换</title><link rel="stylesheet" href="index.css">
</head><body><button onclick="changeThemes('light')">切换白色</button><button onclick="changeThemes('dark')"> 切换黑色 </button><button onclick="changeThemes('ocean')">切换蓝色</button><button onclick="changeThemes('forest')">切换绿色</button><div>我是主题切换字体</div><script>function changeThemes (theme) {document.documentElement.setAttribute('data-theme', theme);}//页面初始化的时候加载,当然也可以使用其他方法changeThemes('light')</script>
</body></html>

ending

Sass中文网地址Sass中文网
Less中文网地址Less中文网

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • python3爬虫(未完结)
  • 产业园区智慧公寓管理系统
  • Springboot项目配置shiro报错No SecurityManager accessible to the calling code
  • java之类和对象的介绍
  • etcd启动和测试
  • PyTorch 基础学习(13)- 混合精度训练
  • C++ 设计模式——建造者模式
  • Redis—持久化机制
  • StarRocks 存算分离数据回收原理
  • jpg怎么转换成pdf?6个简单方法,实现jpg转换成pdf
  • 设计模式(一):单例模式
  • 数字IC/FPGA中有符号数的处理探究
  • Python|OpenCV-基于OpenCV进行图像的复制与克隆(19)
  • 第五章 设置和其他常见活动 - 创建 IRIS 凭证集
  • 【hot100篇-python刷题记录】【买卖股票的最佳时机】
  • 9月CHINA-PUB-OPENDAY技术沙龙——IPHONE
  • Angular6错误 Service: No provider for Renderer2
  • CentOS 7 防火墙操作
  • codis proxy处理流程
  • express如何解决request entity too large问题
  • happypack两次报错的问题
  • Java 多线程编程之:notify 和 wait 用法
  • Java,console输出实时的转向GUI textbox
  • Linux gpio口使用方法
  • MobX
  • Python利用正则抓取网页内容保存到本地
  • Quartz初级教程
  • Vue ES6 Jade Scss Webpack Gulp
  • 复杂数据处理
  • 干货 | 以太坊Mist负责人教你建立无服务器应用
  • 将回调地狱按在地上摩擦的Promise
  • 排序算法学习笔记
  • 容器服务kubernetes弹性伸缩高级用法
  • 如何实现 font-size 的响应式
  • 入手阿里云新服务器的部署NODE
  • 软件开发学习的5大技巧,你知道吗?
  • 使用SAX解析XML
  • -- 数据结构 顺序表 --Java
  • 探索 JS 中的模块化
  • 文本多行溢出显示...之最后一行不到行尾的解决
  • 小程序、APP Store 需要的 SSL 证书是个什么东西?
  • 一个项目push到多个远程Git仓库
  • 原生Ajax
  • 云大使推广中的常见热门问题
  • 在 Chrome DevTools 中调试 JavaScript 入门
  • Mac 上flink的安装与启动
  • 关于Kubernetes Dashboard漏洞CVE-2018-18264的修复公告
  • # 安徽锐锋科技IDMS系统简介
  • ###STL(标准模板库)
  • #565. 查找之大编号
  • ${ }的特别功能
  • (27)4.8 习题课
  • (6)添加vue-cookie
  • (C语言)编写程序将一个4×4的数组进行顺时针旋转90度后输出。
  • (Java实习生)每日10道面试题打卡——JavaWeb篇