FloorBar.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <template>
  2. <ul class="floor-bar list-unstyled" :style="!visible?'opacity: 0;':'opacity: 1;'" v-if="floors.length">
  3. <li v-for="(floor, index) in floors" :key="index" class="floor-bar-item">
  4. <a @click="jumpFloor(index)"
  5. :style="{backgroundColor: index == activeFloor && floor.items[0] ? floor.items[0].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: Array,
  19. default: () => []
  20. }
  21. },
  22. data () {
  23. return {
  24. visible: false,
  25. activeFloor: 0,
  26. floor_scrollTop: 777
  27. }
  28. },
  29. methods: {
  30. _calcaulateHeight() {
  31. let nullObj = {clientHeight: 0}
  32. let obj0 = document.querySelectorAll('header')[0] || nullObj
  33. let obj1 = document.querySelectorAll('.header.clearfix')[0] || nullObj
  34. let obj2 = document.querySelectorAll('.nav-list')[0] || nullObj
  35. let obj3 = document.querySelectorAll('.carousel')[0] || nullObj
  36. // let obj4 = document.querySelectorAll('.advert-slide')[0] || nullObj
  37. let obj5 = document.querySelectorAll('.banner')[0] || nullObj
  38. let obj6 = document.querySelectorAll('.floor.price-floor')[0] || nullObj
  39. let obj7 = document.querySelectorAll('.floor.price-floor')[1] || nullObj
  40. let _heis0 = obj0.clientHeight
  41. let _heis1 = obj1.clientHeight
  42. let _heis2 = obj2.clientHeight
  43. let _heis3 = obj3.clientHeight
  44. // let _heis4 = obj4.clientHeight
  45. let _heis5 = obj5.clientHeight
  46. let _heis6 = obj6.clientHeight
  47. let _heis7 = obj7.clientHeight
  48. // this.bannerHeight = _heis0 + _heis1 + _heis2 + _heis3 + _heis4 + _heis5 + _heis6 + _heis7
  49. this.bannerHeight = _heis0 + _heis1 + _heis2 + _heis3 + _heis5 + _heis6 + _heis7
  50. this.listHeight = []
  51. this.list = document.querySelectorAll('.normal-floor')
  52. let height = 0
  53. this.listHeight.push(height)
  54. for (let i = 0; i < this.list.length; i++) {
  55. const item = this.list[i]
  56. if (i === 0) {
  57. height += this.bannerHeight + item.clientHeight / 2
  58. } else {
  59. height += item.clientHeight
  60. }
  61. this.listHeight.push(height)
  62. }
  63. },
  64. onScroll () {
  65. if (window.location.pathname === '/') {
  66. let scrolled = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop
  67. // console.log(this.bannerHeight)
  68. this.visible = scrolled >= this.bannerHeight - window.innerHeight / 2 + 200
  69. // console.log(this.bannerHeight, window.innerHeight)
  70. let _scrollHeight = scrolled
  71. const listHeight = this.listHeight
  72. for (let i = 0; i < listHeight.length; i++) {
  73. let height1 = listHeight[i]
  74. let height2 = listHeight[i + 1]
  75. // console.log(height1, _scrollHeight)
  76. // if (_scrollHeight >= height1) this.activeFloor = i
  77. if (_scrollHeight >= height1 && _scrollHeight < height2) {
  78. this.activeFloor = i
  79. }
  80. }
  81. if (_scrollHeight > listHeight[listHeight.length - 1]) {
  82. this.visible = false
  83. }
  84. // let floors = document.querySelectorAll('.normal-floor')
  85. // let barOffset = document.querySelector('.floor-bar').offsetTop
  86. // if (floors[0].offsetTop === 0) {
  87. // 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
  88. // if (this.visible) {
  89. // for (let i = 0; i < floors.length; i++) {
  90. // if (barOffset >= floors[i].offsetTop + this.floor_scrollTop - scrolled + 20) {
  91. // this.activeFloor = i
  92. // }
  93. // }
  94. // }
  95. // } else {
  96. // 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
  97. // if (this.visible) {
  98. // for (let i = 0; i < floors.length; i++) {
  99. // if (barOffset >= floors[i].offsetTop - scrolled + 20) {
  100. // this.activeFloor = i
  101. // }
  102. // }
  103. // }
  104. // }
  105. }
  106. },
  107. jumpFloor (index) {
  108. if (this.visible) {
  109. scrollTo(document.querySelectorAll('.normal-floor').item(index), 300, { offset: -60 })
  110. }
  111. }
  112. },
  113. mounted: function () {
  114. this.$nextTick(() => {
  115. this._calcaulateHeight()
  116. window.addEventListener('scroll', this.onScroll)
  117. })
  118. }
  119. }
  120. </script>
  121. <style lang="scss" scoped>
  122. .floor-bar {
  123. position: fixed;
  124. margin-left: -70px;
  125. width: 60px;
  126. bottom: 20%;
  127. .floor-bar-item {
  128. margin-bottom: 1px;
  129. text-align: center;
  130. a {
  131. display: block;
  132. width: 60px;
  133. height: 45px;
  134. padding-top: 5px;
  135. background-color: #b7dfff;
  136. color: #fff;
  137. overflow: hidden;
  138. .floor-item-name {
  139. font-size: 12px;
  140. display: inline-block;
  141. }
  142. }
  143. }
  144. }
  145. </style>