floor-nav.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <!-- -->
  2. <template>
  3. <div id="floor-nav">
  4. <div class="floor-nav-img" v-if="basicSet.pc_temp2_set && basicSet.pc_temp2_set.logo_src">
  5. <img :src="basicSet.pc_temp2_set.logo_src" alt="">
  6. </div>
  7. <div class="nav" v-if="basicSet.pc_temp2_set">
  8. <div class="nav-item" @click="jump(index)" :class="{'active-nav': currentClass == index}" v-for="(item,index) in basicSet.pc_temp2_set.module_diy_name" :key="index">{{item.name}}</div>
  9. </div>
  10. </div>
  11. </template>
  12. <script>
  13. import {mapState} from 'vuex'
  14. export default {
  15. data () {
  16. return {
  17. currentClass: 0,
  18. navArr: null,
  19. scroll: null,
  20. now: null,
  21. last: null,
  22. timer: null
  23. };
  24. },
  25. activated() {},
  26. components: {},
  27. computed: {
  28. ...mapState(['basicSet'])
  29. },
  30. mounted() {
  31. console.log("监听::滚动位置,设置左侧导航栏")
  32. this.initData();
  33. window.addEventListener("scroll", this.throttleFn, true);
  34. },
  35. destroyed(){
  36. console.log("销毁:::监听滚动")
  37. this.initData();
  38. window.removeEventListener("scroll", this.throttleFn, true);
  39. },
  40. methods: {
  41. initData(){
  42. this.navArr = null;
  43. this.scroll = null;
  44. },
  45. throttleFn() {
  46. // 节流
  47. let that = this;
  48. let interval = 100;
  49. that.now = +new Date();
  50. if (that.last && that.now - that.last < interval) {
  51. clearTimeout(that.timer);
  52. that.timer = setTimeout(() => {
  53. that.last = that.now;
  54. that.loadSroll();
  55. }, interval);
  56. } else {
  57. that.last = that.now;
  58. that.loadSroll();
  59. }
  60. },
  61. jump(index) {
  62. if(!this.navArr){
  63. this.navArr = document.getElementsByClassName("nav-section");
  64. }
  65. setTimeout(() => {
  66. this.$nextTick(() => {
  67. let jump = this.navArr;
  68. // 获取需要滚动的距离
  69. let total = jump[index].offsetTop - 180;
  70. window.scrollTo(0, total);
  71. // // Chrome
  72. // document.body.scrollTop = total;
  73. // // Firefox
  74. // document.documentElement.scrollTop = total;
  75. // // Safari
  76. // window.pageYOffset = total;
  77. });
  78. }, 20);
  79. },
  80. loadSroll() {
  81. let that = this;
  82. this.scroll = document.documentElement.scrollTop || document.body.scrollTop;
  83. if(!this.navArr){
  84. this.navArr = document.getElementsByClassName("nav-section");
  85. }
  86. for (let i = this.navArr.length - 1; i >= 0; i--) {
  87. if (that.scroll >= this.navArr[i].offsetTop - 220) {
  88. that.currentClass = i;
  89. return;
  90. }
  91. }
  92. }
  93. }
  94. }
  95. </script>
  96. <style lang='scss' rel='stylesheet/scss' scoped>
  97. #floor-nav{
  98. height: auto;
  99. width: 110px;
  100. text-align: center;
  101. position: fixed;
  102. top: calc(50% - 110px);
  103. margin-left: -140px;
  104. z-index: 999;
  105. .floor-nav-img{
  106. width: 95px;
  107. height: 55px;
  108. margin: 0 auto;
  109. img{
  110. width: 100%;
  111. height: 100%;
  112. }
  113. }
  114. .nav{
  115. position: relative;
  116. border-radius: 10px;
  117. overflow: hidden;
  118. box-shadow: 0px 0px 20px 0px rgba(4, 0, 0, 0.05);
  119. .nav-item{
  120. display: inline-block;
  121. background: #fff;
  122. // line-height: 44px;
  123. cursor: pointer;
  124. width: 100%;
  125. font-size: 14px;
  126. position: relative;
  127. overflow: hidden;
  128. margin-top: -4px;
  129. // height: 44px;
  130. padding: 12px 0;
  131. // white-space: nowrap;
  132. // text-overflow: ellipsis;
  133. &::after{
  134. content: '';
  135. position: absolute;
  136. width: 85px;
  137. height: 1px;
  138. background: #E6E6E6;
  139. left: 12px;
  140. bottom: 0;
  141. }
  142. }
  143. .active-nav{
  144. color: #F2F3F5;
  145. background: var(--color);
  146. font-weight: bold;
  147. &::after{
  148. height: 0;
  149. }
  150. }
  151. }
  152. }
  153. </style>