index.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. const systemInfo = wx.getSystemInfoSync();
  2. Component({
  3. externalClasses: ['t-class', 't-class-load'],
  4. properties: {
  5. loadFailed: {
  6. type: String,
  7. value: 'default',
  8. },
  9. loading: {
  10. type: String,
  11. value: 'default',
  12. },
  13. src: {
  14. type: String,
  15. value: '',
  16. },
  17. mode: {
  18. type: String,
  19. value: 'aspectFill',
  20. },
  21. webp: {
  22. type: Boolean,
  23. value: true,
  24. },
  25. lazyLoad: {
  26. type: Boolean,
  27. value: false,
  28. },
  29. showMenuByLongpress: {
  30. type: Boolean,
  31. value: false,
  32. },
  33. },
  34. data: {
  35. thumbHeight: 375,
  36. thumbWidth: 375,
  37. systemInfo,
  38. },
  39. lifetimes: {
  40. ready() {
  41. const { mode } = this.properties;
  42. // 获取容器的真实宽高,设置图片的裁剪宽度
  43. this.getRect('.J-image').then((res) => {
  44. if (res) {
  45. const { width, height } = res;
  46. this.setData(
  47. mode === 'heightFix'
  48. ? {
  49. thumbHeight: this.px2rpx(height) || 375,
  50. }
  51. : {
  52. thumbWidth: this.px2rpx(width) || 375,
  53. },
  54. );
  55. }
  56. });
  57. },
  58. },
  59. methods: {
  60. px2rpx(px) {
  61. return (750 / (systemInfo.screenWidth || 375)) * px;
  62. },
  63. getRect(selector) {
  64. return new Promise((resolve) => {
  65. if (!this.selectorQuery) {
  66. this.selectorQuery = this.createSelectorQuery();
  67. }
  68. this.selectorQuery.select(selector).boundingClientRect(resolve).exec();
  69. });
  70. },
  71. onLoad(e) {
  72. this.triggerEvent('load', e.detail);
  73. },
  74. onError(e) {
  75. this.triggerEvent('error', e.detail);
  76. },
  77. },
  78. });