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.

u-image.vue 8.2KB

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