header.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <style scoped>
  2. .headerWrapper {
  3. height: 80px;
  4. }
  5. .header {
  6. height: 80px;
  7. background-color: rgba(32, 160, 255, 1);
  8. color: #fff;
  9. top: 0;
  10. left: 0;
  11. width: 100%;
  12. line-height: @height;
  13. z-index: 100;
  14. position: relative;
  15. .container {
  16. height: 100%;
  17. box-sizing: border-box;
  18. }
  19. h1 {
  20. margin: 0;
  21. float: left;
  22. font-size: 32px;
  23. font-weight: normal;
  24. a {
  25. color: #fff;
  26. text-decoration: none;
  27. display: block;
  28. }
  29. span {
  30. font-size: 12px;
  31. display: inline-block;
  32. width: 34px;
  33. height: 18px;
  34. border: 1px solid rgba(255, 255, 255, .5);
  35. text-align: center;
  36. line-height: 18px;
  37. vertical-align: middle;
  38. margin-left: 10px;
  39. border-radius: 3px;
  40. }
  41. }
  42. .nav {
  43. float: right;
  44. height: 100%;
  45. line-height: 80px;
  46. background: transparent;
  47. @utils-clearfix;
  48. padding: 0;
  49. margin: 0;
  50. }
  51. .nav-logo,
  52. .nav-logo-small {
  53. vertical-align: sub;
  54. }
  55. .nav-logo-small {
  56. display: none;
  57. }
  58. .nav-item {
  59. margin: 0;
  60. float: left;
  61. list-style: none;
  62. position: relative;
  63. cursor: pointer;
  64. margin-left: 20px;
  65. &:last-child {
  66. cursor: default;
  67. margin-left: 34px;
  68. span {
  69. opacity: .8;
  70. }
  71. .nav-lang {
  72. cursor: pointer;
  73. display: inline-block;
  74. height: 100%;
  75. &:hover {
  76. opacity: 1;
  77. }
  78. &.active {
  79. font-weight: 700;
  80. opacity: 1;
  81. }
  82. }
  83. }
  84. a {
  85. text-decoration: none;
  86. color: #fff;
  87. display: block;
  88. padding: 0 20px;
  89. opacity: .8;
  90. &.active,
  91. &:hover {
  92. opacity: 1;
  93. }
  94. &.active {
  95. font-weight: 700;
  96. }
  97. &.active::before {
  98. content: '';
  99. display: block;
  100. position: absolute;
  101. bottom: 0;
  102. left: 0;
  103. width: 100%;
  104. height: 4px;
  105. background:#99d2fc;
  106. }
  107. }
  108. }
  109. }
  110. .header-home {
  111. position: fixed;
  112. top: 0;
  113. background-color: rgba(32, 160, 255, 0);
  114. }
  115. @media (max-width: 850px) {
  116. .header {
  117. .nav-logo {
  118. display: none;
  119. }
  120. .nav-logo-small {
  121. display: inline-block;
  122. }
  123. .nav-item {
  124. margin-left: 6px;
  125. &:last-child {
  126. margin-left: 10px;
  127. }
  128. a {
  129. padding: 0 5px;
  130. }
  131. }
  132. }
  133. }
  134. @media (max-width: 700px) {
  135. .header {
  136. .container {
  137. padding: 0 12px;
  138. }
  139. .nav-item a,
  140. .nav-lang {
  141. font-size: 12px;
  142. vertical-align: top;
  143. }
  144. }
  145. }
  146. </style>
  147. <template>
  148. <div class="headerWrapper">
  149. <header class="header"
  150. ref="header"
  151. :style="headerStyle"
  152. :class="{
  153. 'header-home': isHome
  154. }">
  155. <div class="container">
  156. <h1><router-link :to="`/${ lang }`">
  157. <img
  158. src="../assets/images/element-logo.svg"
  159. alt="element-logo"
  160. class="nav-logo">
  161. <img
  162. src="../assets/images/element-logo-small.svg"
  163. alt="element-logo"
  164. class="nav-logo-small">
  165. </router-link></h1>
  166. <ul class="nav">
  167. <li class="nav-item">
  168. <router-link
  169. active-class="active"
  170. :to="`/${ lang }/guide`">{{ langConfig.guide }}
  171. </router-link>
  172. </li>
  173. <li class="nav-item">
  174. <router-link
  175. active-class="active"
  176. :to="`/${ lang }/component`">{{ langConfig.components }}
  177. </router-link>
  178. </li>
  179. <li class="nav-item">
  180. <router-link
  181. active-class="active"
  182. :to="`/${ lang }/resource`"
  183. exact>{{ langConfig.resource }}
  184. </router-link>
  185. </li>
  186. <li class="nav-item">
  187. <span
  188. class="nav-lang"
  189. :class="{ 'active': lang === 'zh-CN' }"
  190. @click="switchLang('zh-CN')">
  191. 中文
  192. </span>
  193. <span> / </span>
  194. <span
  195. class="nav-lang"
  196. :class="{ 'active': lang === 'en-US' }"
  197. @click="switchLang('en-US')">
  198. En
  199. </span>
  200. </li>
  201. </ul>
  202. </div>
  203. </header>
  204. </div>
  205. </template>
  206. <script>
  207. import compoLang from '../i18n/component.json';
  208. export default {
  209. data() {
  210. return {
  211. active: '',
  212. isHome: false,
  213. headerStyle: {}
  214. };
  215. },
  216. watch: {
  217. '$route.path'() {
  218. this.isHome = /^home/.test(this.$route.name);
  219. this.headerStyle.backgroundColor = `rgba(32, 160, 255, ${ this.isHome ? '0' : '1' })`;
  220. }
  221. },
  222. computed: {
  223. lang() {
  224. return this.$route.path.split('/')[1] || 'zh-CN';
  225. },
  226. langConfig() {
  227. return compoLang.filter(config => config.lang === this.lang)[0]['header'];
  228. }
  229. },
  230. methods: {
  231. switchLang(targetLang) {
  232. if (this.lang === targetLang) return;
  233. localStorage.setItem('ELEMENT_LANGUAGE', targetLang);
  234. this.$router.push(this.$route.path.replace(this.lang, targetLang));
  235. }
  236. },
  237. mounted() {
  238. this.isHome = this.$route.name === 'home';
  239. function scroll(fn) {
  240. window.addEventListener('scroll', () => {
  241. fn();
  242. }, false);
  243. }
  244. scroll(() => {
  245. if (this.isHome) {
  246. const threshold = 200;
  247. let alpha = Math.min(document.body.scrollTop, threshold) / threshold;
  248. this.$refs.header.style.backgroundColor = `rgba(32, 160, 255, ${ alpha })`;
  249. }
  250. });
  251. }
  252. };
  253. </script>