horizontal-scrollable.ts 1.7 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. url:'https://wx.hzliuzhi.com/media/knowledge/sport/cover/八段锦.jpg'
  23. },
  24. methods: {
  25. handleHorizontalDrag(event: { deltaX: number }) {
  26. 'worklet';
  27. if ((<any>this).offset.value === 1) runOnJS(this.start.bind(this))();
  28. else {
  29. const value = Math.max((<any>this).x.value + event.deltaX, (<any>this).offset.value);
  30. (<any>this).x.value = Math.min(value, 0)
  31. }
  32. },
  33. start() {
  34. console.log('触发');
  35. const query = this.createSelectorQuery()
  36. query.select('.scrollable').boundingClientRect();
  37. query.selectAll('.scheme__card').boundingClientRect();
  38. query.exec(([box, sub]) => {
  39. const subWidth = sub[sub.length - 1].right - sub[0].left;
  40. const boxWidth = box.width;
  41. (<any>this).offset.value = subWidth > boxWidth ? box.width - subWidth : 0;
  42. })
  43. },
  44. preview(event: WechatMiniprogram.TouchEvent) {
  45. const url = event.target.dataset?.url ?? '';
  46. const sources = this.data.dataset.filter(item => ['image', 'video'].includes(item.type));
  47. const current = Math.max(sources.findIndex(item=>item.url === url), 0);
  48. wx.previewMedia({sources, current})
  49. }
  50. }
  51. })