FloorBar.vue 5.1 KB

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