header.vue 3.3 KB

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