t-linkage.vue 1.8 KB

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