header.vue 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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: #409EFF;
  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: #409EFF;
  186. }
  187. i {
  188. transform: rotateZ(180deg) translateY(2px) scale(.6);
  189. }
  190. }
  191. &:hover {
  192. span, i {
  193. color: #409EFF;
  194. }
  195. }
  196. }
  197. .nav-dropdown-list {
  198. width: auto;
  199. }
  200. @media (max-width: 850px) {
  201. .header {
  202. .nav-logo {
  203. display: none;
  204. }
  205. .nav-logo-small {
  206. display: inline-block;
  207. }
  208. .nav-item {
  209. margin-left: 6px;
  210. &:last-child {
  211. margin-left: 10px;
  212. }
  213. a {
  214. padding: 0 5px;
  215. }
  216. }
  217. }
  218. }
  219. @media (max-width: 700px) {
  220. .header {
  221. .container {
  222. padding: 0 12px;
  223. }
  224. .nav-item a,
  225. .nav-lang {
  226. font-size: 12px;
  227. vertical-align: top;
  228. }
  229. }
  230. }
  231. </style>
  232. <template>
  233. <div class="headerWrapper">
  234. <header class="header"
  235. ref="header"
  236. :style="headerStyle"
  237. :class="{
  238. 'header-home': isHome,
  239. 'header-light': isComponentPage
  240. }">
  241. <div class="container">
  242. <h1><router-link :to="`/${ lang }`">
  243. <!-- logo -->
  244. <slot v-if="isComponentPage">
  245. <img
  246. src="../assets/images/element-logo.svg"
  247. alt="element-logo"
  248. class="nav-logo">
  249. <img
  250. src="../assets/images/element-logo-small.svg"
  251. alt="element-logo"
  252. class="nav-logo-small">
  253. </slot>
  254. <slot v-else>
  255. <img
  256. src="../assets/images/element-logo-white.svg"
  257. alt="element-logo"
  258. class="nav-logo">
  259. <img
  260. src="../assets/images/element-logo-small-white.svg"
  261. alt="element-logo"
  262. class="nav-logo-small">
  263. </slot>
  264. </router-link></h1>
  265. <!-- nav -->
  266. <ul class="nav">
  267. <li class="nav-item">
  268. <router-link
  269. active-class="active"
  270. :to="`/${ lang }/guide`">{{ langConfig.guide }}
  271. </router-link>
  272. </li>
  273. <li class="nav-item">
  274. <router-link
  275. active-class="active"
  276. :to="`/${ lang }/component`">{{ langConfig.components }}
  277. </router-link>
  278. </li>
  279. <li class="nav-item">
  280. <router-link
  281. active-class="active"
  282. :to="`/${ lang }/resource`"
  283. exact>{{ langConfig.resource }}
  284. </router-link>
  285. </li>
  286. <!-- gap -->
  287. <li class="nav-item" v-show="isComponentPage">
  288. <div class="nav-gap"></div>
  289. </li>
  290. <!-- 版本选择器 -->
  291. <li class="nav-item" v-show="isComponentPage">
  292. <el-dropdown
  293. trigger="click"
  294. class="nav-dropdown"
  295. :class="{ 'is-active': dropdownVisible }">
  296. <span>
  297. {{ langConfig.dropdown }}{{ version }}
  298. <i class="el-icon-arrow-down el-icon--right"></i>
  299. </span>
  300. <el-dropdown-menu
  301. slot="dropdown"
  302. class="nav-dropdown-list"
  303. @input="handleDropdownToggle">
  304. <el-dropdown-item
  305. v-for="item in Object.keys(versions)"
  306. :key="item"
  307. @click.native="switchVersion(item)">
  308. {{ item }}
  309. </el-dropdown-item>
  310. </el-dropdown-menu>
  311. </el-dropdown>
  312. </li>
  313. <!-- lang -->
  314. <li class="nav-item">
  315. <span
  316. class="nav-lang"
  317. :class="{ 'active': lang === 'zh-CN' }"
  318. @click="switchLang('zh-CN')">
  319. 中文
  320. </span>
  321. <span class="nav-lang-spe"> / </span>
  322. <span
  323. class="nav-lang"
  324. :class="{ 'active': lang === 'en-US' }"
  325. @click="switchLang('en-US')">
  326. En
  327. </span>
  328. </li>
  329. </ul>
  330. </div>
  331. </header>
  332. </div>
  333. </template>
  334. <script>
  335. import bus from '../bus';
  336. import compoLang from '../i18n/component.json';
  337. import { version } from 'main/index.js';
  338. export default {
  339. data() {
  340. return {
  341. active: '',
  342. isHome: true,
  343. headerStyle: {},
  344. visible: true,
  345. versions: [],
  346. version,
  347. dropdownVisible: true,
  348. isComponentPage: true
  349. };
  350. },
  351. watch: {
  352. '$route.path': {
  353. immediate: true,
  354. handler() {
  355. this.handlePathChange();
  356. }
  357. }
  358. },
  359. computed: {
  360. lang() {
  361. return this.$route.path.split('/')[1] || 'zh-CN';
  362. },
  363. langConfig() {
  364. return compoLang.filter(config => config.lang === this.lang)[0]['header'];
  365. }
  366. },
  367. methods: {
  368. switchVersion(version) {
  369. if (version === this.version) return;
  370. location.href = `${ location.origin }/${ this.versions[version] }/${ location.hash } `;
  371. },
  372. switchLang(targetLang) {
  373. if (this.lang === targetLang) return;
  374. localStorage.setItem('ELEMENT_LANGUAGE', targetLang);
  375. this.$router.push(this.$route.path.replace(this.lang, targetLang));
  376. },
  377. handleDropdownToggle(visible) {
  378. this.dropdownVisible = visible;
  379. },
  380. handlePathChange() {
  381. const routerName = this.$route.name;
  382. this.isComponentPage = /^component-/.test(routerName);
  383. this.isHome = /^home/.test(routerName);
  384. if (this.isComponentPage) {
  385. this.headerStyle.backgroundColor = '#fff';
  386. return;
  387. }
  388. this.headerStyle.backgroundColor = `rgba(32, 160, 255, ${ this.isHome ? '0' : '1' })`;
  389. }
  390. },
  391. created() {
  392. this.handlePathChange();
  393. bus.$on('toggleHeader', visible => {
  394. this.visible = visible;
  395. });
  396. const xhr = new XMLHttpRequest();
  397. xhr.onreadystatechange = _ => {
  398. if (xhr.readyState === 4 && xhr.status === 200) {
  399. this.versions = JSON.parse(xhr.responseText);
  400. }
  401. };
  402. xhr.open('GET', '/versions.json');
  403. xhr.send();
  404. },
  405. mounted() {
  406. function scroll(fn) {
  407. document.body.addEventListener('scroll', fn, false);
  408. }
  409. scroll(() => {
  410. if (this.isHome) {
  411. const threshold = 200;
  412. let alpha = Math.min((document.documentElement.scrollTop || document.body.scrollTop), threshold) / threshold;
  413. this.$refs.header.style.backgroundColor = `rgba(32, 160, 255, ${ alpha })`;
  414. }
  415. });
  416. }
  417. };
  418. </script>