header.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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-logo,
  56. .nav-logo-small {
  57. vertical-align: sub;
  58. }
  59. .nav-logo-small {
  60. display: none;
  61. }
  62. .nav-item {
  63. margin: 0;
  64. float: left;
  65. list-style: none;
  66. position: relative;
  67. cursor: pointer;
  68. margin-left: 20px;
  69. &:last-child {
  70. cursor: default;
  71. margin-left: 34px;
  72. span {
  73. opacity: .8;
  74. }
  75. .nav-lang {
  76. cursor: pointer;
  77. display: inline-block;
  78. height: 100%;
  79. &:hover {
  80. opacity: 1;
  81. }
  82. &.active {
  83. font-weight: 700;
  84. opacity: 1;
  85. }
  86. }
  87. }
  88. a {
  89. text-decoration: none;
  90. color: #333;
  91. display: block;
  92. padding: 0 20px;
  93. opacity: .8;
  94. &.active,
  95. &:hover {
  96. opacity: 1;
  97. }
  98. &.active {
  99. font-weight: 700;
  100. }
  101. &.active::before {
  102. content: '';
  103. display: block;
  104. position: absolute;
  105. bottom: 0;
  106. left: 0;
  107. width: 100%;
  108. height: 4px;
  109. background:#99d2fc;
  110. }
  111. }
  112. }
  113. }
  114. .header-home {
  115. position: fixed;
  116. top: 0;
  117. background-color: rgba(32, 160, 255, 0);
  118. }
  119. @media (max-width: 850px) {
  120. .header {
  121. .nav-logo {
  122. display: none;
  123. }
  124. .nav-logo-small {
  125. display: inline-block;
  126. }
  127. .nav-item {
  128. margin-left: 6px;
  129. &:last-child {
  130. margin-left: 10px;
  131. }
  132. a {
  133. padding: 0 5px;
  134. }
  135. }
  136. }
  137. }
  138. @media (max-width: 700px) {
  139. .header {
  140. .container {
  141. padding: 0 12px;
  142. }
  143. .nav-item a,
  144. .nav-lang {
  145. font-size: 12px;
  146. vertical-align: top;
  147. }
  148. }
  149. }
  150. </style>
  151. <template>
  152. <div
  153. class="headerWrapper"
  154. :class="{ 'is-hidden': !visible }">
  155. <header class="header"
  156. ref="header"
  157. :style="headerStyle"
  158. :class="{
  159. 'header-home': isHome
  160. }">
  161. <div class="container">
  162. <h1><router-link :to="`/${ lang }`">
  163. <img
  164. src="../assets/images/element-logo.svg"
  165. alt="element-logo"
  166. class="nav-logo">
  167. <img
  168. src="../assets/images/element-logo-small.svg"
  169. alt="element-logo"
  170. class="nav-logo-small">
  171. </router-link></h1>
  172. <ul class="nav">
  173. <li class="nav-item">
  174. <router-link
  175. active-class="active"
  176. :to="`/${ lang }/guide`">{{ langConfig.guide }}
  177. </router-link>
  178. </li>
  179. <li class="nav-item">
  180. <router-link
  181. active-class="active"
  182. :to="`/${ lang }/component`">{{ langConfig.components }}
  183. </router-link>
  184. </li>
  185. <li class="nav-item">
  186. <router-link
  187. active-class="active"
  188. :to="`/${ lang }/resource`"
  189. exact>{{ langConfig.resource }}
  190. </router-link>
  191. </li>
  192. <li class="nav-item">
  193. <span
  194. class="nav-lang"
  195. :class="{ 'active': lang === 'zh-CN' }"
  196. @click="switchLang('zh-CN')">
  197. 中文
  198. </span>
  199. <span> / </span>
  200. <span
  201. class="nav-lang"
  202. :class="{ 'active': lang === 'en-US' }"
  203. @click="switchLang('en-US')">
  204. En
  205. </span>
  206. </li>
  207. </ul>
  208. </div>
  209. </header>
  210. </div>
  211. </template>
  212. <script>
  213. import bus from '../bus';
  214. import compoLang from '../i18n/component.json';
  215. export default {
  216. data() {
  217. return {
  218. active: '',
  219. isHome: false,
  220. headerStyle: {},
  221. visible: true
  222. };
  223. },
  224. watch: {
  225. '$route.path': {
  226. immediate: true,
  227. handler() {
  228. this.isHome = /^home/.test(this.$route.name);
  229. if (/^component/.test(this.$route.name)) {
  230. this.headerStyle.backgroundColor = '#fff';
  231. return;
  232. }
  233. this.headerStyle.backgroundColor = `rgba(32, 160, 255, ${ this.isHome ? '0' : '1' })`;
  234. }
  235. }
  236. },
  237. computed: {
  238. lang() {
  239. return this.$route.path.split('/')[1] || 'zh-CN';
  240. },
  241. langConfig() {
  242. return compoLang.filter(config => config.lang === this.lang)[0]['header'];
  243. }
  244. },
  245. methods: {
  246. switchLang(targetLang) {
  247. if (this.lang === targetLang) return;
  248. localStorage.setItem('ELEMENT_LANGUAGE', targetLang);
  249. this.$router.push(this.$route.path.replace(this.lang, targetLang));
  250. }
  251. },
  252. created() {
  253. bus.$on('toggleHeader', visible => {
  254. this.visible = visible;
  255. });
  256. },
  257. mounted() {
  258. function scroll(fn) {
  259. window.addEventListener('scroll', fn, false);
  260. }
  261. scroll(() => {
  262. if (this.isHome) {
  263. const threshold = 200;
  264. let alpha = Math.min((document.documentElement.scrollTop || document.body.scrollTop), threshold) / threshold;
  265. this.$refs.header.style.backgroundColor = `rgba(32, 160, 255, ${ alpha })`;
  266. }
  267. });
  268. }
  269. };
  270. </script>