header.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. a {
  61. text-decoration: none;
  62. color: #fff;
  63. display: block;
  64. padding: 0 20px;
  65. opacity: .8;
  66. &.active,
  67. &:hover {
  68. opacity: 1;
  69. }
  70. &.active::before {
  71. content: '';
  72. display: block;
  73. position: absolute;
  74. bottom: 0;
  75. left: 0;
  76. width: 100%;
  77. height: 4px;
  78. background:#99d2fc;
  79. }
  80. }
  81. }
  82. }
  83. .header-home {
  84. position: fixed;
  85. top: 0;
  86. background-color: rgba(32, 160, 255, 0);
  87. }
  88. </style>
  89. <template>
  90. <div class="headerWrapper">
  91. <header class="header"
  92. ref="header"
  93. :style="headerStyle"
  94. :class="{
  95. 'header-home': isHome
  96. }">
  97. <div class="container">
  98. <h1><router-link to="/">
  99. <img
  100. src="../assets/images/element-logo.svg"
  101. alt="element-logo"
  102. class="nav-logo">
  103. </router-link></h1>
  104. <ul class="nav">
  105. <li class="nav-item">
  106. <router-link
  107. active-class="active"
  108. to="/guide">指南
  109. </router-link>
  110. </li>
  111. <li class="nav-item">
  112. <router-link
  113. active-class="active"
  114. to="/component">组件
  115. </router-link>
  116. </li>
  117. <li class="nav-item">
  118. <router-link
  119. active-class="active"
  120. to="/resource"
  121. exact>资源
  122. </router-link>
  123. </li>
  124. </ul>
  125. </div>
  126. </header>
  127. </div>
  128. </template>
  129. <script>
  130. export default {
  131. data() {
  132. return {
  133. active: '',
  134. isHome: false,
  135. headerStyle: {}
  136. };
  137. },
  138. watch: {
  139. '$route.path'(val) {
  140. this.isHome = val === '/';
  141. this.headerStyle.backgroundColor = `rgba(32, 160, 255, ${ this.isHome ? '0' : '1' })`;
  142. }
  143. },
  144. mounted() {
  145. this.isHome = this.$route.path === '/';
  146. function scroll(fn) {
  147. window.addEventListener('scroll', () => {
  148. fn();
  149. }, false);
  150. }
  151. scroll(() => {
  152. if (this.isHome) {
  153. const threshold = 200;
  154. let alpha = Math.min(document.body.scrollTop, threshold) / threshold;
  155. this.$refs.header.style.backgroundColor = `rgba(32, 160, 255, ${ alpha })`;
  156. }
  157. });
  158. }
  159. };
  160. </script>