explanation.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // pages/skinRecord/components/explanation/explanation.js
  2. const app = getApp();
  3. Component({
  4. /**
  5. * 组件的属性列表
  6. */
  7. options: {
  8. addGlobalClass: true
  9. },
  10. properties: {
  11. explanation: {
  12. type: Array,
  13. value: [],
  14. },
  15. index: {
  16. type: String,
  17. value: '',
  18. observer(newVal) {
  19. if(newVal == '1') {
  20. this.data.heightArr = []
  21. let query = wx.createSelectorQuery().in(this);;
  22. query.selectAll('.item-box').boundingClientRect((rect)=> {
  23. let sum = 0;
  24. rect.forEach((item,index) => {
  25. if(index == 0) {
  26. sum = item.height;
  27. this.data.heightArr.push(sum);
  28. } else {
  29. sum+=item.height
  30. this.data.heightArr.push(sum);
  31. }
  32. // this.calculateHeight(item.height);
  33. })
  34. console.log(rect)
  35. }).exec();
  36. }
  37. }
  38. }
  39. },
  40. /**
  41. * 组件的初始数据
  42. */
  43. data: {
  44. heightArr: [],
  45. _scrollLeft: '',
  46. current: 0,
  47. toView: '',
  48. test: '',
  49. posterPopup: false,
  50. posterItem: {},
  51. posterType: false
  52. },
  53. /**
  54. * 组件的方法列表
  55. */
  56. methods: {
  57. onChangeNav(e) {
  58. let {
  59. index
  60. } = e.currentTarget.dataset;
  61. let ele = 'scroll-item-' + index
  62. this.getRect('#' + ele);
  63. this.setData({
  64. current: e.currentTarget.dataset.index,
  65. toView: 'item' + index
  66. })
  67. },
  68. //获取点击元素的信息,ele为传入的id
  69. getRect(ele) {
  70. var that = this;
  71. //节点查询
  72. const query = wx.createSelectorQuery().in(this);
  73. query.select(ele).boundingClientRect(function (rect) {
  74. that.moveTo(rect);
  75. }).exec()
  76. },
  77. moveTo(rect) {
  78. let screenHalfWidth = app.globalData.systemInfo.screenWidth / 2;
  79. let subLeft = rect.left;
  80. let subHalfWidth = rect.width / 2;
  81. let scrollLeft = this.data._scrollLeft
  82. let distance = subLeft - screenHalfWidth + subHalfWidth;
  83. scrollLeft = scrollLeft + distance;
  84. this.setData({
  85. scrollLeft: scrollLeft
  86. })
  87. },
  88. scrollMove(e) {
  89. console.log(e)
  90. this.setData({
  91. _scrollLeft: e.detail.scrollLeft
  92. })
  93. },
  94. // 弹框状态
  95. onPopupState(e,value) {
  96. console.log(e)
  97. if (e) {
  98. value = e.currentTarget.dataset.value
  99. }
  100. this.setData({
  101. posterPopup: value,
  102. posterItem: e.currentTarget.dataset.item || {},
  103. posterType: e.currentTarget.dataset.type || false
  104. })
  105. },
  106. onSwitchType() {
  107. this.setData({
  108. posterType: !this.data.posterType
  109. })
  110. },
  111. scrollMoveY(e) {
  112. let index = this.calculateIndex(this.data.heightArr,e.detail.scrollTop);
  113. if(index == this.data.current) {
  114. return;
  115. }
  116. this.getRect('#scroll-item-' + index);
  117. this.setData({
  118. current: index,
  119. })
  120. },
  121. // 计算左边选中的下标
  122. calculateIndex(arr, scrollHeight) {
  123. let index= '';
  124. for(let i =0;i<arr.length;i++) {
  125. if (scrollHeight >= 0 && scrollHeight < arr[0]){
  126. index = 0;
  127. }else if(scrollHeight >= arr[i-1] && scrollHeight < arr[i]){
  128. index = i;
  129. }
  130. }
  131. return index;
  132. },
  133. }
  134. })