当前位置:   article > 正文

五分钟完成侧边栏组件_vue侧边栏组件

vue侧边栏组件

效果展示

目录结构

主入口: main.vue

  1. <template>
  2. <div class="app">
  3. <Header :isSideBarShow="isSideBarShow" @isSideBarShow="sideBarShow" />
  4. <div class="main">
  5. <SideBar :isSideBarShow="isSideBarShow" @reciveIndex="reciveIndex"></SideBar>
  6. <div ref="content" :style="{left: contentLeft + 'px'}" class="content">
  7. <!-- <keep-alive exclude="ScrollDemo">
  8. <components :is="pageName"></components>
  9. </keep-alive>-->
  10. <router-view></router-view>
  11. </div>
  12. </div>
  13. </div>
  14. </template>
  15. <script>
  16. import Header from "../../components/header/index.vue";
  17. import SideBar from "../../components/sideBar/index.vue";
  18. // 页面
  19. export default {
  20. name: "App",
  21. components: {
  22. Header,
  23. SideBar
  24. },
  25. data() {
  26. return {
  27. isShow: false,
  28. isSideBarShow: true // 控制侧边栏是否显示
  29. };
  30. },
  31. mounted() {
  32. window.app = this;
  33. },
  34. computed: {
  35. contentLeft() {
  36. // 控制右侧是否全屏
  37. return this.isSideBarShow ? 200 : 0;
  38. }
  39. },
  40. methods: {
  41. reciveIndex(componentName) {
  42. this.pageName = componentName;
  43. },
  44. sideBarShow() {
  45. this.isSideBarShow = !this.isSideBarShow;
  46. }
  47. }
  48. };
  49. </script>
  50. <style scoped>
  51. .app {
  52. position: absolute;
  53. left: 0;
  54. top: 0;
  55. bottom: 0;
  56. right: 0;
  57. }
  58. .main {
  59. position: absolute;
  60. left: 0;
  61. right: 0;
  62. top: 81px;
  63. bottom: 0;
  64. }
  65. .content {
  66. position: absolute;
  67. right: 0;
  68. top: 0px;
  69. bottom: 0px;
  70. background-color: #eee;
  71. padding-left: 10px;
  72. padding-top: 10px;
  73. overflow: auto;
  74. transition: all 0.4s;
  75. }
  76. </style>

slideBar.vue

  1. <template>
  2. <transition>
  3. <div v-show="isSideBarShow" class="side-bar">
  4. <ul>
  5. <li
  6. :class="$route.matched[1].path === item.toPath ? 'active' : ''"
  7. @click="handleClick(item, key)"
  8. v-for="(item,key) in asideData"
  9. :key="key"
  10. >
  11. <span>{{item.name}}</span>
  12. </li>
  13. </ul>
  14. </div>
  15. </transition>
  16. </template>
  17. <script>
  18. import { createAsideData } from "../../router/router.config";
  19. export default {
  20. data() {
  21. return {
  22. asideData: createAsideData()
  23. };
  24. },
  25. props: ["isSideBarShow"],
  26. methods: {
  27. handleClick(item, key) {
  28. this.pageIndex = key;
  29. this.$router.push(item.toPath);
  30. }
  31. },
  32. mounted() {
  33. window.side = this;
  34. }
  35. };
  36. </script>
  37. <style scoped>
  38. .side-bar {
  39. position: absolute;
  40. left: 0px;
  41. top: 0;
  42. width: 200px;
  43. bottom: 0;
  44. background-color: rgb(27, 38, 44);
  45. color: rgb(187, 225, 250);
  46. overflow-y: auto;
  47. }
  48. ul {
  49. padding: 0;
  50. cursor: pointer;
  51. margin: 0;
  52. }
  53. li {
  54. padding-left: 20px;
  55. list-style: none;
  56. line-height: 40px;
  57. background-color: rgb(15, 76, 117);
  58. margin-bottom: 1px;
  59. }
  60. li:hover,
  61. li.active {
  62. background-color: rgb(50, 130, 184);
  63. color: #fff;
  64. }
  65. .v-enter {
  66. left: -200px;
  67. }
  68. .v-enter-active {
  69. transition: all 0.3s;
  70. }
  71. .v-leave-active {
  72. left: -200px;
  73. transition: all 2s;
  74. }
  75. </style>

二级路由配置

  1. const routerConfigAside = [
  2. {
  3. path: '',
  4. redirect: 'todolist'
  5. },
  6. { title: '待办事项', path: 'todolist', component: TodoList, meta: { num: 23 } },
  7. { title: '拖拽效果', path: 'dragtest', component: DragTest },
  8. { title: '简单滚动条', path: 'scrolltest', component: ScrollTest },
  9. { title: '滚动条案例', path: 'scrolldemo', component: ScrollDemo },
  10. { title: '组件通信', path: 'infotest', component: InfoTest },
  11. { title: '监听属性', path: 'listening', component: Listening },
  12. { title: '收集信息', path: 'collectionsfunctions', component: CollectionsFunctions },
  13. { title: '订单系统', path: 'orderos', component: OrderOs },
  14. { title: '动画案例', path: 'transitionTest', component: TransitionTest },
  15. { title: '考试-导航', path: 'navtest', component: NavTest },
  16. { title: '生命周期', path: 'lifeloop', component: LifeLoop },
  17. {
  18. title: '动态路由',
  19. path: 'moverouter',
  20. component: MoveRouter,
  21. children: [
  22. {
  23. path: '',
  24. redirect: 'list'
  25. },
  26. {
  27. name: 'list',
  28. path: 'list',
  29. alias: 'listOther',
  30. component: MoveRouterList
  31. },
  32. {
  33. name: 'detail',
  34. path: 'detail/:sid',
  35. component: MoveRouterDetail
  36. }
  37. ]
  38. },
  39. {
  40. title: '插槽',
  41. path: 'slot',
  42. component: VueSlot
  43. },
  44. { title: '状态管理-vuex', path: 'storemanger', component: StoreManger },
  45. { title: '订单系统-vuex', path: 'orderosvuex', component: OrderOsVuex },
  46. { title: '递归组件-tree', path: 'tree', component: Tree },
  47. { title: '仿照windows目录', path: 'windowsfloder', component: WidowsFloder }
  48. ]
  49. // 生成侧边栏数据
  50. export function createAsideData() {
  51. return routerConfigAside.filter(item => item.title).map(item => {
  52. return {
  53. toPath: routerConfig[2].path + '/' + item.path,
  54. name: item.title
  55. }
  56. })
  57. }

Header组件

  1. <template>
  2. <div class="header">
  3. <h1>
  4. <a href="/">
  5. </a>
  6. </h1>
  7. <div class="controlSidebar">
  8. <button
  9. :class="[isSideBarShow ? 'hide' : 'show']"
  10. @click="btnHandleClick"
  11. >{{isSideBarShow ? '隐藏' : '显示'}}</button>
  12. </div>
  13. <div class="weektwo">
  14. <router-link to="/weektwo">查看周考二试题</router-link>
  15. </div>
  16. <div class="tool">
  17. <button>
  18. <router-link to="/login">退出</router-link>
  19. </button>
  20. </div>
  21. </div>
  22. </template>
  23. <script>
  24. export default {
  25. props: {
  26. isSideBarShow: {
  27. type: Boolean,
  28. default() {
  29. return true;
  30. }
  31. }
  32. },
  33. methods: {
  34. btnHandleClick() {
  35. this.$emit("isSideBarShow");
  36. }
  37. }
  38. };
  39. </script>
  40. <style scoped>
  41. h1 {
  42. margin: 0;
  43. padding: 0;
  44. float: left;
  45. height: 100%;
  46. padding-left: 10px;
  47. }
  48. h1 img {
  49. display: inline-block;
  50. position: relative;
  51. top: 10px;
  52. }
  53. .header {
  54. height: 80px;
  55. border-bottom: 1px solid rgb(27, 38, 44);
  56. background-color: #fff;
  57. }
  58. .controlSidebar {
  59. float: left;
  60. height: 100%;
  61. padding-left: 20px;
  62. position: relative;
  63. width: 100px;
  64. }
  65. .controlSidebar button {
  66. position: absolute;
  67. top: 50%;
  68. min-width: 75px;
  69. transform: translateY(-50%);
  70. outline: none;
  71. border: 1px solid #ccc;
  72. padding: 5px 20px;
  73. border-radius: 10px;
  74. background: transparent;
  75. cursor: pointer;
  76. }
  77. .controlSidebar button.show {
  78. background: #011801;
  79. color: #fff;
  80. }
  81. .tool {
  82. float: right;
  83. height: 100%;
  84. padding-right: 100px;
  85. position: relative;
  86. }
  87. .tool button {
  88. position: absolute;
  89. top: 50%;
  90. transform: translateY(-50%);
  91. border: 1px solid #ccc;
  92. outline: none;
  93. background-color: transparent;
  94. padding: 10px 20px;
  95. border-radius: 8px;
  96. }
  97. .weektwo {
  98. float: left;
  99. line-height: 80px;
  100. }
  101. </style>

无语非说我质量有问题,凑个字数……

无语非说我质量有问题,凑个字数……

无语非说我质量有问题,凑个字数……

无语非说我质量有问题,凑个字数……

无语非说我质量有问题,凑个字数……

无语非说我质量有问题,凑个字数……

无语非说我质量有问题,凑个字数……

无语非说我质量有问题,凑个字数……

无语非说我质量有问题,凑个字数……

无语非说我质量有问题,凑个字数……

无语非说我质量有问题,凑个字数……

无语非说我质量有问题,凑个字数……

 

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号