side-nav.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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: 42px;
  76. }
  77. }
  78. &:first-child a img {
  79. width: 36px;
  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.duotai.net/?utm_source=element" target="_blank">
  119. <img src="~examples/assets/images/duotai.svg" alt="duotai">
  120. </a>
  121. </li>
  122. <li class="nav-item">
  123. <a class="sponsor" href="https://www.duohui.cn/?utm_source=element&utm_medium=web&utm_campaign=element-index" target="_blank">
  124. <img src="~examples/assets/images/duohui.svg" alt="duohui">
  125. </a>
  126. </li>
  127. </ul>
  128. </li>
  129. <li class="nav-item" v-for="item in data">
  130. <a v-if="!item.path && !item.href" @click="expandMenu">{{item.name}}</a>
  131. <a v-if="item.href" :href="item.href" target="_blank">{{item.name}}</a>
  132. <router-link
  133. v-if="item.path"
  134. active-class="active"
  135. :to="base + item.path"
  136. exact
  137. v-text="item.title || item.name">
  138. </router-link>
  139. <ul class="pure-menu-list sub-nav" v-if="item.children">
  140. <li class="nav-item" v-for="navItem in item.children">
  141. <router-link
  142. class=""
  143. active-class="active"
  144. :to="base + navItem.path"
  145. exact
  146. v-text="navItem.title || navItem.name">
  147. </router-link>
  148. </li>
  149. </ul>
  150. <template v-if="item.groups">
  151. <div class="nav-group" v-for="group in item.groups">
  152. <div class="nav-group__title" @click="expandMenu">{{group.groupName}}</div>
  153. <ul class="pure-menu-list">
  154. <li
  155. class="nav-item"
  156. v-for="navItem in group.list"
  157. v-if="!navItem.disabled">
  158. <router-link
  159. active-class="active"
  160. :to="base + navItem.path"
  161. exact
  162. v-text="navItem.title"></router-link>
  163. </li>
  164. </ul>
  165. </div>
  166. </template>
  167. </li>
  168. </ul>
  169. <!--<div id="code-sponsor-widget"></div>-->
  170. </div>
  171. </template>
  172. <script>
  173. import bus from '../bus';
  174. import compoLang from '../i18n/component.json';
  175. export default {
  176. props: {
  177. data: Array,
  178. base: {
  179. type: String,
  180. default: ''
  181. }
  182. },
  183. data() {
  184. return {
  185. highlights: [],
  186. navState: [],
  187. isSmallScreen: false,
  188. isFade: false
  189. };
  190. },
  191. watch: {
  192. '$route.path'() {
  193. this.handlePathChange();
  194. },
  195. isFade(val) {
  196. bus.$emit('navFade', val);
  197. }
  198. },
  199. computed: {
  200. navStyle() {
  201. const style = {};
  202. if (this.isSmallScreen) {
  203. style.paddingBottom = '60px';
  204. }
  205. style.opacity = this.isFade ? '0.5' : '1';
  206. return style;
  207. },
  208. lang() {
  209. return this.$route.meta.lang;
  210. },
  211. langConfig() {
  212. return compoLang.filter(config => config.lang === this.lang)[0]['nav'];
  213. }
  214. },
  215. methods: {
  216. handleResize() {
  217. this.isSmallScreen = document.documentElement.clientWidth < 768;
  218. this.handlePathChange();
  219. },
  220. handlePathChange() {
  221. if (!this.isSmallScreen) {
  222. this.expandAllMenu();
  223. return;
  224. }
  225. this.$nextTick(() => {
  226. this.hideAllMenu();
  227. let activeAnchor = this.$el.querySelector('a.active');
  228. let ul = activeAnchor.parentNode;
  229. while (ul.tagName !== 'UL') {
  230. ul = ul.parentNode;
  231. }
  232. ul.style.height = 'auto';
  233. });
  234. },
  235. hideAllMenu() {
  236. [].forEach.call(this.$el.querySelectorAll('.pure-menu-list'), ul => {
  237. ul.style.height = '0';
  238. });
  239. },
  240. expandAllMenu() {
  241. [].forEach.call(this.$el.querySelectorAll('.pure-menu-list'), ul => {
  242. ul.style.height = 'auto';
  243. });
  244. },
  245. expandMenu(event) {
  246. if (!this.isSmallScreen) return;
  247. let target = event.currentTarget;
  248. if (!target.nextElementSibling || target.nextElementSibling.tagName !== 'UL') return;
  249. this.hideAllMenu();
  250. event.currentTarget.nextElementSibling.style.height = 'auto';
  251. }
  252. },
  253. created() {
  254. bus.$on('fadeNav', () => {
  255. this.isFade = true;
  256. });
  257. },
  258. mounted() {
  259. this.handleResize();
  260. window.addEventListener('resize', this.handleResize);
  261. },
  262. beforeDestroy() {
  263. window.removeEventListener('resize', this.handleResize);
  264. }
  265. };
  266. </script>