You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

281 line
9.7 KiB

  1. <template>
  2. <u-transition
  3. mode="fade"
  4. :show="show"
  5. :style="transStyle"
  6. :duration="fade ? 1000 : 0"
  7. >
  8. <view
  9. class="u-image box-border"
  10. @tap="onClick"
  11. :style="[wrapStyle, backgroundStyle]"
  12. >
  13. <image
  14. v-if="!isError"
  15. :src="src"
  16. :mode="mode"
  17. @error="onErrorHandler"
  18. @load="onLoadHandler"
  19. :show-menu-by-longpress="showMenuByLongpress"
  20. :lazy-load="lazyLoad"
  21. class="u-image__image"
  22. :style="{
  23. width: addUnit(width),
  24. height: addUnit(height),
  25. borderRadius: shape == 'circle' ? '10000px' : addUnit(radius)
  26. }"
  27. ></image>
  28. <view
  29. v-if="showLoading && loading"
  30. class="u-image__loading"
  31. :style="{
  32. borderRadius: shape == 'circle' ? '50%' : addUnit(radius),
  33. backgroundColor: this.bgColor,
  34. width: addUnit(width),
  35. height: addUnit(height)
  36. }"
  37. >
  38. <slot name="loading">
  39. <up-icon
  40. :name="loadingIcon"
  41. ></up-icon>
  42. </slot>
  43. </view>
  44. <view
  45. v-if="showError && isError && !loading"
  46. class="u-image__error"
  47. :style="{
  48. borderRadius: shape == 'circle' ? '50%' : addUnit(radius),
  49. backgroundColor: this.bgColor,
  50. width: addUnit(width),
  51. height: addUnit(height)
  52. }"
  53. >
  54. <slot name="error">
  55. <up-icon
  56. :name="errorIcon"
  57. ></up-icon>
  58. </slot>
  59. </view>
  60. </view>
  61. </u-transition>
  62. </template>
  63. <script>
  64. import { props } from './props';
  65. import { mpMixin } from '../../libs/mixin/mpMixin';
  66. import { mixin } from '../../libs/mixin/mixin';
  67. import { addUnit, addStyle, deepMerge } from '../../libs/function/index';
  68. /**
  69. * Image 图片
  70. * @description 此组件为uni-app的image组件的加强版,在继承了原有功能外,还支持淡入动画、加载中、加载失败提示、圆角值和形状等。
  71. * @tutorial https://uview-plus.jiangruyi.com/components/image.html
  72. * @property {String} src 图片地址
  73. * @property {String} mode 裁剪模式,见官网说明 (默认 'aspectFill' )
  74. * @property {String | Number} width 宽度,单位任意,如果为数值,则为px单位 (默认 '300' )
  75. * @property {String | Number} height 高度,单位任意,如果为数值,则为px单位 (默认 '225' )
  76. * @property {String} shape 图片形状,circle-圆形,square-方形 (默认 'square' )
  77. * @property {String | Number} radius 圆角值,单位任意,如果为数值,则为px单位 (默认 0 )
  78. * @property {Boolean} lazyLoad 是否懒加载,仅微信小程序、App、百度小程序、字节跳动小程序有效 (默认 true )
  79. * @property {Boolean} showMenuByLongpress 是否开启长按图片显示识别小程序码菜单,仅微信小程序有效 (默认 true )
  80. * @property {String} loadingIcon 加载中的图标,或者小图片 (默认 'photo' )
  81. * @property {String} errorIcon 加载失败的图标,或者小图片 (默认 'error-circle' )
  82. * @property {Boolean} showLoading 是否显示加载中的图标或者自定义的slot (默认 true )
  83. * @property {Boolean} showError 是否显示加载错误的图标或者自定义的slot (默认 true )
  84. * @property {Boolean} fade 是否需要淡入效果 (默认 true )
  85. * @property {Boolean} webp 只支持网络资源,只对微信小程序有效 (默认 false )
  86. * @property {String | Number} duration 搭配fade参数的过渡时间,单位ms (默认 500 )
  87. * @property {String} bgColor 背景颜色,用于深色页面加载图片时,为了和背景色融合 (默认 '#f3f4f6' )
  88. * @property {Object} customStyle 定义需要用到的外部样式
  89. * @event {Function} click 点击图片时触发
  90. * @event {Function} error 图片加载失败时触发
  91. * @event {Function} load 图片加载成功时触发
  92. * @example <u-image width="100%" height="300px" :src="src"></u-image>
  93. */
  94. export default {
  95. name: 'u-image',
  96. mixins: [mpMixin, mixin, props],
  97. data() {
  98. return {
  99. // 图片是否加载错误,如果是,则显示错误占位图
  100. isError: false,
  101. // 初始化组件时,默认为加载中状态
  102. loading: true,
  103. // 不透明度,为了实现淡入淡出的效果
  104. opacity: 1,
  105. // 过渡时间,因为props的值无法修改,故需要一个中间值
  106. durationTime: this.duration,
  107. // 图片加载完成时,去掉背景颜色,因为如果是png图片,就会显示灰色的背景
  108. backgroundStyle: {},
  109. // 用于fade模式的控制组件显示与否
  110. show: false
  111. };
  112. },
  113. watch: {
  114. src: {
  115. immediate: true,
  116. handler(n) {
  117. if (!n) {
  118. // 如果传入null或者'',或者false,或者undefined,标记为错误状态
  119. this.isError = true
  120. } else {
  121. this.isError = false;
  122. this.loading = true;
  123. }
  124. }
  125. }
  126. },
  127. computed: {
  128. resolvedSizeStyle() {
  129. const style = {};
  130. // #ifdef APP-NVUE
  131. style.width = addUnit(this.width);
  132. style.height = addUnit(this.height);
  133. // #endif
  134. // #ifndef APP-NVUE
  135. // widthFix/heightFix 加载完成后用 fit-content 跟随图片比例;其余模式固定宽高
  136. if (this.loading || this.isError || this.width == '100%' || this.mode != 'heightFix') {
  137. style.width = addUnit(this.width);
  138. } else {
  139. style.width = 'fit-content';
  140. }
  141. if (this.loading || this.isError || this.height == '100%' || this.mode != 'widthFix') {
  142. style.height = addUnit(this.height);
  143. } else {
  144. style.height = 'fit-content';
  145. }
  146. // #endif
  147. return style;
  148. },
  149. transStyle() {
  150. return {
  151. ...this.resolvedSizeStyle,
  152. // flex 行布局下默认 shrink=1 会把固定宽图片压成细条
  153. flexShrink: 0,
  154. boxSizing: 'border-box',
  155. maxWidth: '100%'
  156. };
  157. },
  158. wrapStyle() {
  159. let style = {
  160. ...this.resolvedSizeStyle,
  161. flexShrink: 0,
  162. boxSizing: 'border-box',
  163. maxWidth: '100%'
  164. };
  165. // 固定尺寸时用 minWidth/minHeight 抵抗 flex 压缩;百分比宽度不设 minWidth 以免撑破父级
  166. const widthText = String(style.width || '')
  167. const heightText = String(style.height || '')
  168. if (widthText && widthText !== 'fit-content' && !widthText.includes('%')) {
  169. style.minWidth = widthText
  170. }
  171. if (heightText && heightText !== 'fit-content' && !heightText.includes('%')) {
  172. style.minHeight = heightText
  173. }
  174. // 如果是显示圆形,设置一个很多的半径值即可
  175. style.borderRadius = this.shape == 'circle' ? '10000px' : addUnit(this.radius)
  176. // radius 可能是 "12px" 字符串,不能用 > 0 判断
  177. const radiusNumber = Number(String(this.radius ?? '').replace(/[^\d.-]/g, ''))
  178. const hasRadius = this.shape == 'circle' || (!Number.isNaN(radiusNumber) && radiusNumber > 0)
  179. // 裁剪圆角 / aspectFill 时需要 hidden,避免内容溢出成细条错位
  180. style.overflow = hasRadius || this.mode === 'aspectFill' || this.mode === 'aspectFit' ? 'hidden' : 'visible'
  181. // if (this.fade) {
  182. // style.opacity = this.opacity
  183. // // nvue下,这几个属性必须要分开写
  184. // style.transitionDuration = `${this.durationTime}ms`
  185. // style.transitionTimingFunction = 'ease-in-out'
  186. // style.transitionProperty = 'opacity'
  187. // }
  188. return deepMerge(style, addStyle(this.customStyle));
  189. }
  190. },
  191. mounted() {
  192. this.show = true
  193. },
  194. emits: ['click', 'error', 'load'],
  195. methods: {
  196. addUnit,
  197. // 点击图片
  198. onClick(e) {
  199. this.$emit('click', e)
  200. },
  201. // 图片加载失败
  202. onErrorHandler(err) {
  203. this.loading = false
  204. this.isError = true
  205. this.$emit('error', err)
  206. },
  207. // 图片加载完成,标记loading结束
  208. onLoadHandler(event) {
  209. this.loading = false
  210. this.isError = false
  211. this.$emit('load', event)
  212. this.removeBgColor()
  213. // 如果不需要动画效果,就不执行下方代码,同时移除加载时的背景颜色
  214. // 否则无需fade效果时,png图片依然能看到下方的背景色
  215. // if (!this.fade) return this.removeBgColor();
  216. // // 原来opacity为1(不透明,是为了显示占位图),改成0(透明,意味着该元素显示的是背景颜色,默认的灰色),再改成1,是为了获得过渡效果
  217. // this.opacity = 0;
  218. // // 这里设置为0,是为了图片展示到背景全透明这个过程时间为0,延时之后延时之后重新设置为duration,是为了获得背景透明(灰色)
  219. // // 到图片展示的过程中的淡入效果
  220. // this.durationTime = 0;
  221. // // 延时50ms,否则在浏览器H5,过渡效果无效
  222. // setTimeout(() => {
  223. // this.durationTime = this.duration;
  224. // this.opacity = 1;
  225. // setTimeout(() => {
  226. // this.removeBgColor();
  227. // }, this.durationTime);
  228. // }, 50);
  229. },
  230. // 移除图片的背景色
  231. removeBgColor() {
  232. // 淡入动画过渡完成后,将背景设置为透明色,否则png图片会看到灰色的背景
  233. // this.backgroundStyle = {
  234. // backgroundColor: this.bgColor || '#ffffff'
  235. // };
  236. }
  237. }
  238. };
  239. </script>
  240. <style lang="scss" scoped>
  241. $u-image-error-top:0px !default;
  242. $u-image-error-left:0px !default;
  243. $u-image-error-width:100% !default;
  244. $u-image-error-hight:100% !default;
  245. $u-image-error-background-color:$u-bg-color !default;
  246. $u-image-error-color:$u-tips-color !default;
  247. $u-image-error-font-size: 46rpx !default;
  248. .u-image {
  249. position: relative;
  250. transition: opacity 0.5s ease-in-out;
  251. flex-shrink: 0;
  252. max-width: 100%;
  253. box-sizing: border-box;
  254. &__image {
  255. width: 100%;
  256. height: 100%;
  257. display: block;
  258. }
  259. &__loading,
  260. &__error {
  261. position: absolute;
  262. top: $u-image-error-top;
  263. left: $u-image-error-left;
  264. width: $u-image-error-width;
  265. height: $u-image-error-hight;
  266. @include flex;
  267. align-items: center;
  268. justify-content: center;
  269. background-color: $u-image-error-background-color;
  270. color: $u-image-error-color;
  271. font-size: $u-image-error-font-size;
  272. }
  273. }
  274. </style>