FloorBar.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <template>
  2. <ul class="floor-bar list-unstyled" :style="!visible?'opacity: 0;':'opacity: 1;'">
  3. <li v-for="(floor, index) in floors.data" :key="index" class="floor-bar-item">
  4. <a @click="jumpFloor(index)"
  5. :style="{backgroundColor: index==activeFloor?floor.items[1].backGroundColor:'#b7dfff'}">
  6. <span>F{{ floor.floorNumber }}</span><br/>
  7. <span class="floor-item-name">{{ floor.name }}</span>
  8. </a>
  9. </li>
  10. </ul>
  11. </template>
  12. <script>
  13. import { scrollTo } from '~utils/scroll'
  14. export default {
  15. name: 'floor-bar',
  16. props: {
  17. floors: {
  18. type: Object
  19. }
  20. },
  21. data () {
  22. return {
  23. visible: false,
  24. activeFloor: 0,
  25. floor_scrollTop: 777
  26. }
  27. },
  28. methods: {
  29. _calcaulateHeight() {
  30. this.bannerHeight = document.querySelectorAll('.normal-floor')[0].offsetTop
  31. this.listHeight = []
  32. this.list = document.querySelectorAll('.normal-floor')
  33. let height = 0
  34. this.listHeight.push(height)
  35. for (let i = 0; i < this.list.length; i++) {
  36. const item = this.list[i]
  37. if (i === 0) {
  38. height += item.offsetTop + item.clientHeight / 2
  39. } else {
  40. height += item.clientHeight
  41. }
  42. this.listHeight.push(height)
  43. }
  44. },
  45. onScroll () {
  46. if (window.location.pathname === '/') {
  47. let scrolled = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop
  48. this.visible = scrolled >= this.bannerHeight - window.innerHeight / 2 + 220
  49. let _scrollHeight = scrolled
  50. const listHeight = this.listHeight
  51. for (let i = 0; i < listHeight.length; i++) {
  52. let height1 = listHeight[i]
  53. // let height2 = listHeight[i + 1]
  54. console.log(height1, _scrollHeight)
  55. if (_scrollHeight >= height1) this.activeFloor = i
  56. // if (_scrollHeight >= height1 && _scrollHeight < height2) {
  57. // this.activeFloor = i
  58. // }
  59. }
  60. if (_scrollHeight > listHeight[listHeight.length - 1]) {
  61. this.visible = false
  62. }
  63. // let floors = document.querySelectorAll('.normal-floor')
  64. // let barOffset = document.querySelector('.floor-bar').offsetTop
  65. // if (floors[0].offsetTop === 0) {
  66. // this.visible = scrolled >= floors[0].offsetTop + this.floor_scrollTop - barOffset && scrolled <= floors[floors.length - 1].offsetTop + floors[floors.length - 1].offsetHeight - barOffset - document.querySelector('.floor-bar').offsetHeight + this.floor_scrollTop
  67. // if (this.visible) {
  68. // for (let i = 0; i < floors.length; i++) {
  69. // if (barOffset >= floors[i].offsetTop + this.floor_scrollTop - scrolled + 20) {
  70. // this.activeFloor = i
  71. // }
  72. // }
  73. // }
  74. // } else {
  75. // this.visible = scrolled >= floors[0].offsetTop - barOffset + 40 && scrolled <= floors[floors.length - 1].offsetTop + floors[floors.length - 1].offsetHeight - barOffset - document.querySelector('.floor-bar').offsetHeight
  76. // if (this.visible) {
  77. // for (let i = 0; i < floors.length; i++) {
  78. // if (barOffset >= floors[i].offsetTop - scrolled + 20) {
  79. // this.activeFloor = i
  80. // }
  81. // }
  82. // }
  83. // }
  84. }
  85. },
  86. jumpFloor (index) {
  87. if (this.visible) {
  88. scrollTo(document.querySelectorAll('.normal-floor').item(index), 300, { offset: -60 })
  89. }
  90. }
  91. },
  92. mounted: function () {
  93. this.$nextTick(function () {
  94. this._calcaulateHeight()
  95. window.addEventListener('scroll', this.onScroll)
  96. })
  97. }
  98. }
  99. </script>
  100. <style lang="scss" scoped>
  101. .floor-bar {
  102. position: fixed;
  103. margin-left: -70px;
  104. width: 60px;
  105. bottom: 20%;
  106. .floor-bar-item {
  107. margin-bottom: 1px;
  108. text-align: center;
  109. a {
  110. display: block;
  111. width: 60px;
  112. height: 45px;
  113. padding-top: 5px;
  114. background-color: #b7dfff;
  115. color: #fff;
  116. overflow: hidden;
  117. .floor-item-name {
  118. font-size: 12px;
  119. display: inline-block;
  120. }
  121. }
  122. }
  123. }
  124. </style>