header.vue 11 KB

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