u-image.vue 9.3 KB

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