header.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <style scoped>
  2. .headerWrapper {
  3. height: 80px;
  4. }
  5. .header {
  6. height: 80px;
  7. background-color: #20a0ff;
  8. color: #fff;
  9. top: 0;
  10. left: 0;
  11. width: 100%;
  12. z-index: 1000;
  13. line-height: @height;
  14. z-index: 100;
  15. position: relative;
  16. .container {
  17. height: 100%;
  18. }
  19. h1 {
  20. margin: 0;
  21. float: left;
  22. font-size: 32px;
  23. font-weight: normal;
  24. a {
  25. color: #fff;
  26. text-decoration: none;
  27. display: block;
  28. }
  29. span {
  30. font-size: 12px;
  31. display: inline-block;
  32. width: 34px;
  33. height: 18px;
  34. border: 1px solid #fff;
  35. text-align: center;
  36. line-height: 18px;
  37. vertical-align: middle;
  38. margin-left: 10px;
  39. border-radius: 3px;
  40. }
  41. }
  42. .nav {
  43. float: right;
  44. height: 100%;
  45. line-height: 80px;
  46. background: transparent;
  47. @utils-clearfix;
  48. padding: 0;
  49. margin: 0;
  50. }
  51. .nav-item {
  52. margin: 0;
  53. float: left;
  54. list-style: none;
  55. position: relative;
  56. cursor: pointer;
  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. /*.el-menu-item__bar {
  75. background-color: #99d2fc;
  76. }*/
  77. }
  78. .header-fixed {
  79. position: fixed;
  80. top: -80px;
  81. box-shadow: 0px 2px 8px 0px rgba(50,63,87,0.45);
  82. }
  83. .header-hangUp {
  84. top: 0;
  85. }
  86. </style>
  87. <template>
  88. <div class="headerWrapper">
  89. <header class="header"
  90. :style="headerStyle"
  91. :class="{
  92. 'header-fixed': isFixed,
  93. 'header-hangUp': hangUp
  94. }">
  95. <div class="container">
  96. <h1><router-link to="/">Element<span>Beta</span></router-link></h1>
  97. <ul class="nav">
  98. <li class="nav-item">
  99. <router-link
  100. active-class="active"
  101. to="/guide/design"
  102. exact>指南
  103. </router-link>
  104. </li>
  105. <li class="nav-item">
  106. <router-link
  107. active-class="active"
  108. to="/component/button"
  109. exact>组件
  110. </router-link>
  111. </li>
  112. <li class="nav-item">
  113. <router-link
  114. active-class="active"
  115. to="/resource"
  116. exact>资源
  117. </router-link>
  118. </li>
  119. </ul>
  120. </div>
  121. </header>
  122. </div>
  123. </template>
  124. <script>
  125. export default {
  126. data() {
  127. return {
  128. active: '',
  129. isFixed: false,
  130. headerStyle: {},
  131. hangUp: false
  132. };
  133. },
  134. watch: {
  135. },
  136. mounted() {
  137. var scrollTop = 0;
  138. function scroll(fn) {
  139. var beforeScrollTop = document.body.scrollTop;
  140. window.addEventListener('scroll', () => {
  141. const afterScrollTop = document.body.scrollTop;
  142. var delta = afterScrollTop - beforeScrollTop;
  143. if (delta === 0) return false;
  144. fn(delta > 0 ? 'down' : 'up');
  145. beforeScrollTop = afterScrollTop;
  146. scrollTop = afterScrollTop;
  147. }, false);
  148. }
  149. scroll((direction) => {
  150. const bounding = this.$el.getBoundingClientRect();
  151. if (bounding.bottom < 0) {
  152. this.isFixed = true;
  153. this.$nextTick(() => {
  154. this.headerStyle.transition = 'all .5s ease';
  155. });
  156. }
  157. if (bounding.top === 0) {
  158. this.isFixed = false;
  159. this.$nextTick(() => {
  160. this.headerStyle.transition = '';
  161. });
  162. }
  163. this.hangUp = direction === 'up';
  164. });
  165. }
  166. };
  167. </script>