header.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. <router-link
  278. active-class="active"
  279. :to="`/${ lang }/guide`">{{ langConfig.guide }}
  280. </router-link>
  281. </li>
  282. <li class="nav-item">
  283. <router-link
  284. active-class="active"
  285. :to="`/${ lang }/component`">{{ langConfig.components }}
  286. </router-link>
  287. </li>
  288. <li class="nav-item">
  289. <router-link
  290. active-class="active"
  291. :to="`/${ lang }/resource`"
  292. exact>{{ langConfig.resource }}
  293. </router-link>
  294. </li>
  295. <!-- gap -->
  296. <li class="nav-item" v-show="isComponentPage">
  297. <div class="nav-gap"></div>
  298. </li>
  299. <!-- 版本选择器 -->
  300. <li class="nav-item" v-show="isComponentPage">
  301. <el-dropdown
  302. trigger="click"
  303. class="nav-dropdown"
  304. :class="{ 'is-active': dropdownVisible }">
  305. <span>
  306. {{ langConfig.dropdown }}{{ version }}
  307. <i class="el-icon-arrow-down el-icon--right"></i>
  308. </span>
  309. <el-dropdown-menu
  310. slot="dropdown"
  311. class="nav-dropdown-list"
  312. @input="handleDropdownToggle">
  313. <el-dropdown-item
  314. v-for="item in Object.keys(versions)"
  315. :key="item"
  316. @click.native="switchVersion(item)">
  317. {{ item }}
  318. </el-dropdown-item>
  319. </el-dropdown-menu>
  320. </el-dropdown>
  321. </li>
  322. <!-- lang -->
  323. <li class="nav-item lang-item">
  324. <span
  325. class="nav-lang"
  326. :class="{ 'active': lang === 'zh-CN' }"
  327. @click="switchLang('zh-CN')">
  328. 中文
  329. </span>
  330. <span class="nav-lang-spe"> / </span>
  331. <span
  332. class="nav-lang"
  333. :class="{ 'active': lang === 'en-US' }"
  334. @click="switchLang('en-US')">
  335. En
  336. </span>
  337. </li>
  338. <!--theme picker-->
  339. <li class="nav-item" v-show="isComponentPage">
  340. <theme-picker></theme-picker>
  341. </li>
  342. </ul>
  343. </div>
  344. </header>
  345. </div>
  346. </template>
  347. <script>
  348. import ThemePicker from './theme-picker.vue';
  349. import bus from '../bus';
  350. import compoLang from '../i18n/component.json';
  351. import { version } from 'main/index.js';
  352. export default {
  353. data() {
  354. return {
  355. active: '',
  356. isHome: true,
  357. headerStyle: {},
  358. visible: true,
  359. versions: [],
  360. version,
  361. dropdownVisible: true,
  362. isComponentPage: true
  363. };
  364. },
  365. components: { ThemePicker },
  366. watch: {
  367. '$route.path': {
  368. immediate: true,
  369. handler() {
  370. this.handlePathChange();
  371. }
  372. }
  373. },
  374. computed: {
  375. lang() {
  376. return this.$route.path.split('/')[1] || 'zh-CN';
  377. },
  378. langConfig() {
  379. return compoLang.filter(config => config.lang === this.lang)[0]['header'];
  380. }
  381. },
  382. methods: {
  383. switchVersion(version) {
  384. if (version === this.version) return;
  385. location.href = `${ location.origin }/${ this.versions[version] }/${ location.hash } `;
  386. },
  387. switchLang(targetLang) {
  388. if (this.lang === targetLang) return;
  389. localStorage.setItem('ELEMENT_LANGUAGE', targetLang);
  390. this.$router.push(this.$route.path.replace(this.lang, targetLang));
  391. },
  392. handleDropdownToggle(visible) {
  393. this.dropdownVisible = visible;
  394. },
  395. handlePathChange() {
  396. const routerName = this.$route.name;
  397. this.isComponentPage = /^component-/.test(routerName);
  398. this.isHome = /^home/.test(routerName);
  399. if (this.isComponentPage) {
  400. this.headerStyle.backgroundColor = '#fff';
  401. return;
  402. }
  403. this.headerStyle.backgroundColor = `rgba(32, 160, 255, ${ this.isHome ? '0' : '1' })`;
  404. }
  405. },
  406. created() {
  407. this.handlePathChange();
  408. bus.$on('toggleHeader', visible => {
  409. this.visible = visible;
  410. });
  411. const xhr = new XMLHttpRequest();
  412. xhr.onreadystatechange = _ => {
  413. if (xhr.readyState === 4 && xhr.status === 200) {
  414. this.versions = JSON.parse(xhr.responseText);
  415. }
  416. };
  417. xhr.open('GET', '/versions.json');
  418. xhr.send();
  419. },
  420. mounted() {
  421. function scroll(fn) {
  422. window.addEventListener('scroll', fn, false);
  423. }
  424. scroll(() => {
  425. if (this.isHome) {
  426. const threshold = 200;
  427. let alpha = Math.min((document.documentElement.scrollTop || document.body.scrollTop), threshold) / threshold;
  428. this.$refs.header.style.backgroundColor = `rgba(32, 160, 255, ${ alpha })`;
  429. }
  430. });
  431. }
  432. };
  433. </script>