工作日志4(用户换主题,一键换肤)

系统管理员 2023-05-29 13:15 86阅读 0赞

业务需求

  1. vue-i18n国际化时,发现表单,table等的labelplaceholder等无法使用$t('')
  2. 增加一键换肤功能

解决方法

1.只需要用v-bind动态绑定label把label :改成:label="$t('user.oldPasswd')"就可以了

一键换肤

首先,先看一下目录和主要文件
在这里插入图片描述
index.scss: 一些通用样式文件,入口文件,引入mixin.scss和varibale.scss等。

mixin.scss: 定义mixin方法的文件。

varibale.scss: 颜色,字体,背景的配置文件

Navbar.vue

  1. <style rel="stylesheet/scss" lang="scss" scoped>
  2. @import "@/styles/index.scss"; //注意要引用index.scss入口文件
  3. .navbar {
  4. height: 50px;
  5. line-height: 50px;
  6. border-radius: 0px !important;
  7. @include bg_color($background-color-theme);
  8. @include font_color($font-color-theme);
  9. .hamburger-container {
  10. line-height: 58px;
  11. height: 50px;
  12. float: left;
  13. padding: 0 10px;
  14. }
  15. }
  16. </style>

重点是这两句话

  1. @include bg_color($background-color-theme);
  2. @include font_color($font-color-theme);

需要把设置背景颜色封装成一个mixin方法(包括字体大小,字体颜色,都需要进行封装)所以mixin.scss需要定义这两个方法

mixin.scss

  1. @mixin font_color($color){ /*通过该函数设置字体颜色,后期方便统一管理;*/
  2. color:$color;
  3. [data-theme="theme1"] & {
  4. color:$font-color-theme1;
  5. }
  6. [data-theme="theme2"] & {
  7. color:$font-color-theme2;
  8. }
  9. [data-theme="theme3"] & {
  10. color:$font-color-theme3;
  11. }
  12. }
  13. @mixin bg_color($color){ /*通过该函数设置主题颜色,后期方便统一管理;*/
  14. background-color:$color;
  15. [data-theme="theme1"] & {
  16. background-color:$background-color-theme1;
  17. }
  18. [data-theme="theme2"] & {
  19. background-color:$background-color-theme2;
  20. }
  21. [data-theme="theme3"] & {
  22. background-color:$background-color-theme3;
  23. }
  24. }

changeTheme.vue

  1. <template>
  2. <div class="system-list wrapper">
  3. <span> 主题切换</span>
  4. <div class="theme-edit">
  5. <div class="content">
  6. <p @click="changeTheme('theme1')">默认</p>
  7. <p @click="changeTheme('theme2')">粉白</p>
  8. <p @click="changeTheme('theme3')">天蓝</p>
  9. </div>
  10. </div>
  11. </div>
  12. </template>
  13. <script>
  14. export default {
  15. name: 'System',
  16. data() {
  17. return {
  18. }
  19. },
  20. created() {
  21. let skin = localStorage.getItem("theme")
  22. window.document.documentElement.setAttribute('data-theme', skin)
  23. },
  24. methods: {
  25. changeTheme(theme) {
  26. window.document.documentElement.setAttribute('data-theme', theme)
  27. localStorage.setItem("theme", theme)
  28. this.$message({
  29. message: '切换成功',
  30. type: 'success'
  31. })
  32. }
  33. }
  34. }
  35. </script>
  36. <style lang="scss" scoped>
  37. .system-list {
  38. border: 1px solid #fff;
  39. background-color: #fff;
  40. height: 300px;
  41. span {
  42. float: left;
  43. padding: 15px 0 0 20px;
  44. }
  45. .theme-edit {
  46. width: 500px;
  47. height: 100px;
  48. margin-top: 35px;
  49. margin-bottom: 40px;
  50. .content {
  51. p {
  52. width: 50px;
  53. height: 50px;
  54. margin: 20px;
  55. float: left;
  56. text-align: center;
  57. line-height: 50px;
  58. }
  59. p:first-child {
  60. background-color: #fff;
  61. border: 1px solid #ccc;
  62. }
  63. p:nth-child(2) {
  64. background-color: rgba(252, 208, 232, 0.877);
  65. }
  66. p:last-child {
  67. background-color: rgb(190, 233, 248);
  68. }
  69. }
  70. }
  71. .font-edit {
  72. display: block;
  73. // margin-top: 100px;
  74. margin-bottom: 40px;
  75. float: left;
  76. }
  77. }
  78. </style>

这个思路是看到别人博主得来的:
通过设置html的attribute属性在封装的函数中进行判断,进行相应的设置不同的颜色
css中 [ ] 可以识别到在html标签上设置的属性,所以在html上对应属性发生变化时,就会执行相应的样式,
这一步有点类似于平时给div添加一个.active属性,css自动执行相应样式。

  1. window.document.documentElement.setAttribute('data-theme', theme)

另外,如果要记住上一次换的皮肤,我使用的是localStorage,将每次点击换肤的主题记录下来,然后再页面渲染之前判断是否有这个主题就可以了。

  1. created() {
  2. let skin = localStorage.getItem("theme")
  3. window.document.documentElement.setAttribute('data-theme', skin)
  4. },
  5. methods: {
  6. changeTheme(theme) {
  7. window.document.documentElement.setAttribute('data-theme', theme)
  8. localStorage.setItem("theme", themename)
  9. }
  10. }

varibale.scss

  1. //颜色定义规范
  2. $background-color-theme: #fff; //背景主题颜色默认
  3. $background-color-theme1: #fff; //背景主题颜色1
  4. $background-color-theme2: rgba(252, 208, 232, 0.877); //背景主题颜色2
  5. $background-color-theme3: rgb(190, 233, 248); //背景主题颜色3
  6. $font-color-theme : #000; //字体主题颜色默认
  7. $font-color-theme1 : #000; //字体主题颜色1
  8. $font-color-theme2 : #fff; //字体主题颜色2
  9. $font-color-theme3 : #fff; //字体主题颜色3

index.scss

  1. @import './element-ui.scss';
  2. @import './mixin.scss';
  3. @import './transition.scss';
  4. @import './sidebar.scss';
  5. @import './login.scss';
  6. body {
  7. -moz-osx-font-smoothing: grayscale;
  8. -webkit-font-smoothing: antialiased;
  9. text-rendering: optimizeLegibility;
  10. font-family: Helvetica Neue, Helvetica, PingFang SC, Hiragino Sans GB, Microsoft YaHei, Arial, sans-serif;
  11. }
  12. html, body {
  13. box-sizing: border-box;
  14. width: 100%;
  15. height: 100%;
  16. }
  17. *,
  18. *:before,
  19. *:after {
  20. box-sizing: inherit;
  21. }
  22. a:focus,
  23. a:active {
  24. outline: none;
  25. }
  26. a,
  27. a:focus,
  28. a:hover {
  29. cursor: pointer;
  30. color: inherit;
  31. text-decoration: none;
  32. }
  33. // ul, li{
  34. // list-style-type: none;
  35. // padding: 0;
  36. // }
  37. .clearfix {
  38. &:after {
  39. visibility: hidden;
  40. display: block;
  41. font-size: 0;
  42. content: " ";
  43. clear: both;
  44. height: 0;
  45. }
  46. }
  47. //main-container全局样式
  48. .app-main{
  49. min-height: 100%
  50. }
  51. .app-container {
  52. background-color: #fff;
  53. }
  54. .app-container.wrapper{
  55. padding: 20px;
  56. }
  57. .svg-icon {
  58. width: 1em;
  59. height: 1em;
  60. vertical-align: -0.15em;
  61. fill: currentColor;
  62. overflow: hidden;
  63. }
  64. input:-webkit-autofill {
  65. box-shadow: 0 0 0px 1000px #fff inset !important;//关于解决输入框背景颜色
  66. -webkit-text-fill-color: #696969 !important;//关于接输入框文字颜色
  67. }

发表评论

表情:
评论列表 (有 0 条评论,86人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Winform原理

     前两天跟着做村级节水系统,在其中遇到了换肤功能的实现。所以,想具体了解一下换肤原理。 跟踪代码,Winform换肤的实现主要是读取换肤的配置文件,然后重绘所有所有窗体