FloorBar.vue 2.8 KB

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