0


后台管理系统中常见的三栏布局总结:使用element ui构建

使用的element ui中el-menu
在这里插入图片描述

  • vue2 使用 el-menu构建的列表布局: 列表可以折叠展开
  1. <template>
  2. <div class="home">
  3. <header>
  4. <el-button type="primary" @click="handleClick">切换</el-button>
  5. </header>
  6. <div class="content">
  7. <el-scrollbar class="sidebar">
  8. <el-menu
  9. class="menubox"
  10. :collapse="this.$store.state.isCollapse"
  11. background-color="#25D083"
  12. :class="{ collapse: this.$store.state.isCollapse }"
  13. >
  14. <!-- 注意:这里是有扩展的子列表 , title放名称, el-men-item放的是具体内容 ! -->
  15. <el-submenu index="1">
  16. <template slot="title">
  17. <i class="el-icon-location icon"></i>
  18. <span slot="title">导航一</span>
  19. </template>
  20. <el-menu-item index="1-4-1">
  21. <i class="el-icon-location icon"></i>
  22. <span>选项1</span>
  23. </el-menu-item>
  24. <el-menu-item index="1-4-1">
  25. <i class="el-icon-location icon"></i>
  26. <span>选项2</span>
  27. </el-menu-item>
  28. </el-submenu>
  29. <el-menu-item
  30. index="2"
  31. >
  32. <i class="el-icon-menu icon"></i>
  33. <span slot="title">导航二</span>
  34. </el-menu-item>
  35. <el-menu-item index="3">
  36. <i class="el-icon-document icon"></i>
  37. <span slot="title">导航三</span>
  38. </el-menu-item>
  39. <el-menu-item index="4">
  40. <i class="el-icon-setting icon"></i>
  41. <span slot="title">导航四</span>
  42. </el-menu-item>
  43. </el-menu>
  44. </el-scrollbar>
  45. <div class="mainbox"></div>
  46. </div>
  47. </div>
  48. </template>
  49. <script>
  50. // @ is an alias to /src
  51. export default {
  52. name: "HomeView",
  53. data() {
  54. return {
  55. isCollapse: true,
  56. };
  57. },
  58. methods: {
  59. handleClick() {
  60. this.$store.dispatch("change");
  61. },
  62. },
  63. };
  64. </script>
  65. <style lang="scss">
  66. $topHeight: 60px;
  67. $minWidth: 50px;
  68. $maxWidth: 160px;
  69. .border {
  70. border: 1px solid blue;
  71. }
  72. .home {
  73. box-sizing: border-box;
  74. overflow: hidden;
  75. height: 100vh;
  76. header {
  77. height: $topHeight;
  78. line-height: $topHeight;
  79. background: pink;
  80. }
  81. .content {
  82. display: flex;
  83. height: calc(100vh - $topHeight);
  84. // 侧边滚动条
  85. .sidebar {
  86. height: 100%;
  87. background: #25d083;
  88. // 设置最大高度,当高度超出时候滚动
  89. .menubox {
  90. width: $maxWidth;
  91. max-height: calc(100vh - $topHeight);
  92. transition: all 0.3s; // 设置过渡效果
  93. // 自定义修改el-menu的布局, 标题对应关系.el-submenu__title 和 el-menu-item
  94. .el-submenu > .el-submenu__title,
  95. .el-menu-item {
  96. margin: 20px auto;
  97. display: flex;
  98. flex-direction: column;
  99. justify-content: center;
  100. align-items: center;
  101. height: 70px; // 默认的高度是56px, line-height也是56px, 根据需要修改
  102. line-height: 40px !important;
  103. .icon {
  104. font-size: 25px;
  105. }
  106. span {
  107. font-size: 14px;
  108. }
  109. }
  110. // 折叠后,el-menu-item的图标距离左边很远,发现是padding导致的,要!important
  111. .el-menu-item {
  112. .el-tooltip {
  113. padding: 0 !important;
  114. }
  115. }
  116. // 当el-menu折叠的时候
  117. &.collapse {
  118. width: $minWidth;
  119. }
  120. }
  121. }
  122. .mainbox {
  123. flex: 1;
  124. background: rgb(217, 236, 171);
  125. }
  126. }
  127. }
  128. </style>

涉及到的知识点整理

  1. el-scrollbar 是element ui中的内置组件,没有官方文档。- 使用场景: 当只需要某一部分高度超出滚动,而不影响整体的布局时,将该部分用el-scrollbar包裹即可,一般要设置这部分的最大高度。<el-scrollbar> <box style="max-height: 200px"> </box></el-scrollbar>// 有时可能有横向滚动条/* 隐藏横向滚动条 */.el-scrollbar__wrap{ overflow-x: hidden;}
  2. 将控制折叠展开的变量放在store中,防止跨组件通信比较麻烦
  3. 修改el-submenu 和 el-menu-item的样式(css基础不好,这块整理了好久才明白):自定义修改el-menu的布局, 标题对应关系.el-submenu__title 和 el-menu-item (牢记这两个位置)- 问题: 折叠后,发现自定义的宽度导致图标并没有居中- 解决方法:!important.el-menu-item{.el-tooltip{padding: 0 !important;}}
  4. 小建议: 可以使用scss先定义好 变量(侧边栏)的宽度,如200 -> 60, 在最外层加过渡效果,视觉更友好
标签: ui vue.js javascript

本文转载自: https://blog.csdn.net/qq_33404590/article/details/131998321
版权归原作者 yy_33404590 所有, 如有侵权,请联系我们删除。

“后台管理系统中常见的三栏布局总结:使用element ui构建”的评论:

还没有评论