FloorBar.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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: -1,
  25. floor_scrollTop: 777
  26. }
  27. },
  28. methods: {
  29. onScroll () {
  30. if (window.location.pathname === '/') {
  31. let scrolled = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop
  32. let floors = document.querySelectorAll('.floor')
  33. let barOffset = document.querySelector('.floor-bar').offsetTop
  34. if (floors[0].offsetTop === 0) {
  35. 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
  36. if (this.visible) {
  37. for (let i = 0; i < floors.length; i++) {
  38. if (barOffset >= floors[i].offsetTop + this.floor_scrollTop - scrolled + 60) {
  39. this.activeFloor = i
  40. }
  41. }
  42. }
  43. } else {
  44. 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
  45. if (this.visible) {
  46. for (let i = 0; i < floors.length; i++) {
  47. if (barOffset >= floors[i].offsetTop - scrolled + 60) {
  48. this.activeFloor = i
  49. }
  50. }
  51. }
  52. }
  53. }
  54. },
  55. jumpFloor (index) {
  56. if (this.visible) {
  57. scrollTo(document.querySelectorAll('.floor').item(index), 300, { offset: -60 })
  58. }
  59. }
  60. },
  61. mounted: function () {
  62. this.$nextTick(function () {
  63. window.addEventListener('scroll', this.onScroll)
  64. })
  65. }
  66. }
  67. </script>
  68. <style lang="scss" scoped>
  69. .floor-bar {
  70. position: fixed;
  71. margin-left: -70px;
  72. width: 60px;
  73. bottom: 20%;
  74. .floor-bar-item {
  75. margin-bottom: 1px;
  76. text-align: center;
  77. a {
  78. display: block;
  79. width: 60px;
  80. height: 45px;
  81. padding-top: 5px;
  82. background-color: #b7dfff;
  83. color: #fff;
  84. overflow: hidden;
  85. .floor-item-name {
  86. font-size: 12px;
  87. display: inline-block;
  88. }
  89. }
  90. }
  91. }
  92. </style>