side-nav.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <style>
  2. .side-nav {
  3. width: 100%;
  4. box-sizing: border-box;
  5. padding-right: 30px;
  6. li {
  7. list-style: none;
  8. }
  9. ul {
  10. padding: 0;
  11. margin: 0;
  12. overflow: hidden;
  13. }
  14. .nav-item {
  15. a {
  16. font-size: 16px;
  17. color: #5e6d82;
  18. line-height: 40px;
  19. height: 40px;
  20. margin: 0;
  21. padding: 0;
  22. text-decoration: none;
  23. display: block;
  24. position: relative;
  25. transition: all .3s;
  26. &.active {
  27. color: #20a0ff;
  28. }
  29. }
  30. .nav-item {
  31. a {
  32. display: block;
  33. height: 40px;
  34. line-height: 40px;
  35. font-size: 13px;
  36. padding-left: 24px;
  37. &:hover {
  38. color: #20a0ff;
  39. }
  40. }
  41. }
  42. }
  43. .nav-group__title {
  44. font-size: 12px;
  45. color: #99a9bf;
  46. padding-left: 8px;
  47. line-height: 26px;
  48. margin-top: 10px;
  49. }
  50. }
  51. </style>
  52. <template>
  53. <div class="side-nav" :style="navStyle">
  54. <ul>
  55. <li class="nav-item" v-for="item in data">
  56. <a v-if="!item.path" @click="expandMenu">{{item.name}}</a>
  57. <router-link
  58. v-else
  59. active-class="active"
  60. :to="base + item.path"
  61. exact
  62. v-text="item.title || item.name">
  63. </router-link>
  64. <ul class="pure-menu-list sub-nav" v-if="item.children">
  65. <li class="nav-item" v-for="navItem in item.children">
  66. <router-link
  67. class=""
  68. active-class="active"
  69. :to="base + navItem.path"
  70. exact
  71. v-text="navItem.title || navItem.name">
  72. </router-link>
  73. </li>
  74. </ul>
  75. <template v-if="item.groups">
  76. <div class="nav-group" v-for="group in item.groups">
  77. <div class="nav-group__title" @click="expandMenu">{{group.groupName}}</div>
  78. <ul class="pure-menu-list">
  79. <li
  80. class="nav-item"
  81. v-for="navItem in group.list"
  82. v-if="!navItem.disabled">
  83. <router-link
  84. active-class="active"
  85. :to="base + navItem.path"
  86. exact
  87. v-text="navItem.title"></router-link>
  88. </li>
  89. </ul>
  90. </div>
  91. </template>
  92. </li>
  93. </ul>
  94. </div>
  95. </template>
  96. <script>
  97. export default {
  98. props: {
  99. data: Array,
  100. base: {
  101. type: String,
  102. default: ''
  103. }
  104. },
  105. data() {
  106. return {
  107. highlights: [],
  108. navState: [],
  109. isSmallScreen: false,
  110. };
  111. },
  112. watch: {
  113. '$route.path'() {
  114. this.handlePathChange();
  115. }
  116. },
  117. computed: {
  118. navStyle() {
  119. return this.isSmallScreen ? { 'padding-bottom': '60px' } : {};
  120. }
  121. },
  122. methods: {
  123. handleResize() {
  124. this.isSmallScreen = document.documentElement.clientWidth < 768;
  125. this.handlePathChange();
  126. },
  127. handlePathChange() {
  128. if (!this.isSmallScreen) {
  129. this.expandAllMenu();
  130. return;
  131. }
  132. this.$nextTick(() => {
  133. this.hideAllMenu();
  134. let activeAnchor = this.$el.querySelector('a.active');
  135. let ul = activeAnchor.parentNode;
  136. while (ul.tagName !== 'UL') {
  137. ul = ul.parentNode;
  138. }
  139. ul.style.height = 'auto';
  140. });
  141. },
  142. hideAllMenu() {
  143. [].forEach.call(this.$el.querySelectorAll('.pure-menu-list'), ul => {
  144. ul.style.height = '0';
  145. });
  146. },
  147. expandAllMenu() {
  148. [].forEach.call(this.$el.querySelectorAll('.pure-menu-list'), ul => {
  149. ul.style.height = 'auto';
  150. });
  151. },
  152. expandMenu(event) {
  153. if (!this.isSmallScreen) return;
  154. let target = event.currentTarget;
  155. if (!target.nextElementSibling || target.nextElementSibling.tagName !== 'UL') return;
  156. this.hideAllMenu();
  157. event.currentTarget.nextElementSibling.style.height = 'auto';
  158. }
  159. },
  160. mounted() {
  161. this.handleResize();
  162. window.addEventListener('resize', this.handleResize);
  163. },
  164. beforeDestroy() {
  165. window.removeEventListener('resize', this.handleResize);
  166. }
  167. };
  168. </script>