header.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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-fixed {
  81. position: fixed;
  82. top: -80px;
  83. box-shadow: 0 2px 6px 0 rgba(50, 63, 87, 0.25);
  84. }
  85. .header-hangUp {
  86. top: 0;
  87. }
  88. .header-home {
  89. position: fixed;
  90. top: 0;
  91. background-color: rgba(32, 160, 255, 0);
  92. }
  93. </style>
  94. <template>
  95. <div class="headerWrapper">
  96. <header class="header"
  97. ref="header"
  98. :style="headerStyle"
  99. :class="{
  100. 'header-home': isHome,
  101. 'header-fixed': isFixed,
  102. 'header-hangUp': hangUp
  103. }">
  104. <div class="container">
  105. <h1><router-link to="/">Element<span>Beta</span></router-link></h1>
  106. <ul class="nav">
  107. <li class="nav-item">
  108. <router-link
  109. active-class="active"
  110. to="/guide">指南
  111. </router-link>
  112. </li>
  113. <li class="nav-item">
  114. <router-link
  115. active-class="active"
  116. to="/component">组件
  117. </router-link>
  118. </li>
  119. <li class="nav-item">
  120. <router-link
  121. active-class="active"
  122. to="/resource"
  123. exact>资源
  124. </router-link>
  125. </li>
  126. </ul>
  127. </div>
  128. </header>
  129. </div>
  130. </template>
  131. <script>
  132. export default {
  133. data() {
  134. return {
  135. active: '',
  136. isFixed: false,
  137. isHome: false,
  138. headerStyle: {},
  139. hangUp: false
  140. };
  141. },
  142. watch: {
  143. '$route.path'(val) {
  144. this.isHome = val === '/';
  145. this.headerStyle.backgroundColor = `rgba(32, 160, 255, ${ this.isHome ? '0' : '1' })`;
  146. }
  147. },
  148. mounted() {
  149. this.isHome = this.$route.path === '/';
  150. function scroll(fn) {
  151. var beforeScrollTop = document.body.scrollTop;
  152. window.addEventListener('scroll', () => {
  153. const afterScrollTop = document.body.scrollTop;
  154. var delta = afterScrollTop - beforeScrollTop;
  155. if (delta === 0) return false;
  156. fn(delta > 0 ? 'down' : 'up');
  157. beforeScrollTop = afterScrollTop;
  158. }, false);
  159. }
  160. scroll((direction) => {
  161. if (this.isHome) {
  162. this.hangUp = false;
  163. this.isFixed = false;
  164. this.headerStyle.transition = '';
  165. const threshold = 200;
  166. let alpha = Math.min(document.body.scrollTop, threshold) / threshold;
  167. this.$refs.header.style.backgroundColor = `rgba(32, 160, 255, ${ alpha })`;
  168. return;
  169. }
  170. this.headerStyle.backgroundColor = 'rgba(32, 160, 255, 1)';
  171. const bounding = this.$el.getBoundingClientRect();
  172. if (bounding.bottom < 0) {
  173. this.isFixed = true;
  174. this.$nextTick(() => {
  175. this.headerStyle.transition = 'all .5s ease';
  176. });
  177. }
  178. if (bounding.top === 0) {
  179. this.isFixed = false;
  180. this.$nextTick(() => {
  181. this.headerStyle.transition = '';
  182. });
  183. }
  184. this.hangUp = direction === 'up';
  185. });
  186. }
  187. };
  188. </script>