u-image.vue 8.0 KB

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