side-nav.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. &.sponsors {
  59. & > .sub-nav {
  60. margin-top: -10px;
  61. }
  62. & > a {
  63. color: #777;
  64. font-weight: 300;
  65. font-size: 14px;
  66. }
  67. .nav-item {
  68. display: inline-block;
  69. a {
  70. height: auto;
  71. display: inline-block;
  72. vertical-align: middle;
  73. margin: 8px 12px 12px 0;
  74. img {
  75. width: 36px;
  76. }
  77. }
  78. &:nth-child(2) a img {
  79. width: 42px;
  80. }
  81. }
  82. }
  83. }
  84. .nav-group__title {
  85. font-size: 12px;
  86. color: #999;
  87. line-height: 26px;
  88. margin-top: 15px;
  89. }
  90. #code-sponsor-widget {
  91. margin: 0 0 0 -20px;
  92. }
  93. }
  94. .nav-dropdown-list {
  95. width: 120px;
  96. margin-top: -8px;
  97. li {
  98. font-size: 14px;
  99. }
  100. }
  101. </style>
  102. <template>
  103. <div
  104. class="side-nav"
  105. @mouseenter="isFade = false"
  106. :class="{ 'is-fade': isFade }"
  107. :style="navStyle">
  108. <ul>
  109. <li class="nav-item sponsors">
  110. <a>{{ lang === 'zh-CN' ? '赞助商' : 'Sponsors' }}</a>
  111. <ul class="pure-menu-list sub-nav">
  112. <li class="nav-item" v-show="lang !== 'zh-CN'">
  113. <a href="https://tipe.io/?ref=element" target="_blank">
  114. <img src="~examples/assets/images/tipe.svg" alt="tipe.io">
  115. </a>
  116. </li>
  117. <li class="nav-item">
  118. <a class="sponsor" href="https://www.duohui.cn/?utm_source=element&utm_medium=web&utm_campaign=element-index" target="_blank">
  119. <img src="~examples/assets/images/duohui.svg" alt="tipe.io">
  120. </a>
  121. </li>
  122. </ul>
  123. </li>
  124. <li class="nav-item" v-for="item in data">
  125. <a v-if="!item.path && !item.href" @click="expandMenu">{{item.name}}</a>
  126. <a v-if="item.href" :href="item.href" target="_blank">{{item.name}}</a>
  127. <router-link
  128. v-if="item.path"
  129. active-class="active"
  130. :to="base + item.path"
  131. exact
  132. v-text="item.title || item.name">
  133. </router-link>
  134. <ul class="pure-menu-list sub-nav" v-if="item.children">
  135. <li class="nav-item" v-for="navItem in item.children">
  136. <router-link
  137. class=""
  138. active-class="active"
  139. :to="base + navItem.path"
  140. exact
  141. v-text="navItem.title || navItem.name">
  142. </router-link>
  143. </li>
  144. </ul>
  145. <template v-if="item.groups">
  146. <div class="nav-group" v-for="group in item.groups">
  147. <div class="nav-group__title" @click="expandMenu">{{group.groupName}}</div>
  148. <ul class="pure-menu-list">
  149. <li
  150. class="nav-item"
  151. v-for="navItem in group.list"
  152. v-if="!navItem.disabled">
  153. <router-link
  154. active-class="active"
  155. :to="base + navItem.path"
  156. exact
  157. v-text="navItem.title"></router-link>
  158. </li>
  159. </ul>
  160. </div>
  161. </template>
  162. </li>
  163. </ul>
  164. <!--<div id="code-sponsor-widget"></div>-->
  165. </div>
  166. </template>
  167. <script>
  168. import bus from '../bus';
  169. import compoLang from '../i18n/component.json';
  170. export default {
  171. props: {
  172. data: Array,
  173. base: {
  174. type: String,
  175. default: ''
  176. }
  177. },
  178. data() {
  179. return {
  180. highlights: [],
  181. navState: [],
  182. isSmallScreen: false,
  183. isFade: false
  184. };
  185. },
  186. watch: {
  187. '$route.path'() {
  188. this.handlePathChange();
  189. },
  190. isFade(val) {
  191. bus.$emit('navFade', val);
  192. }
  193. },
  194. computed: {
  195. navStyle() {
  196. const style = {};
  197. if (this.isSmallScreen) {
  198. style.paddingBottom = '60px';
  199. }
  200. style.opacity = this.isFade ? '0.5' : '1';
  201. return style;
  202. },
  203. lang() {
  204. return this.$route.meta.lang;
  205. },
  206. langConfig() {
  207. return compoLang.filter(config => config.lang === this.lang)[0]['nav'];
  208. }
  209. },
  210. methods: {
  211. handleResize() {
  212. this.isSmallScreen = document.documentElement.clientWidth < 768;
  213. this.handlePathChange();
  214. },
  215. handlePathChange() {
  216. if (!this.isSmallScreen) {
  217. this.expandAllMenu();
  218. return;
  219. }
  220. this.$nextTick(() => {
  221. this.hideAllMenu();
  222. let activeAnchor = this.$el.querySelector('a.active');
  223. let ul = activeAnchor.parentNode;
  224. while (ul.tagName !== 'UL') {
  225. ul = ul.parentNode;
  226. }
  227. ul.style.height = 'auto';
  228. });
  229. },
  230. hideAllMenu() {
  231. [].forEach.call(this.$el.querySelectorAll('.pure-menu-list'), ul => {
  232. ul.style.height = '0';
  233. });
  234. },
  235. expandAllMenu() {
  236. [].forEach.call(this.$el.querySelectorAll('.pure-menu-list'), ul => {
  237. ul.style.height = 'auto';
  238. });
  239. },
  240. expandMenu(event) {
  241. if (!this.isSmallScreen) return;
  242. let target = event.currentTarget;
  243. if (!target.nextElementSibling || target.nextElementSibling.tagName !== 'UL') return;
  244. this.hideAllMenu();
  245. event.currentTarget.nextElementSibling.style.height = 'auto';
  246. }
  247. },
  248. created() {
  249. bus.$on('fadeNav', () => {
  250. this.isFade = true;
  251. });
  252. },
  253. mounted() {
  254. this.handleResize();
  255. window.addEventListener('resize', this.handleResize);
  256. },
  257. beforeDestroy() {
  258. window.removeEventListener('resize', this.handleResize);
  259. }
  260. };
  261. </script>