当前位置:   article > 正文

vue2、vue3实现暗黑模式_vue2 element 暗黑主题

vue2 element 暗黑主题

1、序言

        elementPlus、naive UI这些UI组件里面封装好了暗黑模式,直接使用相关api即可实现暗黑模式切换功能,而elementUI没有封装好,我们可以看看elementPlus、naive UI如何实现暗黑模式,然后在elementUI中模仿,从而实现暗黑模式!

2、vue3实现暗黑模式

        2.1、使用

(1)npm i @vueuse/core,  这个里面封装了很多hook工具函数:   VueUse | VueUse

(2)import { useDark, useToggle } from '@vueuse/core'

(3)在<script> 下使用:

  1. <script setup>
  2. import { useDark, useToggle} from '@vueuse/core'
  3. const isDark = useDark();
  4. </script>
  5. <style scoped>

(4)useDark()可以获取当前的主题模式是不是暗黑模式,返回的是布尔值,useToggle()类似于开关,能将useDark()取反,实现主题模式切换效果

(5)main.js中引入暗黑模式的样式包:

 

        import 'element-plus/dist/index.css' :引入elementplus推荐的light模式的样式文件

        import 'element-plus/theme-chalk/dark/css-vars.css':是elementplus实现的dark模式的样式文件

 

        2.2、原理

        结论:暗黑模式时 import 'element-plus/theme-chalk/dark/css-vars.css'  覆盖 light模式中

import 'element-plus/dist/index.css' 的样式文件

        (1)会给html添加个class = "dark" 选择器

暗黑模式:

light模式

        (2)dark模式会在 'element-plus/theme-chalk/dark/css-vars.css'   进行样式覆盖

'element-plus/theme-chalk/dark/css-vars.css' 

  1. html.dark {
  2. color-scheme: dark;
  3. --el-color-primary: #409eff;
  4. --el-color-primary-light-3: #3375b9;
  5. --el-color-primary-light-5: #2a598a;
  6. --el-color-primary-light-7: #213d5b;
  7. --el-color-primary-light-8: #1d3043;
  8. --el-color-primary-light-9: #18222c;
  9. --el-color-primary-dark-2: #66b1ff;
  10. --el-color-success: #67c23a;
  11. --el-color-success-light-3: #4e8e2f;
  12. --el-color-success-light-5: #3e6b27;
  13. --el-color-success-light-7: #2d481f;
  14. --el-color-success-light-8: #25371c;
  15. --el-color-success-light-9: #1c2518;
  16. --el-color-success-dark-2: #85ce61;
  17. --el-color-warning: #e6a23c;
  18. --el-color-warning-light-3: #a77730;
  19. --el-color-warning-light-5: #7d5b28;
  20. --el-color-warning-light-7: #533f20;
  21. --el-color-warning-light-8: #3e301c;
  22. --el-color-warning-light-9: #292218;
  23. --el-color-warning-dark-2: #ebb563;
  24. --el-color-danger: #f56c6c;
  25. --el-color-danger-light-3: #b25252;
  26. --el-color-danger-light-5: #854040;
  27. --el-color-danger-light-7: #582e2e;
  28. --el-color-danger-light-8: #412626;
  29. --el-color-danger-light-9: #2b1d1d;
  30. --el-color-danger-dark-2: #f78989;
  31. --el-color-error: #f56c6c;
  32. --el-color-error-light-3: #b25252;
  33. --el-color-error-light-5: #854040;
  34. --el-color-error-light-7: #582e2e;
  35. --el-color-error-light-8: #412626;
  36. --el-color-error-light-9: #2b1d1d;
  37. --el-color-error-dark-2: #f78989;
  38. --el-color-info: #909399;
  39. --el-color-info-light-3: #6b6d71;
  40. --el-color-info-light-5: #525457;
  41. --el-color-info-light-7: #393a3c;
  42. --el-color-info-light-8: #2d2d2f;
  43. --el-color-info-light-9: #202121;
  44. --el-color-info-dark-2: #a6a9ad;
  45. --el-box-shadow: 0px 12px 32px 4px rgba(0, 0, 0, 0.36), 0px 8px 20px rgba(0, 0, 0, 0.72);
  46. --el-box-shadow-light: 0px 0px 12px rgba(0, 0, 0, 0.72);
  47. --el-box-shadow-lighter: 0px 0px 6px rgba(0, 0, 0, 0.72);
  48. --el-box-shadow-dark: 0px 16px 48px 16px rgba(0, 0, 0, 0.72), 0px 12px 32px #000000, 0px 8px 16px -8px #000000;
  49. --el-bg-color-page: #0a0a0a;
  50. --el-bg-color: #141414;
  51. --el-bg-color-overlay: #1d1e1f;
  52. --el-text-color-primary: #E5EAF3;
  53. --el-text-color-regular: #CFD3DC;
  54. --el-text-color-secondary: #A3A6AD;
  55. --el-text-color-placeholder: #8D9095;
  56. --el-text-color-disabled: #6C6E72;
  57. --el-border-color-darker: #636466;
  58. --el-border-color-dark: #58585B;
  59. --el-border-color: #4C4D4F;
  60. --el-border-color-light: #414243;
  61. --el-border-color-lighter: #363637;
  62. --el-border-color-extra-light: #2B2B2C;
  63. --el-fill-color-darker: #424243;
  64. --el-fill-color-dark: #39393A;
  65. --el-fill-color: #303030;
  66. --el-fill-color-light: #262727;
  67. --el-fill-color-lighter: #1D1D1D;
  68. --el-fill-color-extra-light: #191919;
  69. --el-fill-color-blank: transparent;
  70. --el-mask-color: rgba(0, 0, 0, 0.8);
  71. --el-mask-color-extra-light: rgba(0, 0, 0, 0.3)
  72. }
  73. html.dark .el-button {
  74. --el-button-disabled-text-color: rgba(255, 255, 255, 0.5)
  75. }
  76. html.dark .el-card {
  77. --el-card-bg-color: var(--el-bg-color-overlay)
  78. }
  79. html.dark .el-empty {
  80. --el-empty-fill-color-0: var(--el-color-black);
  81. --el-empty-fill-color-1: #4b4b52;
  82. --el-empty-fill-color-2: #36383d;
  83. --el-empty-fill-color-3: #1e1e20;
  84. --el-empty-fill-color-4: #262629;
  85. --el-empty-fill-color-5: #202124;
  86. --el-empty-fill-color-6: #212224;
  87. --el-empty-fill-color-7: #1b1c1f;
  88. --el-empty-fill-color-8: #1c1d1f;
  89. --el-empty-fill-color-9: #18181a
  90. }

        (3)css文件中,--开头的为自定义变量,要使用它时var(--自定义变量),从而实现变量复用,减少冗余的css样式

        (4)举个例子:el-card

element-plus/dist/index.css 文件中

 

  'element-plus/theme-chalk/dark/css-vars.css' 文件中 

3、vue2实现暗黑模式

        3.1、实现

        (1)建立一个暗黑主题样式文件:my-theme.css,并将其引入到main.js

        (2)my-theme.css文件中 自己根据chrome浏览器的样式工具进行调整了,html.dark为父选择器

 示范:在my-theme.css 中根据需求调节出自己想要的样式

  1. html.dark .el-submenu__title {
  2. background-color: #121212;
  3. color: #fff;
  4. }
  5. html.dark .el-submenu__title:hover {
  6. background-color: #18222b;
  7. }
  8. html.dark .el-menu--inline {
  9. background-color: #121212;
  10. color: #fff;
  11. }
  12. html.dark .el-menu-item.is-active {
  13. color: var(--el-color-primary);
  14. }
  15. html.dark .el-menu-item {
  16. background-color: #121212;
  17. color: #fff;
  18. }
  19. html.dark .el-menu-item:hover {
  20. background-color: #18222b;
  21. }
  22. html.dark .el-card {
  23. background-color: #121212;
  24. }

 

        (3)添加一个开关用于给html添加class="dark"选择器,也就是暗黑模式

  1. <template>
  2. <el-switch v-model="isDark" @change="changeMode" active-color="#13ce66" inactive-color="#ff4949">
  3. </el-switch>
  4. </template>
  1. <script>
  2. export default {
  3. data() {
  4. return {
  5. isDark: false,
  6. }
  7. },
  8. methods: {
  9. // 模式切换
  10. changeMode(val) {
  11. // 获取html根元素标签
  12. let html = document.documentElement
  13. if (val) {
  14. // html添加class="dark"选择器
  15. html.classList.add('dark')
  16. } else {
  17. // html移除class="dark"选择器
  18. html.classList.remove('dark')
  19. }
  20. }
  21. }
  22. }
  23. </script>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/81997
推荐阅读
相关标签
  

闽ICP备14008679号