side-nav.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <style>
  2. .side-nav {
  3. width: 100%;
  4. box-sizing: border-box;
  5. padding-right: 30px;
  6. transition: opacity .3s;
  7. &.is-fade {
  8. transition: opacity 3s;
  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 + 4) > 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. :class="{ 'is-fade': isFade }"
  82. :style="navStyle">
  83. <ul>
  84. <li class="nav-item" v-for="item in data">
  85. <a v-if="!item.path && !item.href" @click="expandMenu">{{item.name}}</a>
  86. <a v-if="item.href" :href="item.href" target="_blank">{{item.name}}</a>
  87. <router-link
  88. v-if="item.path"
  89. active-class="active"
  90. :to="base + item.path"
  91. exact
  92. v-text="item.title || item.name">
  93. </router-link>
  94. <ul class="pure-menu-list sub-nav" v-if="item.children">
  95. <li class="nav-item" v-for="navItem in item.children">
  96. <router-link
  97. class=""
  98. active-class="active"
  99. :to="base + navItem.path"
  100. exact
  101. v-text="navItem.title || navItem.name">
  102. </router-link>
  103. </li>
  104. </ul>
  105. <template v-if="item.groups">
  106. <div class="nav-group" v-for="group in item.groups">
  107. <div class="nav-group__title" @click="expandMenu">{{group.groupName}}</div>
  108. <ul class="pure-menu-list">
  109. <li
  110. class="nav-item"
  111. v-for="navItem in group.list"
  112. v-if="!navItem.disabled">
  113. <router-link
  114. active-class="active"
  115. :to="base + navItem.path"
  116. exact
  117. v-text="navItem.title"></router-link>
  118. </li>
  119. </ul>
  120. </div>
  121. </template>
  122. </li>
  123. </ul>
  124. <!--<div id="code-sponsor-widget"></div>-->
  125. </div>
  126. </template>
  127. <script>
  128. import bus from '../bus';
  129. import compoLang from '../i18n/component.json';
  130. export default {
  131. props: {
  132. data: Array,
  133. base: {
  134. type: String,
  135. default: ''
  136. }
  137. },
  138. data() {
  139. return {
  140. highlights: [],
  141. navState: [],
  142. isSmallScreen: false,
  143. isFade: false
  144. };
  145. },
  146. watch: {
  147. '$route.path'() {
  148. this.handlePathChange();
  149. },
  150. isFade(val) {
  151. bus.$emit('navFade', val);
  152. }
  153. },
  154. computed: {
  155. navStyle() {
  156. const style = {};
  157. if (this.isSmallScreen) {
  158. style.paddingBottom = '60px';
  159. }
  160. style.opacity = this.isFade ? '0.5' : '1';
  161. return style;
  162. },
  163. langConfig() {
  164. return compoLang.filter(config => config.lang === this.$route.meta.lang)[0]['nav'];
  165. }
  166. },
  167. methods: {
  168. handleResize() {
  169. this.isSmallScreen = document.documentElement.clientWidth < 768;
  170. this.handlePathChange();
  171. },
  172. handlePathChange() {
  173. if (!this.isSmallScreen) {
  174. this.expandAllMenu();
  175. return;
  176. }
  177. this.$nextTick(() => {
  178. this.hideAllMenu();
  179. let activeAnchor = this.$el.querySelector('a.active');
  180. let ul = activeAnchor.parentNode;
  181. while (ul.tagName !== 'UL') {
  182. ul = ul.parentNode;
  183. }
  184. ul.style.height = 'auto';
  185. });
  186. },
  187. hideAllMenu() {
  188. [].forEach.call(this.$el.querySelectorAll('.pure-menu-list'), ul => {
  189. ul.style.height = '0';
  190. });
  191. },
  192. expandAllMenu() {
  193. [].forEach.call(this.$el.querySelectorAll('.pure-menu-list'), ul => {
  194. ul.style.height = 'auto';
  195. });
  196. },
  197. expandMenu(event) {
  198. if (!this.isSmallScreen) return;
  199. let target = event.currentTarget;
  200. if (!target.nextElementSibling || target.nextElementSibling.tagName !== 'UL') return;
  201. this.hideAllMenu();
  202. event.currentTarget.nextElementSibling.style.height = 'auto';
  203. }
  204. },
  205. created() {
  206. bus.$on('fadeNav', () => {
  207. this.isFade = true;
  208. });
  209. },
  210. mounted() {
  211. this.handleResize();
  212. window.addEventListener('resize', this.handleResize);
  213. },
  214. beforeDestroy() {
  215. window.removeEventListener('resize', this.handleResize);
  216. }
  217. };
  218. </script>