horizontal-scrollable.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // module/health/components/horizontal-scrollable/horizontal-scrollable.ts
  2. const { shared, spring, runOnJS } = wx.worklet;
  3. Component({
  4. lifetimes: {
  5. ready() {
  6. },
  7. attached() {
  8. const x = shared(0);
  9. const offset = shared(1);
  10. (<any>this).applyAnimatedStyle('.scrollable', () => {
  11. 'worklet';
  12. return { transform: `translate(${x.value}px)`, };
  13. });
  14. (<any>this).x = x;
  15. (<any>this).offset = offset;
  16. }
  17. },
  18. properties: {
  19. dataset: { type: Array, value: [] },
  20. },
  21. data: {
  22. },
  23. methods: {
  24. handleHorizontalDrag(event: { deltaX: number }) {
  25. 'worklet';
  26. if ((<any>this).offset.value === 1) runOnJS(this.start.bind(this))();
  27. else {
  28. const value = Math.max((<any>this).x.value + event.deltaX, (<any>this).offset.value);
  29. (<any>this).x.value = Math.min(value, 0)
  30. }
  31. },
  32. start() {
  33. console.log('触发');
  34. const query = this.createSelectorQuery()
  35. query.select('.scrollable').boundingClientRect();
  36. query.selectAll('.scheme__card').boundingClientRect();
  37. query.exec(([box, sub]) => {
  38. const subWidth = sub[sub.length - 1].right - sub[0].left;
  39. const boxWidth = box.width;
  40. (<any>this).offset.value = subWidth > boxWidth ? box.width - subWidth : 0;
  41. })
  42. },
  43. preview(event: WechatMiniprogram.TouchEvent) {
  44. const url = event.target.dataset?.url ?? '';
  45. const sources = this.data.dataset.filter(item => ['image', 'video'].includes(item.type));
  46. const current = Math.max(sources.findIndex(item=>item.url === url), 0);
  47. wx.previewMedia({sources, current})
  48. }
  49. }
  50. })