header.vue 4.9 KB

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