u-image.vue 7.6 KB

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