首页 > 其他分享 >三种方式实现主题切换方案

三种方式实现主题切换方案

时间:2022-11-16 22:37:33浏览次数:49  
标签:name color 主题 site -- 三种 切换 config id

前端主题切换

有些时候我们在网站上会进行夜间/白天模式的切换。
这里我们介绍一种流行的切换模式 css变量 + 动态类名来进行切换
非常的简单。废话不多说。我们直接上代码。
第1种是通过 类名和变量来实现的
第2种是通过 属性和变量还实现的
最后1种 给body加 filter: invert(1);

设置两种模式的颜色

//白天模式
body {
  --site-config-color-bar: #fff; //
  --site-config-color-text: #555; //
  --site-config-color-border: rgba(0, 0, 0, 0.12); //
  --site-config-color-shadow: #eee; //
  --site-config-color-introduce-border: #2196f3; //
  --site-config-color-primary: #2196f3; //
  --site-config-color-side-bar: #3a7afe; //
  --site-config-color-side-bar-active-background: #3a7afe1a; //
}

//夜间模式
body.dark {
    --site-config-color-bar: #1e1e1e; //
    --site-config-color-text: #fff; //
    --site-config-color-border: #333; //
    --site-config-color-shadow: #121212; //
    --site-config-color-introduce-border: #555;  //
    --site-config-color-primary: #96cbfe; //
    --site-config-color-side-bar: #4a7afe; //
    --site-config-color-side-bar-active-background: #4a7afe1a; //
}

ps:将这两种模式的代码放置在全局中。比如app.vue文件中。
反正要在项目的全局中可以使用就行了。

通过给body设置dark类名和移除类名来显示夜间和白天

<template>
  <div>
    <div class="top-header">
       我是顶部的标题
      <button class="change-btn" @click="changeBgHandler">切换皮肤</button>
    </div>
    <div class="cont-box">
      <div class="left-asider-box">
        <div class="item-cont-css" :class="{activecss:item.id==1}" 
          v-for="(item,index) in leftData" :key="index">
            {{item.name}}
        </div>
      </div>

      <div class="center-cont">
        <h3 class="h3-css">我是标题</h3>
        <div class="top-center-header">
            <p class="p1-cont" v-for="item in 10" :key="item">
              {{ item}}
              组件库使用中文作为默认语言,支持多语言切换,内置支持 <a> 中文</a>,<a>英文</a>。
            </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() { 
    return {
      msg: 'xx',
      leftData: [
        { name: '优秀的功能1', id: 1 },
        { name: '优秀的功能2', id: 2 },
        { name: '优秀的功能3', id: 3 },
        {name:'优秀的功能4', id:4},
        {name:'优秀的功能5', id:5},
        { name: '优秀的功能6', id: 6 },
        { name: '优秀的功能7', id: 7 },
        { name: '优秀的功能8', id: 8 },
        {name:'优秀的功能9', id:9},
        {name:'优秀的功能10', id:10},
        {name:'优秀的功能11', id:11},
        {name:'优秀的功能12', id:12},
        { name: '优秀的功能13', id: 13 },
        {name:'优秀的功能14', id:14},
        { name: '优秀的功能15', id: 15 },
        { name: '优秀的功能16', id: 16 },
        { name: '优秀的功能17', id: 17 },
        {name:'优秀的功能18', id:18},
        { name: '优秀的功能19', id: 19 },
        { name: '优秀的功能20', id: 20 },
        { name: '优秀的功能21', id: 21 },
        { name: '优秀的功能22', id: 22 },
        { name: '优秀的功能23', id: 23 },
        { name: '优秀的功能24', id: 24 },
      ],
    }
  },
  methods: {
    changeBgHandler() {
      if (!document.body.className) {
        document.body.className = "dark"
      } else {
        document.body.className = ""
      }
    }
  }
  
}
</script>

<style lang="scss" scoped>
.top-header{
  height: 46px;
  line-height: 46px;
  text-align: center;
  background: var(--site-config-color-bar);
  border-bottom: 1px solid var(--site-config-color-border);
  color: var(--site-config-color-text);
  position: relative;
  .change-btn {
    position: absolute;
    right: 20px;
    top: 14px;
  }
}

.cont-box {
  display: flex;
  background: var(--site-config-color-bar);
  .left-asider-box {
    width: 240px;
    box-shadow: 0 8px 12px var(--site-config-color-shadow);
    background: var(--site-config-color-bar);
    .item-cont-css {
      cursor: pointer;
      padding-left: 20px;
      height: 40px;
      line-height: 40px;
      color: var(--site-config-color-text);
    }

    .activecss {
      color: var(--site-config-color-side-bar);
      background: var(--site-config-color-side-bar-active-background);
      transition: color .2s;
    }
  }
}

.center-cont{
  padding-left: 20px;
  padding-right: 20px;
  flex:1;
  .h3-css {
    color: var(--site-config-color-text);
    margin-top: 10px;
    margin-bottom: 10px;
  }
  .top-center-header {
    border-radius: 4px;
    background: var(--site-config-color-bar);
    padding: 20px;
    margin-bottom: 30px;
    box-shadow: 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 2px 2px rgba(0, 0, 0, 0.14), 0 1px 5px rgba(0, 0, 0, 0.12);
  }
  .p1-cont {
    height: 20px;
    color: var(--site-config-color-text);
  }
  .p1-cont a {
    color: var(--site-config-color-primary);
  }
}
</style>

这些网站有夜间模式

https://varlet.gitee.io/varlet-ui/#/zh-CN/home

第2种-变量+属性

全局的颜色样式-app.vue中
<style lang="scss">
*{
  padding: 0;
  margin: 0;
}
/* 默认白天主题 */
:root {
  --bg-color: #fff;
  --text-color: #555;
  // 白天的颜色放置在这里
}
/* 夜间模式主题 */
:root[theme='light'] {
  --bg-color: #1e1e1e;
  --text-color: #fff;
  // 页面模式的放置在这里
}
// 设置整个页面的背景色
html,body{
  background-color: var(--bg-color);
}
</style>

在页面中的使用

<template>
  <div>
      <div class="center-cont">
        <h3 class="h3-css">我是标题</h3>
        <div class="top-center-header">
          <p class="p1-cont" v-for="item in 10" :key="item">{{ item}}组件库使用中文作为默认语言,支持多语言切换,内置支持 <a> 中文</a>,<a>英文</a>。</p>
        </div>
      </div>
      <button class="change-btn" @click="changeBgHandler">切换皮肤</button>
  </div>
</template>

<script>
export default {
  methods: {
    changeBgHandler() {
      if (document.documentElement.getAttribute('theme')) {
        document.documentElement.removeAttribute('theme')
      } else {
        document.documentElement.setAttribute('theme', 'light')
      }
    }
  }
  
}
</script>
<style lang="scss" scoped>
.center-cont{
  background: var(--bg-color);
  .h3-css{
    color: var(--text-color);
  }
  .p1-cont{
    color: var(--text-color);
  }
}
</style>

还有没有更加简单的切换模式 --有的

给body加 filter: invert(1);
但是这一种只能够临时解决问题。

标签:name,color,主题,site,--,三种,切换,config,id
From: https://www.cnblogs.com/IwishIcould/p/16897789.html

相关文章

  • spingboot10(切换内置web服务器)
    1-我们在使用SpringBoot_Web的使用默认会使用"Tomcat"服务器2-查看SpringBoot服务器的内置种类解析:知道了SpringBoot的4种服务器种类,现在就可以选择切换......
  • JS实现轮播图的三种简单方法
    01、JS轮播图实现1实现思路这可能是轮播图最简单点的实现之一,通过更改图片的src来实现该效果,首先需要将图片命名格式统一比如pic01.jpg,pic02.jpg…,再通过js使用定时器去......
  • linux root切换新用户出现-bash-4.2$
     原因:新用户目录下缺少环境变量文件.bash_profile和.bashrc解决办法:将.bash_profile和.bashrc文件复制到登录的用户目录下登录root用户执行命令,执行完再次切换,切换成......
  • 使用windows切换程序窗口时关闭edg的多个选项卡
    关闭edg浏览器切换程序窗口时展示多个选项卡一、切换程序窗口的方式当使用电脑打开了多个程序时,可以通过按住快捷键alt+tab(或者使用笔记本的触控屏,三指左右滑动快捷手势)......
  • AWS EMR实例组竞价实例扩展失败切换按需实例方法
    背景本文介绍如何解决EMR集群实例组模式下竞价实例请求失败后无法切换到按需实例问题,本文通过CloudWatch+Lambda实现在EMR集群竞价实例请求失败情况下补充制定数量的按需实......
  • js定义函数的三种方法及区别
    1函数定义:2functiontest(){};3函数表达式|匿名函数4vartest=function(){};5构造函数6vartest=newFunction('a','b','函数体');functio......
  • Day6-1 三种初始化及下标越界
    三种初始化及内存分析Java内存:堆:存放new的对象和数组可以被所有的线程共享,不会存放别的对象引用栈:存放基本变量类型(会包含这个基本类型的具体数值)引用......
  • 查看Sql Server的执行计划三种方式
    1、图形化,比较直观  2、文本形式查看,是和大型查询语句  3、使用xml方式查看  2.MicrosoftSQLServer查询计划的查看方式_哔哩哔哩_bilibili ......
  • 什么是分布式锁?实现分布式锁的三种方式
    转载:https://www.cnblogs.com/liuqingzheng/p/11080501.html在很多场景中,我们为了保证数据的最终一致性,需要很多的技术方案来支持,比如分布式事务、分布式锁等。那具体什......
  • Manico Mac苹果电脑快速App启动和切换工具软件
    mac软件下载:ManicoforMac为macOS设计的快速的App启动和切换工具。使用它,在macOS上启动及切换App将变得无比顺畅。在长期的使用过程中,用户还将形成肌肉记忆,从而......