header.vue 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. <style scoped>
  2. .headerWrapper {
  3. height: 80px;
  4. transition: transform .3s;
  5. &.is-hidden {
  6. transform: translateY(-80px);
  7. }
  8. }
  9. .header {
  10. height: 80px;
  11. background-color: rgba(32, 160, 255, 1);
  12. color: #fff;
  13. top: 0;
  14. left: 0;
  15. width: 100%;
  16. line-height: @height;
  17. z-index: 100;
  18. position: relative;
  19. .container {
  20. height: 100%;
  21. box-sizing: border-box;
  22. }
  23. h1 {
  24. margin: 0;
  25. float: left;
  26. font-size: 32px;
  27. font-weight: normal;
  28. a {
  29. color: #333;
  30. text-decoration: none;
  31. display: block;
  32. }
  33. span {
  34. font-size: 12px;
  35. display: inline-block;
  36. width: 34px;
  37. height: 18px;
  38. border: 1px solid rgba(255, 255, 255, .5);
  39. text-align: center;
  40. line-height: 18px;
  41. vertical-align: middle;
  42. margin-left: 10px;
  43. border-radius: 3px;
  44. }
  45. }
  46. .nav {
  47. float: right;
  48. height: 100%;
  49. line-height: 80px;
  50. background: transparent;
  51. @utils-clearfix;
  52. padding: 0;
  53. margin: 0;
  54. }
  55. .nav-gap {
  56. position: relative;
  57. width: 1px;
  58. height: 80px;
  59. padding: 0 20px;
  60. &::before {
  61. content: '';
  62. position: absolute;
  63. top: calc(50% - 8px);
  64. width: 1px;
  65. height: 16px;
  66. background: #ebebeb;
  67. }
  68. }
  69. .nav-logo,
  70. .nav-logo-small {
  71. vertical-align: sub;
  72. }
  73. .nav-logo-small {
  74. display: none;
  75. }
  76. .nav-item {
  77. margin: 0;
  78. float: left;
  79. list-style: none;
  80. position: relative;
  81. cursor: pointer;
  82. &:last-child {
  83. cursor: default;
  84. margin-left: 34px;
  85. span {
  86. opacity: .8;
  87. }
  88. .nav-lang {
  89. cursor: pointer;
  90. display: inline-block;
  91. height: 100%;
  92. &:hover {
  93. opacity: 1;
  94. }
  95. &.active {
  96. font-weight: 700;
  97. opacity: 1;
  98. }
  99. }
  100. }
  101. a {
  102. text-decoration: none;
  103. color: #fff;
  104. display: block;
  105. padding: 0 22px;
  106. opacity: .8;
  107. &.active,
  108. &:hover {
  109. opacity: 1;
  110. }
  111. &.active::after {
  112. content: '';
  113. display: inline-block;
  114. position: absolute;
  115. bottom: 0;
  116. left: 0;
  117. width: 100%;
  118. height: 4px;
  119. background:#99d2fc;
  120. }
  121. }
  122. }
  123. }
  124. .header-home {
  125. position: fixed;
  126. top: 0;
  127. background-color: rgba(32, 160, 255, 0);
  128. }
  129. .header-light {
  130. background-color: #fff;
  131. .nav-lang {
  132. color: #888;
  133. &:hover,
  134. &.acive {
  135. font-weight: normal;
  136. opacity: 1;
  137. color: #1989fa;
  138. }
  139. }
  140. .nav-lang-spe {
  141. color: #888;
  142. }
  143. .nav-item {
  144. a {
  145. color: #888;
  146. opacity: 1;
  147. }
  148. a:hover,
  149. a.active {
  150. color: #333;
  151. }
  152. a.active::after {
  153. width: 14px;
  154. left: calc(50% - 7px);
  155. bottom: 15px;
  156. background: #1989fa;
  157. }
  158. }
  159. }
  160. .nav-dropdown {
  161. margin-bottom: 6px;
  162. padding-left: 18px;
  163. width: 100%;
  164. span {
  165. display: block;
  166. width: 100%;
  167. font-size: 16px;
  168. color: #888;
  169. line-height: 40px;
  170. transition: .2s;
  171. padding-bottom: 6px;
  172. user-select: none;
  173. &:hover {
  174. cursor: pointer;
  175. }
  176. }
  177. i {
  178. transition: .2s;
  179. transform: scale(.6);
  180. font-size: 12px;
  181. color: #979797;
  182. }
  183. @when active {
  184. span, i {
  185. color: #1989FA;
  186. }
  187. i {
  188. transform: rotateZ(180deg) translateY(2px) scale(.6);
  189. }
  190. }
  191. &:hover {
  192. span, i {
  193. color: #1989FA;
  194. }
  195. }
  196. }
  197. @media (max-width: 850px) {
  198. .header {
  199. .nav-logo {
  200. display: none;
  201. }
  202. .nav-logo-small {
  203. display: inline-block;
  204. }
  205. .nav-item {
  206. margin-left: 6px;
  207. &:last-child {
  208. margin-left: 10px;
  209. }
  210. a {
  211. padding: 0 5px;
  212. }
  213. }
  214. }
  215. }
  216. @media (max-width: 700px) {
  217. .header {
  218. .container {
  219. padding: 0 12px;
  220. }
  221. .nav-item a,
  222. .nav-lang {
  223. font-size: 12px;
  224. vertical-align: top;
  225. }
  226. }
  227. }
  228. </style>
  229. <template>
  230. <div
  231. class="headerWrapper"
  232. :class="{ 'is-hidden': !visible }">
  233. <header class="header"
  234. ref="header"
  235. :style="headerStyle"
  236. :class="{
  237. 'header-home': isHome,
  238. 'header-light': isComponentPage
  239. }">
  240. <div class="container">
  241. <h1><router-link :to="`/${ lang }`">
  242. <!-- logo -->
  243. <slot v-if="isComponentPage">
  244. <img
  245. src="../assets/images/element-logo.svg"
  246. alt="element-logo"
  247. class="nav-logo">
  248. <img
  249. src="../assets/images/element-logo-small.svg"
  250. alt="element-logo"
  251. class="nav-logo-small">
  252. </slot>
  253. <slot v-else>
  254. <img
  255. src="../assets/images/element-logo-white.svg"
  256. alt="element-logo"
  257. class="nav-logo">
  258. <img
  259. src="../assets/images/element-logo-small-white.svg"
  260. alt="element-logo"
  261. class="nav-logo-small">
  262. </slot>
  263. </router-link></h1>
  264. <!-- nav -->
  265. <ul class="nav">
  266. <li class="nav-item">
  267. <router-link
  268. active-class="active"
  269. :to="`/${ lang }/guide`">{{ langConfig.guide }}
  270. </router-link>
  271. </li>
  272. <li class="nav-item">
  273. <router-link
  274. active-class="active"
  275. :to="`/${ lang }/component`">{{ langConfig.components }}
  276. </router-link>
  277. </li>
  278. <li class="nav-item">
  279. <router-link
  280. active-class="active"
  281. :to="`/${ lang }/resource`"
  282. exact>{{ langConfig.resource }}
  283. </router-link>
  284. </li>
  285. <!-- gap -->
  286. <li class="nav-item" v-show="isComponentPage">
  287. <div class="nav-gap"></div>
  288. </li>
  289. <!-- 版本选择器 -->
  290. <li class="nav-item" v-show="isComponentPage">
  291. <el-dropdown
  292. trigger="click"
  293. class="nav-dropdown"
  294. :class="{ 'is-active': dropdownVisible }">
  295. <span>
  296. {{ langConfig.dropdown }}{{ version }}
  297. <i class="el-icon-arrow-down el-icon--right"></i>
  298. </span>
  299. <el-dropdown-menu
  300. slot="dropdown"
  301. class="nav-dropdown-list"
  302. @input="handleDropdownToggle">
  303. <el-dropdown-item
  304. v-for="item in Object.keys(versions)"
  305. :key="item"
  306. @click.native="switchVersion(item)">
  307. {{ item }}
  308. </el-dropdown-item>
  309. </el-dropdown-menu>
  310. </el-dropdown>
  311. </li>
  312. <!-- lang -->
  313. <li class="nav-item">
  314. <span
  315. class="nav-lang"
  316. :class="{ 'active': lang === 'zh-CN' }"
  317. @click="switchLang('zh-CN')">
  318. 中文
  319. </span>
  320. <span class="nav-lang-spe"> / </span>
  321. <span
  322. class="nav-lang"
  323. :class="{ 'active': lang === 'en-US' }"
  324. @click="switchLang('en-US')">
  325. En
  326. </span>
  327. </li>
  328. </ul>
  329. </div>
  330. </header>
  331. </div>
  332. </template>
  333. <script>
  334. import bus from '../bus';
  335. import compoLang from '../i18n/component.json';
  336. import { version } from 'main/index.js';
  337. export default {
  338. data() {
  339. return {
  340. active: '',
  341. isHome: true,
  342. headerStyle: {},
  343. visible: true,
  344. versions: [],
  345. version,
  346. dropdownVisible: true,
  347. isComponentPage: true
  348. };
  349. },
  350. watch: {
  351. '$route.path': {
  352. immediate: true,
  353. handler() {
  354. this.handlePathChange();
  355. }
  356. }
  357. },
  358. computed: {
  359. lang() {
  360. return this.$route.path.split('/')[1] || 'zh-CN';
  361. },
  362. langConfig() {
  363. return compoLang.filter(config => config.lang === this.lang)[0]['header'];
  364. }
  365. },
  366. methods: {
  367. switchVersion(version) {
  368. if (version === this.version) return;
  369. location.href = `${ location.origin }/${ this.versions[version] }/${ location.hash } `;
  370. },
  371. switchLang(targetLang) {
  372. if (this.lang === targetLang) return;
  373. localStorage.setItem('ELEMENT_LANGUAGE', targetLang);
  374. this.$router.push(this.$route.path.replace(this.lang, targetLang));
  375. },
  376. handleDropdownToggle(visible) {
  377. this.dropdownVisible = visible;
  378. },
  379. handlePathChange() {
  380. const routerName = this.$route.name;
  381. this.isComponentPage = /^component-/.test(routerName);
  382. this.isHome = /^home/.test(routerName);
  383. if (this.isComponentPage) {
  384. this.headerStyle.backgroundColor = '#fff';
  385. return;
  386. }
  387. this.headerStyle.backgroundColor = `rgba(32, 160, 255, ${ this.isHome ? '0' : '1' })`;
  388. }
  389. },
  390. created() {
  391. this.handlePathChange();
  392. bus.$on('toggleHeader', visible => {
  393. this.visible = visible;
  394. });
  395. const xhr = new XMLHttpRequest();
  396. xhr.onreadystatechange = _ => {
  397. if (xhr.readyState === 4 && xhr.status === 200) {
  398. this.versions = JSON.parse(xhr.responseText);
  399. }
  400. };
  401. xhr.open('GET', '/versions.json');
  402. xhr.send();
  403. },
  404. mounted() {
  405. function scroll(fn) {
  406. window.addEventListener('scroll', fn, false);
  407. }
  408. scroll(() => {
  409. if (this.isHome) {
  410. const threshold = 200;
  411. let alpha = Math.min((document.documentElement.scrollTop || document.body.scrollTop), threshold) / threshold;
  412. this.$refs.header.style.backgroundColor = `rgba(32, 160, 255, ${ alpha })`;
  413. }
  414. });
  415. }
  416. };
  417. </script>