资讯专栏INFORMATION COLUMN

切换页面主题样式研究及less教程

DangoSky / 3588人阅读

摘要:具体操作如下这样我们就可以自由的切换主题啦。这个时候只需要引入一份就行了,要切换样式的时候修改的类标签。,有没有更好的切换主题的方案呢,求大佬指点参考

</>复制代码

  1. 某一天,一个头条的大佬问我,听说你之前在项目里面弄过主题切换,你当时是怎么实现的可以详说一下吗?

如果已经对less了如指掌,直接从这里开始。

从less说起 使用

Node.js 环境中使用 Less :

</>复制代码

  1. npm install -g less
  2. > lessc styles.less styles.css

在浏览器环境中使用 Less :

</>复制代码

不过比较推荐的还是通过webpack或者gulp等工具先将less编译成css,然后再引入使用。

变量

</>复制代码

  1. @nice-blue: #5B83AD;
  2. @light-blue: @nice-blue + #111;
  3. #header {
  4. color: @light-blue;
  5. }
混合

常用

</>复制代码

  1. .bordered {
  2. border-top: dotted 1px black;
  3. &:hover {
  4. border: 1px solid red;
  5. } // 同样可以包含选择器混用
  6. }
  7. .post a {
  8. color: red;
  9. .bordered !important; // 可以在属性后面都加上!important
  10. }

编译后不输出至css文件

</>复制代码

  1. .my-other-mixin() {
  2. background: white;
  3. }

作用域混合

</>复制代码

  1. #outer {
  2. .inner() {
  3. color: red;
  4. }
  5. }
  6. #outer1 {
  7. .inner() {
  8. color: blue;
  9. }
  10. }
  11. .c {
  12. #outer > .inner;
  13. }

输出

</>复制代码

  1. .c {
  2. color: red;
  3. }

拥有前置条件的作用域混合,更多详情

</>复制代码

  1. @mode: little;
  2. #outer when (@mode=huge) {
  3. .inner {
  4. color: red;
  5. }
  6. }
  7. #outer when (@mode=little) {
  8. .inner {
  9. color: blue;
  10. }
  11. }
  12. .c {
  13. #outer > .inner;
  14. }

输出(可以看到没有通过前置条件的样式被过滤了)

</>复制代码

  1. #outer .inner {
  2. color: blue;
  3. }
  4. .c {
  5. color: blue;
  6. }

带参数混合,可以指定默认值,可以带多个参数,使用时可以通过名称赋值

</>复制代码

  1. .mixin(@color: black; @margin: 10px; @padding: 20px) {
  2. color: @color;
  3. margin: @margin;
  4. padding: @padding;
  5. }
  6. .class1 {
  7. .mixin(@margin: 20px; @color: #33acfe);
  8. }
  9. .class2 {
  10. .mixin(#efca44; @padding: 40px);
  11. }

使用@arguments

</>复制代码

  1. .mixin(@a; @b) {
  2. margin: @arguments;
  3. right:extract(@arguments,2);
  4. top:@b;
  5. }
  6. p {.mixin(1px; 2px);}

输出

</>复制代码

  1. p {
  2. margin: 1px 2px;
  3. right: 2px;
  4. top: 2px;
  5. }

使用@rest参数

</>复制代码

  1. // 方式1
  2. .mixin(@listofvariables...) {
  3. border: @listofvariables;
  4. }
  5. p {
  6. .mixin(1px; solid; red);
  7. }
  8. // 方式2
  9. .mixin(@a; ...) { color: @a;}
  10. .mixin(@a) { background-color: contrast(@a); width:100%;}
  11. .mixin(@a; @b;) { background-color: contrast(@a); width:@b;}
  12. p {
  13. .mixin(red);
  14. }
  15. p.small {
  16. .mixin(red,50%);
  17. }

输出

</>复制代码

  1. /*方式1*/
  2. p {
  3. border: 1px solid red;
  4. }
  5. /*方式2*/
  6. p {
  7. color: red;
  8. background-color: #ffffff;
  9. width: 100%;
  10. }
  11. p.small {
  12. color: red;
  13. background-color: #ffffff;
  14. width: 50%;
  15. }

模式匹配混合

</>复制代码

  1. .mixin(dark; @color) {
  2. color: darken(@color, 10%);
  3. }
  4. .mixin(light; @color) {
  5. color: lighten(@color, 10%);
  6. }
  7. .mixin(@_; @color) {
  8. display: block;
  9. }
  10. @switch: light;
  11. .class {
  12. .mixin(@switch; #888);
  13. }

输出

</>复制代码

  1. .class {
  2. color: #a2a2a2;
  3. display: block;
  4. }

规则集混合

</>复制代码

  1. // declare detached ruleset
  2. @detached-ruleset: { background: red; };
  3. // use detached ruleset
  4. .top {
  5. @detached-ruleset();
  6. }

函数

类似于先函数运算计算出变量的值,再通过变量的值设置属性。

</>复制代码

  1. .mixin() {
  2. @width: 100%;
  3. @height: 200px;
  4. }
  5. .caller {
  6. .mixin();
  7. width: @width;
  8. height: @height;
  9. }

输出

</>复制代码

  1. .caller {
  2. width: 100%;
  3. height: 200px;
  4. }
继承

继承另外一个选择器的属性,更多详情

</>复制代码

  1. nav ul {
  2. &:extend(.inline);
  3. background: blue;
  4. } // &是父类型选择器,指代的是nav ul
  5. .inline {
  6. color: red;
  7. }

输出

</>复制代码

  1. nav ul {
  2. background: blue;
  3. }
  4. .inline,
  5. nav ul {
  6. color: red;
  7. }
嵌套

</>复制代码

  1. #header {
  2. color: black;
  3. .navigation {
  4. font-size: 12px;
  5. }
  6. .logo {
  7. width: 300px;
  8. }
  9. }
引入

</>复制代码

  1. @import "foo"; // foo.less is imported
  2. @import "foo.less"; // foo.less is imported
  3. @import "foo.php"; // foo.php imported as a less file
  4. @import "foo.css"; // statement left in place, as-is
参数

@import (keyword) "filename";

reference: use a Less file but do not output it

inline: include the source file in the output but do not process it

less: treat the file as a Less file, no matter what the file extension

css: treat the file as a CSS file, no matter what the file extension

once: only include the file once (this is default behavior)

multiple: include the file multiple times

optional: continue compiling when file is not found

循环

</>复制代码

  1. .generate-columns(4);
  2. .generate-columns(@n, @i: 1) when (@i =< @n) {
  3. .column-@{i} {
  4. width: (@i * 100% / @n);
  5. }
  6. .generate-columns(@n, (@i + 1));
  7. }

输出

</>复制代码

  1. .column-1 {
  2. width: 25%;
  3. }
  4. .column-2 {
  5. width: 50%;
  6. }
  7. .column-3 {
  8. width: 75%;
  9. }
  10. .column-4 {
  11. width: 100%;
  12. }
合并

逗号合并

</>复制代码

  1. .mixin() {
  2. box-shadow+: inset 0 0 10px #555;
  3. }
  4. .myclass {
  5. .mixin();
  6. box-shadow+: 0 0 20px black;
  7. }

输出

</>复制代码

  1. .myclass {
  2. box-shadow: inset 0 0 10px #555, 0 0 20px black;
  3. }

空格合并

</>复制代码

  1. .mixin() {
  2. transform+_: scale(2);
  3. }
  4. .myclass {
  5. .mixin();
  6. transform+_: rotate(15deg);
  7. }

输出

</>复制代码

  1. .myclass {
  2. transform: scale(2) rotate(15deg);
  3. }

切换主题样式 方案1

当几种主题布局类似,几乎只是颜色不同时,可以使用这种。

通过对less的了解(可以进行许多额外的骚操作哦),我们这里最简单地编写出如下less文件。

基础样式模版style.less,里面主要通过变量设置各个样式

爱国红样式patriotic-red.less,设置变量的值

天空蓝样式sky-blue.less,设置不同的变量值

...

style.less里样式类似这样

</>复制代码

  1. a,
  2. .link {
  3. color: @link-color;
  4. }
  5. a:hover {
  6. color: @link-color-hover;
  7. }
  8. .widget {
  9. color: #fff;
  10. background: @link-color;
  11. }

patriotic-red.less里样式类似这样

</>复制代码

  1. @import "style";
  2. @link-color: #f03818;
  3. @link-color-hover: darken(@link-color, 10%);

sky-blue.less里样式类似这样

</>复制代码

  1. @import "test";
  2. @link-color: #428bca;
  3. @link-color-hover: darken(@link-color, 10%);

利用相关工具(或原始人手动lessc)提前编译成patriotic-red.csssky-blue.css文件。

这里简单的假设页面header中只引入了默认爱国红——

同时存在一个按钮,用户点击时切换主题。首先我们给按钮绑定点击事件,当用户点击时删除当前导入的样式,然后再引入另外一个样式。具体操作如下:

</>复制代码

  1. function toggleThemeClick() {
  2. let a = document.getElementsByTagName("link");
  3. let aHref = a[0].getAttribute("href").slice(2, -4);
  4. a[0].parentElement.removeChild(a[0]);
  5. let b = document.createElement("link");
  6. b.setAttribute("rel", "stylesheet");
  7. if ("patriotic-red" === aHref) {
  8. b.setAttribute("href", "./sky-blue.css");
  9. } else {
  10. b.setAttribute("href", "./patriotic-red.css");
  11. }
  12. document.getElementsByTagName("head")[0].appendChild(b);
  13. }

这样我们就可以自由的切换主题啦。

方案2

我们还可以通过给body添加类标签来进行主题的切换。

新建一个style-mixin.less,里面内容如下——

</>复制代码

  1. .patriotic-red {
  2. @import "patriotic-red";
  3. }
  4. .sky-blue {
  5. @import "sky-blue";
  6. }

这里需要注意的是,在sky-blue.less或者patriotic-red.less中引入style.less时,需要使用(multiple)参数,否则会只编译出一份样式。编译出来的样式会自动加上.patriotic-redsky-blue前缀。

这个时候只需要引入一份style-mixin.css就行了,要切换样式的时候修改body的类标签。

document,getElementsByTagName("body")[0].className="xxx"

题外话

头条大佬似乎并不是很认可方案1,他认为方案1会导致之前引入的样式和后来的冲突?我没有明白具体是什么意思,由于时间关系他也没有解释得很清楚,希望知道问题出在哪里的同学告诉我一下哦。

ps,有没有更好的切换主题的方案呢,求大佬指点~

参考

lesscss.cn

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/114455.html

相关文章

  • 切换页面主题样式研究less教程

    摘要:具体操作如下这样我们就可以自由的切换主题啦。这个时候只需要引入一份就行了,要切换样式的时候修改的类标签。,有没有更好的切换主题的方案呢,求大佬指点参考 某一天,一个头条的大佬问我,听说你之前在项目里面弄过主题切换,你当时是怎么实现的可以详说一下吗? 如果已经对less了如指掌,直接从这里开始。 从less说起 使用 Node.js 环境中使用 Less : npm install -g...

    taoszu 评论0 收藏0
  • 切换页面主题样式研究less教程

    摘要:具体操作如下这样我们就可以自由的切换主题啦。这个时候只需要引入一份就行了,要切换样式的时候修改的类标签。,有没有更好的切换主题的方案呢,求大佬指点参考 某一天,一个头条的大佬问我,听说你之前在项目里面弄过主题切换,你当时是怎么实现的可以详说一下吗? 如果已经对less了如指掌,直接从这里开始。 从less说起 使用 Node.js 环境中使用 Less : npm install -g...

    hosition 评论0 收藏0
  • 应用vue2+vuex+vue-router+webpack2+es6+express+mysql技

    摘要:其实这里还是有漏洞的,坐等高手指出来微笑脸后台没有用生成一个完整的架构。来自不同视图的行为需要变更同一状态。 最近对vue很感兴趣,趁闲暇时间,模仿了wunderlist里面的部分功能,编写了前后端分离的基于vue技术栈和express的todolist小项目。写这篇博文来总结思考下。项目所在github,可以自行参考克隆。 本人博客 总体概览 整个项目最终做成的样子如下: showI...

    voidking 评论0 收藏0
  • vue开发项目完全指南

    摘要:有两种方法,一种是在开发环境中设置通过的,另一种是在服务器上修改的配置设置。这样我们以后使用访问接口就可以不加了,打包后访问也不用手动去除统一管理在项目开发过程中,会涉及到很多接口的处理,当项目足够大时,就需要统一管理接口。 这篇文章总结了vue项目的所遇到的问题,包括跨域、用户认证、接口统一管理、路由配置、兼容性处理,性能优化等内容。 项目github地址 : 前端 https:...

    leoperfect 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<