waterfall-item.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { querySelector } from './query-node';
  2. Component({
  3. externalClasses: ['custom-class'],
  4. relations: {
  5. './waterfall': {
  6. type: 'parent',
  7. linked(target) {
  8. }
  9. }
  10. },
  11. properties: {
  12. },
  13. data: {
  14. itemCount: 10, // 瀑布流重绘之前,在viewport之外渲染,防止低性能手机出现页面抖动的问题
  15. name: 'li',
  16. position: 'left',
  17. top: -1,
  18. },
  19. parent: null,
  20. observers: {
  21. },
  22. lifetimes: {
  23. ready() {
  24. this.renderView()
  25. },
  26. },
  27. /**
  28. * 组件的方法列表
  29. */
  30. methods: {
  31. renderView(){
  32. // setTimeout(() => {
  33. const { itemCount } = this.data;
  34. const [waterfall] = this.getRelationNodes('./waterfall');
  35. waterfall.childCount += 1;
  36. this.parent = waterfall;
  37. this.setData({
  38. itemCount: itemCount + waterfall.childCount,
  39. })
  40. this.setWaterfallItemPosition();
  41. // }, 100);
  42. },
  43. async setItemGap(node) {
  44. const { itemGap, waterfallWidth } = this.parent;
  45. if(itemGap < 0 && node) {
  46. const { width } = node;
  47. this.parent.itemGap = waterfallWidth - width * 2;
  48. }
  49. },
  50. async setWaterfallItemPosition() {
  51. // setTimeout(() => {
  52. querySelector('.waterfall-item', this)
  53. .then(async (node) => {
  54. await this.setItemGap(node);
  55. const {
  56. top,
  57. position,
  58. } = await this.parent.getWaterfallItemPostionInfo(node);
  59. this.setData({
  60. top,
  61. position
  62. })
  63. })
  64. // }, 500);
  65. },
  66. }
  67. })