Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

95 rindas
2.4 KiB

  1. <template>
  2. <image
  3. :mode="node.attr.mode"
  4. :lazy-load="node.attr.lazyLoad"
  5. :class="node.classStr"
  6. :style="newStyleStr || node.styleStr"
  7. :data-src="node.attr.src"
  8. :src="node.attr.src"
  9. @tap="wxParseImgTap"
  10. @load="wxParseImgLoad"
  11. />
  12. </template>
  13. <script>
  14. export default {
  15. name: 'wxParseImg',
  16. data() {
  17. return {
  18. newStyleStr: '',
  19. preview: true
  20. };
  21. },
  22. inject: ['parseWidth'],
  23. mounted() {},
  24. props: {
  25. node: {
  26. type: Object,
  27. default() {
  28. return {};
  29. }
  30. }
  31. },
  32. methods: {
  33. wxParseImgTap(e) {
  34. if (!this.preview) return;
  35. const { src } = e.currentTarget.dataset;
  36. if (!src) return;
  37. let parent = this.$parent;
  38. while (!parent.preview || typeof parent.preview !== 'function') {
  39. // TODO 遍历获取父节点执行方法
  40. parent = parent.$parent;
  41. }
  42. parent.preview(src, e);
  43. },
  44. // 图片视觉宽高计算函数区
  45. wxParseImgLoad(e) {
  46. const { src } = e.currentTarget.dataset;
  47. if (!src) return;
  48. let { width, height } = e.mp.detail;
  49. const recal = this.wxAutoImageCal(width, height);
  50. const { imageheight, imageWidth } = recal;
  51. const { padding, mode } = this.node.attr;//删除padding
  52. // const { mode } = this.node.attr;
  53. const { styleStr } = this.node;
  54. const imageHeightStyle = mode === 'widthFix' ? '' : `height: ${imageheight}px;`;
  55. this.newStyleStr = `${styleStr}; ${imageHeightStyle}; width: ${imageWidth}px; padding: 0 ${+padding}px;`;//删除padding
  56. // this.newStyleStr = `${styleStr}; ${imageHeightStyle}; width: ${imageWidth}px;`;
  57. },
  58. // 计算视觉优先的图片宽高
  59. wxAutoImageCal(originalWidth, originalHeight) {
  60. // 获取图片的原始长宽
  61. const windowWidth = this.parseWidth.value;
  62. const results = {};
  63. if (originalWidth < 60 || originalHeight < 60) {
  64. const { src } = this.node.attr;
  65. let parent = this.$parent;
  66. while (!parent.preview || typeof parent.preview !== 'function') {
  67. parent = parent.$parent;
  68. }
  69. parent.removeImageUrl(src);
  70. this.preview = false;
  71. }
  72. // 判断按照那种方式进行缩放
  73. if (originalWidth > windowWidth) {
  74. // 在图片width大于手机屏幕width时候
  75. results.imageWidth = windowWidth;
  76. results.imageheight = windowWidth * (originalHeight / originalWidth);
  77. } else {
  78. // 否则展示原来的数据
  79. results.imageWidth = originalWidth;
  80. results.imageheight = originalHeight;
  81. }
  82. return results;
  83. }
  84. }
  85. };
  86. </script>