index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // components/My_categoryListings/index.js
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. properties: {
  7. height: {
  8. type: String,
  9. value: '500'
  10. },
  11. list: {
  12. type: Array,
  13. value: []
  14. }
  15. },
  16. options: {
  17. addGlobalClass: true
  18. },
  19. lifetimes: {
  20. ready() {
  21. /* setTimeout(() => {
  22. this.setData({
  23. intoViewId: 'basic1'
  24. })
  25. }, 1000) */
  26. }
  27. },
  28. /**
  29. * 组件的初始数据
  30. */
  31. data: {
  32. result: ['a', 'b'],
  33. leftIntoViewId: null,
  34. rightIntoViewId: null,
  35. heightList: [],
  36. },
  37. observers: {
  38. 'list': function (list) {
  39. if (!list.length) return;
  40. if (this.data.intoViewId == null) this.setData({
  41. leftIntoViewId: list[0].system + list[0].systemid,
  42. rightIntoViewId: list[0].system + list[0].systemid
  43. })
  44. setTimeout(() => {
  45. this.getAppsHeight(list);
  46. }, 500)
  47. }
  48. },
  49. /**
  50. * 组件的方法列表
  51. */
  52. methods: {
  53. /* 点击左侧分类 */
  54. changeType(e) {
  55. this.setData({
  56. leftIntoViewId: e.target.id,
  57. rightIntoViewId: e.target.id
  58. })
  59. },
  60. getAppsHeight(list) {
  61. let heightList = [];
  62. for (let i = 0; i < list.length; i++) {
  63. let cas = '.' + list[i].system + list[i].systemid;
  64. let query = wx.createSelectorQuery().in(this).select(cas).boundingClientRect();
  65. query.exec(res => {
  66. if (!res[0]) return this.getAppsHeight(list);
  67. heightList.push(res[0])
  68. if (heightList.length == list.length) this.setData({
  69. heightList
  70. })
  71. })
  72. }
  73. },
  74. viewScroll({
  75. detail
  76. }) {
  77. let arr = this.data.heightList, //获取元素信息
  78. id = this.data.leftIntoViewId, //获取当前左侧栏选项
  79. i = arr.findIndex(v => v.id == id), //选项当前在数组中的索引号
  80. scrollTop = detail.scrollTop,
  81. //判断当前索引是不是数组最后一条数据,不是的话拿下一个索引的距顶距离
  82. top = (i + 1 == arr.length) ? arr[i].top - 15 - arr[i].height : arr[i + 1].top - 15 - arr[i + 1].height;
  83. if (scrollTop >= top) {
  84. if (!arr[i + 1]) return;
  85. if (id == arr[i + 1].id) return;
  86. this.setData({
  87. leftIntoViewId: arr[i + 1].id
  88. })
  89. } else if (scrollTop < top) {
  90. if (!arr[i - 1] || id == arr[0].id) return;
  91. if (scrollTop < Math.abs((arr[i - 1].top - 15 - arr[i - 1].height) / 2)) this.setData({
  92. leftIntoViewId: arr[i - 1].id
  93. })
  94. } else {
  95. if (id == arr[1].id) return; //排除第一个
  96. }
  97. },
  98. }
  99. })