当前位置:   article > 正文

vue组件化-换肤_vue themepicker组件

vue themepicker组件

需要使用的页面直接使用即可

  1. <template><!-- 换肤 -->
  2. <div title="换肤">
  3. <el-color-picker v-model="theme" class="theme-picker" popper-class="theme-picker-dropdown" size="default" />
  4. </div>
  5. </template>
  1. <script>
  2. import elePlus from "element-plus/package.json";
  3. import { ElMessage } from 'element-plus'//引入消息提示组件
  4. const version = elePlus.version; // element-ui version from node_modules
  5. const ORIGINAL_THEME = "#409EFF"; // default color
  6. export default {
  7. data() {
  8. return {
  9. chalk: "", // content of theme-chalk css
  10. theme: ORIGINAL_THEME,
  11. };
  12. },
  13. watch: {
  14. theme(val, oldVal) {
  15. if (typeof val !== "string") return;
  16. const themeCluster = this.getThemeCluster(val.replace("#", ""));
  17. const originalCluster = this.getThemeCluster(oldVal.replace("#", ""));
  18. const getHandler = (variable, id) => {
  19. return () => {
  20. const originalCluster = this.getThemeCluster(
  21. ORIGINAL_THEME.replace("#", "")
  22. );
  23. const newStyle = this.updateStyle(
  24. this[variable],
  25. originalCluster,
  26. themeCluster
  27. );
  28. let styleTag = document.getElementById(id);
  29. if (!styleTag) {
  30. styleTag = document.createElement("style");
  31. styleTag.setAttribute("id", id);
  32. document.head.appendChild(styleTag);
  33. }
  34. styleTag.innerText = newStyle;
  35. };
  36. };
  37. const chalkHandler = getHandler("chalk", "chalk-style");
  38. if (!this.chalk) {
  39. const url = `https://unpkg.com/element-plus@${version}/theme-chalk/index.css`;
  40. this.getCSSString(url, chalkHandler, "chalk");
  41. } else {
  42. chalkHandler();
  43. }
  44. const styles = [].slice
  45. .call(document.querySelectorAll("style"))
  46. .filter((style) => {
  47. const text = style.innerText;
  48. return (
  49. new RegExp(oldVal, "i").test(text) && !/Chalk Variables/.test(text)
  50. );
  51. });
  52. styles.forEach((style) => {
  53. const { innerText } = style;
  54. if (typeof innerText !== "string") return;
  55. style.innerText = this.updateStyle(
  56. innerText,
  57. originalCluster,
  58. themeCluster
  59. );
  60. });
  61. setTimeout(() => {
  62. ElMessage.success("换肤成功");
  63. }, 1);
  64. },
  65. },
  66. methods: {
  67. updateStyle(style, oldCluster, newCluster) {
  68. let newStyle = style;
  69. oldCluster.forEach((color, index) => {
  70. newStyle = newStyle.replace(new RegExp(color, "ig"), newCluster[index]);
  71. });
  72. return newStyle;
  73. },
  74. getCSSString(url, callback, variable) {
  75. const xhr = new XMLHttpRequest();
  76. xhr.onreadystatechange = () => {
  77. if (xhr.readyState === 4 && xhr.status === 200) {
  78. this[variable] = xhr.responseText.replace(/@font-face{[^}]+}/, "");
  79. callback();
  80. }
  81. };
  82. xhr.open("GET", url);
  83. xhr.send();
  84. },
  85. getThemeCluster(theme) {
  86. const tintColor = (color, tint) => {
  87. let red = parseInt(color.slice(0, 2), 16);
  88. let green = parseInt(color.slice(2, 4), 16);
  89. let blue = parseInt(color.slice(4, 6), 16);
  90. if (tint === 0) {
  91. // when primary color is in its rgb space
  92. return [red, green, blue].join(",");
  93. } else {
  94. red += Math.round(tint * (255 - red));
  95. green += Math.round(tint * (255 - green));
  96. blue += Math.round(tint * (255 - blue));
  97. red = red.toString(16);
  98. green = green.toString(16);
  99. blue = blue.toString(16);
  100. return `#${red}${green}${blue}`;
  101. }
  102. };
  103. const shadeColor = (color, shade) => {
  104. let red = parseInt(color.slice(0, 2), 16);
  105. let green = parseInt(color.slice(2, 4), 16);
  106. let blue = parseInt(color.slice(4, 6), 16);
  107. red = Math.round((1 - shade) * red);
  108. green = Math.round((1 - shade) * green);
  109. blue = Math.round((1 - shade) * blue);
  110. red = red.toString(16);
  111. green = green.toString(16);
  112. blue = blue.toString(16);
  113. return `#${red}${green}${blue}`;
  114. };
  115. const clusters = [theme];
  116. for (let i = 0; i <= 9; i++) {
  117. clusters.push(tintColor(theme, Number((i / 10).toFixed(2))));
  118. }
  119. clusters.push(shadeColor(theme, 0.1));
  120. return clusters;
  121. },
  122. },
  123. };
  124. </script>
  1. .theme-picker .el-color-picker__trigger {
  2. vertical-align: middle;
  3. }
  4. .theme-picker-dropdown .el-color-dropdown__link-btn {
  5. display: none;
  6. }
  7. </style>

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

闽ICP备14008679号