u-image.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <template>
  2. <view class="u-image" @tap="onClick" :style="[wrapStyle, backgroundStyle]">
  3. <image
  4. v-if="!isError"
  5. :src="src"
  6. :mode="mode"
  7. @error="onErrorHandler"
  8. @load="onLoadHandler"
  9. :lazy-load="lazyLoad"
  10. class="u-image__image"
  11. :show-menu-by-longpress="showMenuByLongpress"
  12. :style="{
  13. borderRadius: shape == 'circle' ? '50%' : $u.addUnit(borderRadius)
  14. }"
  15. ></image>
  16. <view
  17. v-if="showLoading && loading"
  18. class="u-image__loading"
  19. :style="{
  20. borderRadius: shape == 'circle' ? '50%' : $u.addUnit(borderRadius),
  21. backgroundColor: bgColor
  22. }"
  23. >
  24. <slot v-if="$slots.loading" name="loading" />
  25. <u-icon v-else :name="loadingIcon" :width="width" :height="height"></u-icon>
  26. </view>
  27. <view
  28. v-if="showError && isError && !loading"
  29. class="u-image__error"
  30. :style="{
  31. borderRadius: shape == 'circle' ? '50%' : $u.addUnit(borderRadius)
  32. }"
  33. >
  34. <slot v-if="$slots.error" name="error" />
  35. <u-icon v-else :name="errorIcon" :width="width" :height="height"></u-icon>
  36. </view>
  37. </view>
  38. </template>
  39. <script>
  40. /**
  41. * Image 图片
  42. * @description 此组件为uni-app的image组件的加强版,在继承了原有功能外,还支持淡入动画、加载中、加载失败提示、圆角值和形状等。
  43. * @tutorial https://uviewui.com/components/image.html
  44. * @property {String} src 图片地址
  45. * @property {String} mode 裁剪模式,见官网说明
  46. * @value scaleToFill 不保持纵横比缩放图片,使图片的宽高完全拉伸至填满 image 元素
  47. * @value aspectFit 保持纵横比缩放图片,使图片的长边能完全显示出来。也就是说,可以完整地将图片显示出来。
  48. * @value aspectFill 保持纵横比缩放图片,只保证图片的短边能完全显示出来。也就是说,图片通常只在水平或垂直方向是完整的,另一个方向将会发生截取。
  49. * @value widthFix 宽度不变,高度自动变化,保持原图宽高比不变
  50. * @value heightFix 高度不变,宽度自动变化,保持原图宽高比不变 App 和 H5 平台 HBuilderX 2.9.3+ 支持、微信小程序需要基础库 2.10.3
  51. * @value top 不缩放图片,只显示图片的顶部区域
  52. * @value bottom 不缩放图片,只显示图片的底部区域
  53. * @value center 不缩放图片,只显示图片的中间区域
  54. * @value left 不缩放图片,只显示图片的左边区域
  55. * @value right 不缩放图片,只显示图片的右边区域
  56. * @value top left 不缩放图片,只显示图片的左上边区域
  57. * @value top right 不缩放图片,只显示图片的右上边区域
  58. * @value bottom left 不缩放图片,只显示图片的左下边区域
  59. * @value bottom right 不缩放图片,只显示图片的右下边区域
  60. * @property {String | Number} width 宽度,单位任意,如果为数值,则为rpx单位(默认100%)
  61. * @property {String | Number} height 高度,单位任意,如果为数值,则为rpx单位(默认 auto)
  62. * @property {String} shape 图片形状,circle-圆形,square-方形(默认square)
  63. * @property {String | Number} border-radius 圆角值,单位任意,如果为数值,则为rpx单位(默认 0)
  64. * @property {Boolean} lazy-load 是否懒加载,仅微信小程序、App、百度小程序、字节跳动小程序有效(默认 true)
  65. * @property {Boolean} show-menu-by-longpress 是否开启长按图片显示识别小程序码菜单,仅微信小程序有效(默认 false)
  66. * @property {String} loading-icon 加载中的图标,或者小图片(默认 photo)
  67. * @property {String} error-icon 加载失败的图标,或者小图片(默认 error-circle)
  68. * @property {Boolean} show-loading 是否显示加载中的图标或者自定义的slot(默认 true)
  69. * @property {Boolean} show-error 是否显示加载错误的图标或者自定义的slot(默认 true)
  70. * @property {Boolean} fade 是否需要淡入效果(默认 true)
  71. * @property {String Number} width 传入图片路径时图片的宽度
  72. * @property {String Number} height 传入图片路径时图片的高度
  73. * @property {Boolean} webp 只支持网络资源,只对微信小程序有效(默认 false)
  74. * @property {String | Number} duration 搭配fade参数的过渡时间,单位ms(默认 500)
  75. * @event {Function} click 点击图片时触发
  76. * @event {Function} error 图片加载失败时触发
  77. * @event {Function} load 图片加载成功时触发
  78. * @example <u-image width="100%" height="300rpx" :src="src"></u-image>
  79. */
  80. export default {
  81. name: 'u-image',
  82. emits: ["click", "error", "load"],
  83. props: {
  84. // 图片地址
  85. src: {
  86. type: [String,null],
  87. default: ''
  88. },
  89. // 裁剪模式
  90. mode: {
  91. type: String,
  92. default: 'aspectFill'
  93. },
  94. // 宽度,单位任意
  95. width: {
  96. type: [String, Number],
  97. default: '100%'
  98. },
  99. // 高度,单位任意
  100. height: {
  101. type: [String, Number],
  102. default: 'auto'
  103. },
  104. // 图片形状,circle-圆形,square-方形
  105. shape: {
  106. type: String,
  107. default: 'square'
  108. },
  109. // 圆角,单位任意
  110. borderRadius: {
  111. type: [String, Number],
  112. default: 0
  113. },
  114. // 是否懒加载,微信小程序、App、百度小程序、字节跳动小程序
  115. lazyLoad: {
  116. type: Boolean,
  117. default: true
  118. },
  119. // 开启长按图片显示识别微信小程序码菜单
  120. showMenuByLongpress: {
  121. type: Boolean,
  122. default: true
  123. },
  124. // 加载中的图标,或者小图片
  125. loadingIcon: {
  126. type: String,
  127. default: 'photo'
  128. },
  129. // 加载失败的图标,或者小图片
  130. errorIcon: {
  131. type: String,
  132. default: 'error-circle'
  133. },
  134. // 是否显示加载中的图标或者自定义的slot
  135. showLoading: {
  136. type: Boolean,
  137. default: true
  138. },
  139. // 是否显示加载错误的图标或者自定义的slot
  140. showError: {
  141. type: Boolean,
  142. default: true
  143. },
  144. // 是否需要淡入效果
  145. fade: {
  146. type: Boolean,
  147. default: true
  148. },
  149. // 只支持网络资源,只对微信小程序有效
  150. webp: {
  151. type: Boolean,
  152. default: false
  153. },
  154. // 过渡时间,单位ms
  155. duration: {
  156. type: [String, Number],
  157. default: 500
  158. },
  159. // 背景颜色,用于深色页面加载图片时,为了和背景色融合
  160. bgColor: {
  161. type: String,
  162. default: '#f3f4f6'
  163. }
  164. },
  165. data() {
  166. return {
  167. // 图片是否加载错误,如果是,则显示错误占位图
  168. isError: false,
  169. // 初始化组件时,默认为加载中状态
  170. loading: true,
  171. // 不透明度,为了实现淡入淡出的效果
  172. opacity: 1,
  173. // 过渡时间,因为props的值无法修改,故需要一个中间值
  174. durationTime: this.duration,
  175. // 图片加载完成时,去掉背景颜色,因为如果是png图片,就会显示灰色的背景
  176. backgroundStyle: {}
  177. };
  178. },
  179. watch: {
  180. src: {
  181. immediate: true,
  182. handler (n) {
  183. if(!n) {
  184. // 如果传入null或者'',或者false,或者undefined,标记为错误状态
  185. this.isError = true;
  186. this.loading = false;
  187. } else {
  188. this.isError = false;
  189. this.loading = true;
  190. }
  191. }
  192. }
  193. },
  194. computed: {
  195. wrapStyle() {
  196. let style = {};
  197. // 通过调用addUnit()方法,如果有单位,如百分比,px单位等,直接返回,如果是纯粹的数值,则加上rpx单位
  198. style.width = this.$u.addUnit(this.width);
  199. style.height = this.$u.addUnit(this.height);
  200. // 如果是配置了圆形,设置50%的圆角,否则按照默认的配置值
  201. style.borderRadius = this.shape == 'circle' ? '50%' : this.$u.addUnit(this.borderRadius);
  202. // 如果设置圆角,必须要有hidden,否则可能圆角无效
  203. style.overflow = this.borderRadius > 0 ? 'hidden' : 'visible';
  204. if (this.fade) {
  205. style.opacity = this.opacity;
  206. style.transition = `opacity ${Number(this.durationTime) / 1000}s ease-in-out`;
  207. }
  208. return style;
  209. }
  210. },
  211. methods: {
  212. // 点击图片
  213. onClick() {
  214. this.$emit('click');
  215. },
  216. // 图片加载失败
  217. onErrorHandler(err) {
  218. this.loading = false;
  219. this.isError = true;
  220. this.$emit('error', err);
  221. },
  222. // 图片加载完成,标记loading结束
  223. onLoadHandler() {
  224. this.loading = false;
  225. this.isError = false;
  226. this.$emit('load');
  227. // 如果不需要动画效果,就不执行下方代码,同时移除加载时的背景颜色
  228. // 否则无需fade效果时,png图片依然能看到下方的背景色
  229. if (!this.fade) return this.removeBgColor();
  230. // 原来opacity为1(不透明,是为了显示占位图),改成0(透明,意味着该元素显示的是背景颜色,默认的灰色),再改成1,是为了获得过渡效果
  231. this.opacity = 0;
  232. // 这里设置为0,是为了图片展示到背景全透明这个过程时间为0,延时之后延时之后重新设置为duration,是为了获得背景透明(灰色)
  233. // 到图片展示的过程中的淡入效果
  234. this.durationTime = 0;
  235. // 延时50ms,否则在浏览器H5,过渡效果无效
  236. setTimeout(() => {
  237. this.durationTime = this.duration;
  238. this.opacity = 1;
  239. setTimeout(() => {
  240. this.removeBgColor();
  241. }, this.durationTime);
  242. }, 50);
  243. },
  244. // 移除图片的背景色
  245. removeBgColor() {
  246. // 淡入动画过渡完成后,将背景设置为透明色,否则png图片会看到灰色的背景
  247. this.backgroundStyle = {
  248. backgroundColor: 'transparent'
  249. };
  250. }
  251. }
  252. };
  253. </script>
  254. <style scoped lang="scss">
  255. @import '../../libs/css/style.components.scss';
  256. .u-image {
  257. position: relative;
  258. transition: opacity 0.5s ease-in-out;
  259. &__image {
  260. width: 100%;
  261. height: 100%;
  262. }
  263. &__loading,
  264. &__error {
  265. position: absolute;
  266. top: 0;
  267. left: 0;
  268. width: 100%;
  269. height: 100%;
  270. @include vue-flex;
  271. align-items: center;
  272. justify-content: center;
  273. background-color: $u-bg-color;
  274. color: $u-tips-color;
  275. font-size: 46rpx;
  276. }
  277. }
  278. </style>