You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
8.2 KiB

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