u-image.vue 7.9 KB

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