当前位置:   article > 正文

css 变量(自定义属性)实际应用(CRM based Vue)_scss #{$--header-height}

scss #{$--header-height}

css 变量的定义与使用

以 "--" 作为属性名的开始,通过 var(变量名) 使用变量。

  1. .app {
  2. --base-color: #323232; // 定义
  3. }
  4. .app .container {
  5. background-color: var(--base-color) // 使用
  6. }

实际应用

场景:菜单的展开与折叠。

  1. <!-- layout/index.vue -->
  2. <template>
  3. <div>
  4. <el-container :style="{'--sidebar-width': sidebarWidth+'px'}">
  5. <!-- 侧边菜单 -->
  6. <el-aside>
  7. <Sidebar @switchCollaps="switchCollaps" />
  8. </el-aside>
  9. <el-container>
  10. <!-- 头部 -->
  11. <el-header>
  12. <Header />
  13. </el-header>
  14. <!-- 内容区域 -->
  15. <el-main>
  16. <div class="view-container">
  17. <router-view />
  18. </div>
  19. </el-main>
  20. </el-container>
  21. </el-container>
  22. </div>
  23. </template>
  24. <script>
  25. import Header from './header.vue';
  26. import Sidebar from './sidebar.vue';
  27. export default {
  28. components: {
  29. Header,
  30. Sidebar
  31. },
  32. data () {
  33. return {
  34. sidebarWidth: 200
  35. }
  36. },
  37. methods: {
  38. switchCollaps (collaps) {
  39. this.sidebarWidth = collaps ? 40 : 240;
  40. }
  41. }
  42. }
  43. </script>
  44. <style lang="scss" scoped>
  45. $header-height: 56px;
  46. $base-dis: 16px;
  47. .el-aside {
  48. width: var(--sidebar-width) !important;
  49. }
  50. .el-header {
  51. height: $header-height !important;
  52. box-shadow: 0px 1px 0px 0px #EEEEEE;
  53. z-index: 1;
  54. overflow: hidden;
  55. padding: 0 $base-dis;
  56. }
  57. .el-main {
  58. background-color: rgb(245, 245, 245);
  59. padding: $base-dis;
  60. height: calc(100vh - #{$header-height});
  61. overflow: auto;
  62. .view-container {
  63. box-sizing: border-box;
  64. padding: $base-dis;
  65. background-color: #fff;
  66. }
  67. }
  68. </style>
  1. <!-- layout/header.vue -->
  2. <template>
  3. <div class="header flex">
  4. <div class="location">
  5. location
  6. </div>
  7. <div class="info flex">
  8. <ul class="flex">
  9. <li>公告</li>
  10. <li>消息</li>
  11. </ul>
  12. <div class="info-user flex">
  13. <i class="el-icon-s-custom"></i>
  14. <div class="info-user-name">
  15. <p>wzm</p>
  16. <p class="permission">admin</p>
  17. </div>
  18. </div>
  19. </div>
  20. </div>
  21. </template>
  22. <style lang="scss" scoped>
  23. $base-color: #006186;
  24. $header-height: 56px;
  25. $base-dis: 16px;
  26. .flex {
  27. display: flex;
  28. }
  29. .header {
  30. background-color: #fff;
  31. justify-content: space-between;
  32. .location {
  33. line-height: $header-height;
  34. }
  35. .info {
  36. ul {
  37. list-style: none;
  38. display: flex;
  39. line-height: $header-height;
  40. margin-right: 2 * $base-dis;
  41. li {
  42. padding: 0 $base-dis;
  43. cursor: pointer;
  44. }
  45. }
  46. &-user {
  47. align-items: center;
  48. i {
  49. color: $base-color;
  50. font-size: 26px;
  51. margin-right: $base-dis/2;
  52. }
  53. &-name {
  54. .permission {
  55. font-size: 12px;
  56. color: rgb(139, 131, 131);
  57. }
  58. }
  59. }
  60. }
  61. }
  62. </style>
  1. <!-- layout/sidebar.vue -->
  2. <template>
  3. <div class="sidebar">
  4. <div class="sidebar-top">
  5. <div v-if="!isCollapse" class="sidebar-top-title"> vue-element-pre </div>
  6. <div>
  7. <i
  8. :class="{
  9. 'el-icon-s-unfold': isCollapse,
  10. 'el-icon-s-fold': !isCollapse
  11. }"
  12. @click="switchCollaps"
  13. ></i>
  14. </div>
  15. </div>
  16. </div>
  17. </template>
  18. <script>
  19. export default {
  20. data () {
  21. return {
  22. isCollapse: false
  23. }
  24. },
  25. methods: {
  26. switchCollaps () {
  27. this.isCollapse = !this.isCollapse;
  28. this.$emit('switchCollaps', this.isCollapse);
  29. }
  30. }
  31. }
  32. </script>
  33. <style lang="scss" scoped>
  34. $base-color: #006186;
  35. $base-dis: 16px;
  36. $top-w-h: 40px;
  37. .sidebar {
  38. height: 100vh;
  39. background-color: $base-color;
  40. color: #fff;
  41. // padding: $base-dis;
  42. &-top {
  43. display: flex;
  44. justify-content: space-between;
  45. &-title {
  46. height: $top-w-h;
  47. line-height: $top-w-h;
  48. padding-left: $base-dis;
  49. }
  50. i {
  51. cursor: pointer;
  52. width: $top-w-h;
  53. height: $top-w-h;
  54. display: flex;
  55. justify-content: center;
  56. align-items: center;
  57. &:hover {
  58. background-image: linear-gradient(to right, $base-color, #4191b1);
  59. }
  60. }
  61. }
  62. }
  63. </style>

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

闽ICP备14008679号