side-nav.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <style>
  2. .side-nav {
  3. width: 100%;
  4. box-sizing: border-box;
  5. padding-right: 30px;
  6. transition: opacity .5s;
  7. &:hover {
  8. opacity: 1 !important;
  9. }
  10. li {
  11. list-style: none;
  12. }
  13. ul {
  14. padding: 0;
  15. margin: 0;
  16. overflow: hidden;
  17. }
  18. > ul > .nav-item > a {
  19. margin-top: 15px;
  20. }
  21. > ul > .nav-item:nth-child(-n + 3) > a {
  22. margin-top: 0;
  23. }
  24. .nav-item {
  25. a {
  26. font-size: 16px;
  27. color: #333;
  28. line-height: 40px;
  29. height: 40px;
  30. margin: 0;
  31. padding: 0;
  32. text-decoration: none;
  33. display: block;
  34. position: relative;
  35. transition: .15s ease-out;
  36. font-weight: bold;
  37. &.active {
  38. color: #409EFF;
  39. }
  40. }
  41. .nav-item {
  42. a {
  43. display: block;
  44. height: 40px;
  45. color: #444;
  46. line-height: 40px;
  47. font-size: 14px;
  48. overflow: hidden;
  49. white-space: nowrap;
  50. text-overflow: ellipsis;
  51. font-weight: normal;
  52. &:hover,
  53. &.active {
  54. color: #409EFF;
  55. }
  56. }
  57. }
  58. }
  59. .nav-group__title {
  60. font-size: 12px;
  61. color: #999;
  62. line-height: 26px;
  63. margin-top: 15px;
  64. }
  65. #code-sponsor-widget {
  66. margin: 0 0 0 -20px;
  67. }
  68. }
  69. .nav-dropdown-list {
  70. width: 120px;
  71. margin-top: -8px;
  72. li {
  73. font-size: 14px;
  74. }
  75. }
  76. </style>
  77. <template>
  78. <div
  79. class="side-nav"
  80. @mouseenter="isFade = false"
  81. :style="navStyle">
  82. <ul>
  83. <li class="nav-item" v-for="item in data">
  84. <a v-if="!item.path && !item.href" @click="expandMenu">{{item.name}}</a>
  85. <a v-if="item.href" :href="item.href" target="_blank">{{item.name}}</a>
  86. <router-link
  87. v-if="item.path"
  88. active-class="active"
  89. :to="base + item.path"
  90. exact
  91. v-text="item.title || item.name">
  92. </router-link>
  93. <ul class="pure-menu-list sub-nav" v-if="item.children">
  94. <li class="nav-item" v-for="navItem in item.children">
  95. <router-link
  96. class=""
  97. active-class="active"
  98. :to="base + navItem.path"
  99. exact
  100. v-text="navItem.title || navItem.name">
  101. </router-link>
  102. </li>
  103. </ul>
  104. <template v-if="item.groups">
  105. <div class="nav-group" v-for="group in item.groups">
  106. <div class="nav-group__title" @click="expandMenu">{{group.groupName}}</div>
  107. <ul class="pure-menu-list">
  108. <li
  109. class="nav-item"
  110. v-for="navItem in group.list"
  111. v-if="!navItem.disabled">
  112. <router-link
  113. active-class="active"
  114. :to="base + navItem.path"
  115. exact
  116. v-text="navItem.title"></router-link>
  117. </li>
  118. </ul>
  119. </div>
  120. </template>
  121. </li>
  122. </ul>
  123. <!--<div id="code-sponsor-widget"></div>-->
  124. </div>
  125. </template>
  126. <script>
  127. import bus from '../bus';
  128. import compoLang from '../i18n/component.json';
  129. export default {
  130. props: {
  131. data: Array,
  132. base: {
  133. type: String,
  134. default: ''
  135. }
  136. },
  137. data() {
  138. return {
  139. highlights: [],
  140. navState: [],
  141. isSmallScreen: false,
  142. isFade: false
  143. };
  144. },
  145. watch: {
  146. '$route.path'() {
  147. this.handlePathChange();
  148. },
  149. isFade(val) {
  150. bus.$emit('navFade', val);
  151. }
  152. },
  153. computed: {
  154. navStyle() {
  155. const style = {};
  156. if (this.isSmallScreen) {
  157. style.paddingBottom = '60px';
  158. }
  159. if (this.isFade) {
  160. style.opacity = '0.5';
  161. }
  162. return style;
  163. },
  164. langConfig() {
  165. return compoLang.filter(config => config.lang === this.$route.meta.lang)[0]['nav'];
  166. }
  167. },
  168. methods: {
  169. handleResize() {
  170. this.isSmallScreen = document.documentElement.clientWidth < 768;
  171. this.handlePathChange();
  172. },
  173. handlePathChange() {
  174. if (!this.isSmallScreen) {
  175. this.expandAllMenu();
  176. return;
  177. }
  178. this.$nextTick(() => {
  179. this.hideAllMenu();
  180. let activeAnchor = this.$el.querySelector('a.active');
  181. let ul = activeAnchor.parentNode;
  182. while (ul.tagName !== 'UL') {
  183. ul = ul.parentNode;
  184. }
  185. ul.style.height = 'auto';
  186. });
  187. },
  188. hideAllMenu() {
  189. [].forEach.call(this.$el.querySelectorAll('.pure-menu-list'), ul => {
  190. ul.style.height = '0';
  191. });
  192. },
  193. expandAllMenu() {
  194. [].forEach.call(this.$el.querySelectorAll('.pure-menu-list'), ul => {
  195. ul.style.height = 'auto';
  196. });
  197. },
  198. expandMenu(event) {
  199. if (!this.isSmallScreen) return;
  200. let target = event.currentTarget;
  201. if (!target.nextElementSibling || target.nextElementSibling.tagName !== 'UL') return;
  202. this.hideAllMenu();
  203. event.currentTarget.nextElementSibling.style.height = 'auto';
  204. }
  205. },
  206. created() {
  207. bus.$on('fadeNav', () => {
  208. this.isFade = true;
  209. });
  210. },
  211. mounted() {
  212. this.handleResize();
  213. window.addEventListener('resize', this.handleResize);
  214. },
  215. beforeDestroy() {
  216. window.removeEventListener('resize', this.handleResize);
  217. }
  218. };
  219. </script>