side-nav.vue 7.1 KB

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