需求:

设计三套主题色+部分图标更换;

实现方式汇总:

1.传统做法,生成多套css主题包,切换link引入路径切换href实现,参考网站:http://jui.org/

<link id="skincolor" href="skin-default.css" rel="stylesheet" type="text/css">

document.getElementById('#skincolor').href = 'skin-red.css';

这种实现对于,颜色和主题多了的时候,维护起来就很麻烦,需要同时维护 n 个样式文件,并且使用JS改变href属性会带来加载延迟,样式切换不流畅,体验也不好。

同时需要考虑切换时延时问题,解决方案:

https://developer.mozilla.org/en-US/docs/Web/CSS/Alternative_style_sheets

示例:

  1. <link href="reset.css" rel="stylesheet" type="text/css">
  2. <link href="default.css" rel="stylesheet" type="text/css" title="Default Style">
  3. <link href="fancy.css" rel="alternate stylesheet" type="text/css" title="Fancy">
  4. <link href="basic.css" rel="alternate stylesheet" type="text/css" title="Basic">

所有样式表都可分为3类:

  • 没有title属性,rel属性值仅仅是stylesheet的<link>无论如何都会加载并渲染,如reset.css;
  • 有title属性,rel属性值仅仅是stylesheet的<link>作为默认样式CSS文件加载并渲染,如default.css;
  • 有title属性,rel属性值同时包含alternate stylesheet的<link>作为备选样式CSS文件加载,默认不渲染,如red.css和green.css;

alternate意味备用,相当于是 css 预加载进来备用,所以不会有上面那种切换延时

或者是将多套样式一起打包,不过每套样式需要添加不同的前缀名称用于区分,用内存换功能,这样直接在body上利用js切换class名称即可:

  toggleClass(element, className) {
if (!element || !className) {
return;
}
element.className = className;
}
// 点击按钮切换
this.toggleClass(document.body, 'theme-1');

2.饿了么官方demo:直接在页面上插入 style 标签样式,利用样式优先级强行覆盖(不推荐),具体步骤:

先把默认主题文件中涉及到颜色的 CSS 值替换成关键词

根据用户选择的主题色生成一系列对应的颜色值

把关键词再换回刚刚生成的相应的颜色值

直接在页面上加 style 标签,把生成的样式填进去

3.利用html引用css生效原生属性进行切换:如果是elementUI推荐使用sass,antd推荐使用less(注意编译速度);

window.document.documentElement.setAttribute('data-theme', ‘theme1’)

elementUI实战:

1.准备工作:

安装:vue+elementUI+sass

配置:以上版本问题、自行在官网查阅,关于sass推荐一个网站https://www.sassmeister.com/

2.demo:

页面:

<template>
<div>
<el-button @click="changeTheme('theme1')">
theme1
</el-button>
<el-button @click="changeTheme('theme2')">
theme2
</el-button>
<el-button @click="changeTheme('theme3')">
theme3
</el-button>
</div>
</template>
<script>
export default {
methods: {
changeTheme (theme) {
window.document.documentElement.setAttribute('data-theme', theme)
}
}
}
</script>
<style scoped="" lang="scss"> </style>

sass配置:

1.主题文件theme.scss

//主题色
$font-color-theme1 : #3f8e4d;//字体默认颜色
$font-color-theme2 : red;
//
$background-color-theme1: #3f8e4d;//默认主题颜色
$background-color-theme2: red; $font-color-shallow0 : #000;
$font-color-shallow1 : #111;
$font-color-shallow2 : #222;
$font-color-shallow3 : #333;
$font-color-shallow4 : #444;
$font-color-shallow5 : #555;
$font-color-shallow6 : #666;
$font-color-shallow7 : #777;
$font-color-shallow8 : #888;
$font-color-shallow9 : #999; //字体定义规范
$font_little_s:10px;
$font_little:12px;
$font_medium_s:14px;
$font_medium:16px;
$font_large_s:18px;
$font_large:20px;

2.公共变量文件

@import "./theme";/*引入配置*/
@mixin font_size($size){/*通过该函数设置字体大小,后期方便统一管理;*/
@include font-dpr($size);
}
@mixin font_color($color){/*通过该函数设置字体颜色,后期方便统一管理;*/
color:$color;
[data-theme="theme1"] & {
color:$font-color-theme1;
}
[data-theme="theme2"] & {
color:$font-color-theme2;
}
}
@mixin bg_color($color){/*通过该函数设置主题颜色,后期方便统一管理;*/
background-color:$color;
[data-theme="theme1"] & {
background-color:$background-color-theme1;
}
[data-theme="theme2"] & {
background-color:$background-color-theme2;
}
}

3.修改elment组件样式变量:如alert

@import "./common/var";

@include b(alert) {
width: 100%;
padding: $--alert-padding;
margin: 0;
box-sizing: border-box;
border-radius: $--alert-border-radius;
position: relative;
// background-color: $--color-white;
@include bg_color(background-color);
overflow: hidden;
opacity: 1;
display: flex;
align-items: center;
transition: opacity .2s; @include when(light) { // 默认
.el-alert__closebtn {
// color: $--color-text-placeholder;
@include font_color(color);
}
}

参考推荐:

https://github.com/ElemeFE/element/issues/3054

elementUI——主题定制的更多相关文章

  1. 车载导航应用中基于Sketch UI主题定制方案的实现

    1.导读 关于应用的主题定制,相信大家或多或少都有接触,基本上,实现思路可以分为两类: 内置主题(应用内自定义style) 外部加载方式(资源apk形式.压缩资源.插件等) 其实,针对不同的主题定制实 ...

  2. 提升组件库通用能力 - NutUI 在线主题定制功能探索

    开发背景 NutUI 作为京东风格的组件库,已具备 H5 和多端小程序开发能力.随着业务的不断发展,组件库的应用场景越来越广.在公司内外面临诸如科技.金融.物流等各多个大型团队使用时,单一的京东 AP ...

  3. 自定义element-ui主题

    自定义element主题颜色:主要参考这个文章https://blog.csdn.net/wangcuiling_123/article/details/78513245,再自己做了一遍成功.感谢. ...

  4. 公司4:JrVue主题定制

    JrVue是我们基于element重新封装的一套组件库;  具体组件使用方法可以mnote->研发小组查看; 这里我们定制了一套主题色, 具体变动如下: 1.主题色变动: mfront有蓝.紫. ...

  5. vue使用改变element-ui主题色

    每个项目的主题色一般都不一样,直接用element-ui的默认主题色似乎有点不合适,还需要自己一个一个的找元素class名然后在修改样式,非常麻烦,还容易影响到包含该类名的其他元素样式,所以需要自定义 ...

  6. Visual Studio中的主题定制变得更加容易

    有时Visual Studio的默认主题是不够的.幸运的是,我们刚刚重新设计了创建和导入自定义主题的过程. 导入主题的唯一方法之一是下载旧的Color Theme Editor扩展.如果你足够勇敢地创 ...

  7. 公司4:JrVue主题定制-2

    页面折叠布局:(折叠按钮.transition动画.git项目池模块分支) 布局组件(template): <el-container> <el-aside> <!-- ...

  8. 构建前端第7篇之---elementUI设置主题,进而改变全局button底色

    张艳涛写于2020-1-20 What:是elementUI主题? 是内置的格式,elementUI默认只有一个主题,如果想整体替换按钮的颜色等问题,那么就可以用主题 设置步骤 在路径src/styl ...

  9. 决定如何开发你的WordPress主题框架

    在本系列教程的第一部分,我介绍了不同类型的主题框架并解释了它们是如何工作的. 在你开始建立你的主题框架之前,你需要考虑它是如何工作的,以及它将会被用来做什么,这样你才能从一开始就找到最合适的开发途径. ...

随机推荐

  1. codeDecodeError ascii codec can't decode byte 0xe2 in position 44 ordinal not in range(128)

  2. ThreadLocal:的面试

    ThreadLocal: 为解决多线程程序的并发问题提供了一种新的思路.使用这个工具类可以很简洁地编写出优美的多线程程序. 当使用ThreadLocal维护变量时,ThreadLocal为每个使用该变 ...

  3. GIS地理处理脚本案例教程——批量栅格分割-批量栅格裁剪-批量栅格掩膜-深度学习样本批量提取

    GIS地理处理脚本案例教程--批量栅格分割-批量栅格裁剪-批量栅格掩膜-深度学习样本批量提取 商务合作,科技咨询,版权转让:向日葵,135-4855_4328,xiexiaokui#qq.com 关键 ...

  4. Python实例100个(基于最新Python3.7版本)

    Python3 100例 原题地址:   http://www.runoob.com/python/python-100-examples.html    git地址:    https://gith ...

  5. PostgreSQL中的Object Identifier(oid)数据类型

    PostgreSQL在内部使用对象标识符(OID)作为各种系统表的主键.OID不会添加到用户创建的表中,除非在创建表时指定了WITH OIDS,或者启用了default_with_oids配置变量.类 ...

  6. 使用yarn代替npm作为node.js的模块管理器

    使用yarn代替npm作为node.js的模块管理器 转 https://www.jianshu.com/p/bfe96f89da0e     Fast, reliable, and secure d ...

  7. IfcWallStandardCase 构件吊装模拟

    wall_node = (osg::Node*)(index_node->clone(osg::CopyOp::DEEP_COPY_ALL));vc_mobileCrane->tranMo ...

  8. [译]如何在红帽系统(RHEL)上源码安装python3?

    原文来源: https://stackoverflow.com/questions/8087184/installing-python-3-on-rhel 很容易手动安装. 1.下载对应的python ...

  9. EasyNVR摄像机网页Chrome无插件视频播放功能二次开发之通道配置文件上传下载示例代码

    背景需求 熟悉EasyNVR产品的朋友们都知道,产品设计初期根据整个直播流程层级,我们将EasyNVR无插件直播系统划分为:硬件层.能力层.应用层,连接硬件与应用之间的桥梁,同时屏蔽各种厂家硬件的不同 ...

  10. [LeetCode] 52. N-Queens II N皇后问题 II

    The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...