t-linkage.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <template>
  2. <view class="tui-linkage-class"><slot></slot></view>
  3. </template>
  4. <script>
  5. //分类菜单左右联动
  6. export default {
  7. name: 'tLinkage',
  8. props: {
  9. scrollTop: {
  10. type: Number
  11. },
  12. recalc: {
  13. type: Number,
  14. default: 0
  15. },
  16. //px
  17. distanceTop: {
  18. type: Number,
  19. default: 0
  20. },
  21. //列表中的索引值
  22. index: {
  23. type: [Number, String],
  24. default: 0
  25. }
  26. },
  27. watch: {
  28. scrollTop(newValue, oldValue) {
  29. if (this.initialize != 0) {
  30. this.updateScrollChange(() => {
  31. this.updateStickyChange();
  32. this.initialize = 0;
  33. });
  34. } else {
  35. this.updateStickyChange();
  36. }
  37. },
  38. recalc(newValue, oldValue) {
  39. this.updateScrollChange(() => {
  40. this.updateStickyChange();
  41. this.initialize = 0;
  42. });
  43. }
  44. },
  45. created() {
  46. this.initialize = this.recalc;
  47. },
  48. mounted() {
  49. setTimeout(() => {
  50. this.updateScrollChange();
  51. }, 20);
  52. },
  53. data() {
  54. return {
  55. timer: null,
  56. top: 0,
  57. height: 0,
  58. initialize: 0 //重新初始化
  59. };
  60. },
  61. methods: {
  62. updateStickyChange() {
  63. const top = this.top;
  64. const height = this.height;
  65. const scrollTop = this.scrollTop;
  66. let linkage = scrollTop + this.distanceTop >= top && scrollTop + this.distanceTop < top + height ? true : false;
  67. if (linkage) {
  68. this.$emit('linkage', {
  69. isLinkage: linkage,
  70. index: this.index
  71. });
  72. }
  73. },
  74. updateScrollChange(callback) {
  75. if (this.timer) {
  76. clearTimeout(this.timer);
  77. this.timer = null;
  78. }
  79. this.timer = setTimeout(() => {
  80. const className = '.tui-linkage-class';
  81. const query = uni.createSelectorQuery().in(this);
  82. query
  83. .select(className)
  84. .boundingClientRect(res => {
  85. if (res) {
  86. this.top = res.top + (this.scrollTop || 0);
  87. this.height = res.height;
  88. callback && callback();
  89. }
  90. })
  91. .exec();
  92. }, 0);
  93. }
  94. }
  95. };
  96. </script>
  97. <style></style>