u-image.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. <u-icon
  40. :name="loadingIcon"
  41. ></u-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. }"
  50. >
  51. <slot name="error">
  52. <u-icon
  53. :name="errorIcon"
  54. ></u-icon>
  55. </slot>
  56. </view>
  57. </view>
  58. </u-transition>
  59. </template>
  60. <script>
  61. import { props } from './props';
  62. import { mpMixin } from '../../libs/mixin/mpMixin';
  63. import { mixin } from '../../libs/mixin/mixin';
  64. import { addUnit, addStyle, deepMerge } from '../../libs/function/index';
  65. /**
  66. * Image 图片
  67. * @description 此组件为uni-app的image组件的加强版,在继承了原有功能外,还支持淡入动画、加载中、加载失败提示、圆角值和形状等。
  68. * @tutorial https://uview-plus.jiangruyi.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: [mpMixin, 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 = true;
  120. }
  121. }
  122. }
  123. },
  124. computed: {
  125. transStyle() {
  126. let style = {};
  127. // 通过调用addUnit()方法,如果有单位,如百分比,px单位等,直接返回,如果是纯粹的数值,则加上rpx单位
  128. // #ifdef APP-NVUE
  129. style.width = addUnit(this.width);
  130. style.height = addUnit(this.height);
  131. // #endif
  132. // #ifndef APP-NVUE
  133. if (this.width == '100%') {
  134. style.width = this.width;
  135. } else {
  136. style.width = 'fit-content';
  137. }
  138. if (this.height == '100%') {
  139. style.height = this.height;
  140. } else {
  141. style.height = 'fit-content';
  142. }
  143. // #endif
  144. return style;
  145. },
  146. wrapStyle() {
  147. let style = {};
  148. // 通过调用addUnit()方法,如果有单位,如百分比,px单位等,直接返回,如果是纯粹的数值,则加上rpx单位
  149. // #ifdef APP-NVUE
  150. style.width = addUnit(this.width);
  151. style.height = addUnit(this.height);
  152. // #endif
  153. // #ifndef APP-NVUE
  154. if (this.width == '100%') {
  155. style.width = this.width;
  156. } else {
  157. style.width = 'fit-content';
  158. }
  159. if (this.height == '100%') {
  160. style.height = this.height;
  161. } else {
  162. style.height = 'fit-content';
  163. }
  164. // #endif
  165. // 如果是显示圆形,设置一个很多的半径值即可
  166. style.borderRadius = this.shape == 'circle' ? '10000px' : addUnit(this.radius)
  167. // 如果设置圆角,必须要有hidden,否则可能圆角无效
  168. style.overflow = this.radius > 0 ? 'hidden' : 'visible'
  169. // if (this.fade) {
  170. // style.opacity = this.opacity
  171. // // nvue下,这几个属性必须要分开写
  172. // style.transitionDuration = `${this.durationTime}ms`
  173. // style.transitionTimingFunction = 'ease-in-out'
  174. // style.transitionProperty = 'opacity'
  175. // }
  176. return deepMerge(style, addStyle(this.customStyle));
  177. }
  178. },
  179. mounted() {
  180. this.show = true
  181. },
  182. emits: ['click', 'error', 'load'],
  183. methods: {
  184. addUnit,
  185. // 点击图片
  186. onClick(e) {
  187. this.$emit('click', e)
  188. },
  189. // 图片加载失败
  190. onErrorHandler(err) {
  191. this.loading = false
  192. this.isError = true
  193. this.$emit('error', err)
  194. },
  195. // 图片加载完成,标记loading结束
  196. onLoadHandler(event) {
  197. this.loading = false
  198. this.isError = false
  199. this.$emit('load', event)
  200. this.removeBgColor()
  201. // 如果不需要动画效果,就不执行下方代码,同时移除加载时的背景颜色
  202. // 否则无需fade效果时,png图片依然能看到下方的背景色
  203. // if (!this.fade) return this.removeBgColor();
  204. // // 原来opacity为1(不透明,是为了显示占位图),改成0(透明,意味着该元素显示的是背景颜色,默认的灰色),再改成1,是为了获得过渡效果
  205. // this.opacity = 0;
  206. // // 这里设置为0,是为了图片展示到背景全透明这个过程时间为0,延时之后延时之后重新设置为duration,是为了获得背景透明(灰色)
  207. // // 到图片展示的过程中的淡入效果
  208. // this.durationTime = 0;
  209. // // 延时50ms,否则在浏览器H5,过渡效果无效
  210. // setTimeout(() => {
  211. // this.durationTime = this.duration;
  212. // this.opacity = 1;
  213. // setTimeout(() => {
  214. // this.removeBgColor();
  215. // }, this.durationTime);
  216. // }, 50);
  217. },
  218. // 移除图片的背景色
  219. removeBgColor() {
  220. // 淡入动画过渡完成后,将背景设置为透明色,否则png图片会看到灰色的背景
  221. this.backgroundStyle = {
  222. backgroundColor: this.bgColor || '#ffffff'
  223. };
  224. }
  225. }
  226. };
  227. </script>
  228. <style lang="scss" scoped>
  229. @import '../../libs/css/components.scss';
  230. $u-image-error-top:0px !default;
  231. $u-image-error-left:0px !default;
  232. $u-image-error-width:100% !default;
  233. $u-image-error-hight:100% !default;
  234. $u-image-error-background-color:$u-bg-color !default;
  235. $u-image-error-color:$u-tips-color !default;
  236. $u-image-error-font-size: 46rpx !default;
  237. .u-image {
  238. position: relative;
  239. transition: opacity 0.5s ease-in-out;
  240. &__image {
  241. width: 100%;
  242. height: 100%;
  243. }
  244. &__loading,
  245. &__error {
  246. position: absolute;
  247. top: $u-image-error-top;
  248. left: $u-image-error-left;
  249. width: $u-image-error-width;
  250. height: $u-image-error-hight;
  251. @include flex;
  252. align-items: center;
  253. justify-content: center;
  254. background-color: $u-image-error-background-color;
  255. color: $u-image-error-color;
  256. font-size: $u-image-error-font-size;
  257. }
  258. }
  259. </style>