FloorBar.vue 5.3 KB

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