header.vue 11 KB

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