header.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 #fff;
  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</router-link></h1>
  101. <ul class="nav">
  102. <li class="nav-item">
  103. <router-link
  104. active-class="active"
  105. to="/guide/design"
  106. exact>指南
  107. </router-link>
  108. </li>
  109. <li class="nav-item">
  110. <router-link
  111. active-class="active"
  112. to="/component/layout"
  113. exact>组件
  114. </router-link>
  115. </li>
  116. <li class="nav-item">
  117. <router-link
  118. active-class="active"
  119. to="/resource"
  120. exact>资源
  121. </router-link>
  122. </li>
  123. </ul>
  124. </div>
  125. </header>
  126. </div>
  127. </template>
  128. <script>
  129. export default {
  130. data() {
  131. return {
  132. active: '',
  133. isFixed: false,
  134. isHome: false,
  135. headerStyle: {},
  136. hangUp: false
  137. };
  138. },
  139. watch: {
  140. '$route.path'(val) {
  141. this.isHome = val === '/';
  142. this.headerStyle.backgroundColor = `rgba(32, 160, 255, ${ this.isHome ? '0' : '1' })`;
  143. }
  144. },
  145. mounted() {
  146. this.isHome = this.$route.path === '/';
  147. function scroll(fn) {
  148. var beforeScrollTop = document.body.scrollTop;
  149. window.addEventListener('scroll', () => {
  150. const afterScrollTop = document.body.scrollTop;
  151. var delta = afterScrollTop - beforeScrollTop;
  152. if (delta === 0) return false;
  153. fn(delta > 0 ? 'down' : 'up');
  154. beforeScrollTop = afterScrollTop;
  155. }, false);
  156. }
  157. scroll((direction) => {
  158. if (this.isHome) {
  159. const threshold = 200;
  160. let alpha = Math.min(document.body.scrollTop, threshold) / threshold;
  161. this.$refs.header.style.backgroundColor = `rgba(32, 160, 255, ${ alpha })`;
  162. return;
  163. }
  164. this.headerStyle.backgroundColor = 'rgba(32, 160, 255, 1)';
  165. const bounding = this.$el.getBoundingClientRect();
  166. if (bounding.bottom < 0) {
  167. this.isFixed = true;
  168. this.$nextTick(() => {
  169. this.headerStyle.transition = 'all .5s ease';
  170. });
  171. }
  172. if (bounding.top === 0) {
  173. this.isFixed = false;
  174. this.$nextTick(() => {
  175. this.headerStyle.transition = '';
  176. });
  177. }
  178. this.hangUp = direction === 'up';
  179. });
  180. }
  181. };
  182. </script>