AppSidebar1.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <template>
  2. <div class="index-nav" :class="{'index-nav-top':isActive}">
  3. <nav class="side-navigator-wrap">
  4. <div v-for="(item,index) in sideBarList" :key="index" class="nav-item-wrap">
  5. <div class="nav-item-content" :class="{ active: item.checked }">
  6. <router-link :to="item.path" class="nav-item">
  7. <!-- <i :class="item.icon"></i> -->
  8. <span class="nav-item-text">{{ item.name }}</span>
  9. </router-link>
  10. </div>
  11. </div>
  12. </nav>
  13. </div>
  14. </template>
  15. <script>
  16. export default {
  17. name: 'AppSidebar1',
  18. data() {
  19. return {
  20. sideBarList:[
  21. {
  22. value:'present',
  23. name:'当前积分',
  24. path:'/pointsRank/present',
  25. icon:'icon-mall-shouye',
  26. checked:false,
  27. },
  28. {
  29. value:'history',
  30. name:'获取总积分',
  31. path:'/pointsRank/history',
  32. icon:'icon-mall-jifenshangcheng',
  33. checked:false,
  34. },
  35. ],
  36. isActive:false,
  37. };
  38. },
  39. methods:{
  40. getRoute(){
  41. var path = this.$route.path;
  42. this.sideBarList.forEach(item => {
  43. if(path.indexOf(item.value) != -1){
  44. item.checked = true;
  45. } else{
  46. item.checked = false;
  47. }
  48. });
  49. },
  50. // 保存滚动值,这是兼容的写法
  51. handleScroll () {
  52. var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
  53. if(scrollTop >= 80){
  54. this.isActive = true
  55. }else{
  56. this.isActive = false
  57. }
  58. },
  59. },
  60. watch: {
  61. $route:'getRoute'
  62. },
  63. created() {
  64. this.getRoute();
  65. },
  66. mounted () {
  67. window.addEventListener('scroll', this.handleScroll)
  68. },
  69. destroyed () {
  70. // 离开该页面需要移除这个监听的事件,不然会报错
  71. window.removeEventListener('scroll', this.handleScroll)
  72. }
  73. }
  74. </script>
  75. <style scoped>
  76. .index-nav{
  77. width: 180px;
  78. position: -webkit-sticky;
  79. position: sticky;
  80. top: 80px;
  81. margin-right: 20px;
  82. height: -webkit-fit-content;
  83. height: -moz-fit-content;
  84. height: fit-content;
  85. border-radius: 4px;
  86. background-color: #fff;
  87. max-height: calc(100vh - 101px);
  88. /* overflow-x: hidden; */
  89. }
  90. .index-nav-top{
  91. top: 20px;
  92. max-height: calc(100vh - 40px);
  93. }
  94. li a {
  95. font-weight: bold;
  96. color: #515767;
  97. }
  98. li a.router-link-active {
  99. color: #1e80ff;
  100. }
  101. .side-navigator-wrap{
  102. min-width: 180px;
  103. box-sizing: border-box;
  104. padding: 8px;
  105. font-size: 16px;
  106. color: #515767;
  107. }
  108. .nav-item-wrap{
  109. display: flex;
  110. flex-direction: column;
  111. }
  112. .nav-item-content{
  113. line-height: 24px;
  114. border-radius: 4px;
  115. cursor: pointer;
  116. display: flex;
  117. flex-direction: row;
  118. align-items: center;
  119. justify-content: space-between;
  120. }
  121. .active{
  122. background-color: #eaf2ff;
  123. color: #1e80ff;
  124. font-weight: 500
  125. }
  126. .nav-item{
  127. display: inline-block;
  128. width: 100%;
  129. box-sizing: border-box;
  130. position: relative;
  131. padding: 10px 17px;
  132. }
  133. .nav-item i{
  134. vertical-align: middle;
  135. margin-right: 12px;
  136. }
  137. .nav-item-text{
  138. vertical-align: middle;
  139. position: relative;
  140. }
  141. </style>