header.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. &.active:before {
  63. content: '';
  64. display: block;
  65. position: absolute;
  66. bottom: 0;
  67. left: 0;
  68. width: 100%;
  69. height: 4px;
  70. background:#99d2fc;
  71. }
  72. }
  73. }
  74. }
  75. .header-fixed {
  76. position: fixed;
  77. top: -80px;
  78. box-shadow: 0 2px 6px 0 rgba(50, 63, 87, 0.25);
  79. }
  80. .header-hangUp {
  81. top: 0;
  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. 'header-fixed': isFixed,
  97. 'header-hangUp': hangUp
  98. }">
  99. <div class="container">
  100. <h1><router-link to="/">Element<span>Beta</span></router-link></h1>
  101. <ul class="nav">
  102. <li class="nav-item">
  103. <router-link
  104. active-class="active"
  105. to="/guide">指南
  106. </router-link>
  107. </li>
  108. <li class="nav-item">
  109. <router-link
  110. active-class="active"
  111. to="/component">组件
  112. </router-link>
  113. </li>
  114. <li class="nav-item">
  115. <router-link
  116. active-class="active"
  117. to="/resource"
  118. exact>资源
  119. </router-link>
  120. </li>
  121. </ul>
  122. </div>
  123. </header>
  124. </div>
  125. </template>
  126. <script>
  127. export default {
  128. data() {
  129. return {
  130. active: '',
  131. isFixed: false,
  132. isHome: false,
  133. headerStyle: {},
  134. hangUp: false
  135. };
  136. },
  137. watch: {
  138. '$route.path'(val) {
  139. this.isHome = val === '/';
  140. this.headerStyle.backgroundColor = `rgba(32, 160, 255, ${ this.isHome ? '0' : '1' })`;
  141. }
  142. },
  143. mounted() {
  144. this.isHome = this.$route.path === '/';
  145. function scroll(fn) {
  146. var beforeScrollTop = document.body.scrollTop;
  147. window.addEventListener('scroll', () => {
  148. const afterScrollTop = document.body.scrollTop;
  149. var delta = afterScrollTop - beforeScrollTop;
  150. if (delta === 0) return false;
  151. fn(delta > 0 ? 'down' : 'up');
  152. beforeScrollTop = afterScrollTop;
  153. }, false);
  154. }
  155. scroll((direction) => {
  156. if (this.isHome) {
  157. this.hangUp = false;
  158. this.isFixed = false;
  159. this.headerStyle.transition = '';
  160. const threshold = 200;
  161. let alpha = Math.min(document.body.scrollTop, threshold) / threshold;
  162. this.$refs.header.style.backgroundColor = `rgba(32, 160, 255, ${ alpha })`;
  163. return;
  164. }
  165. this.headerStyle.backgroundColor = 'rgba(32, 160, 255, 1)';
  166. const bounding = this.$el.getBoundingClientRect();
  167. if (bounding.bottom < 0) {
  168. this.isFixed = true;
  169. this.$nextTick(() => {
  170. this.headerStyle.transition = 'all .5s ease';
  171. });
  172. }
  173. if (bounding.top === 0) {
  174. this.isFixed = false;
  175. this.$nextTick(() => {
  176. this.headerStyle.transition = '';
  177. });
  178. }
  179. this.hangUp = direction === 'up';
  180. });
  181. }
  182. };
  183. </script>