u-image.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <template>
  2. <view class="u-image" @tap="onClick" :style="[wrapStyle, backgroundStyle]">
  3. <image
  4. v-if="!isError"
  5. :src="getSrc"
  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: this.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. import site from '@/core/site'
  67. export default {
  68. name: 'u-image',
  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. }
  176. }
  177. }
  178. },
  179. computed: {
  180. wrapStyle() {
  181. let style = {};
  182. // 通过调用addUnit()方法,如果有单位,如百分比,px单位等,直接返回,如果是纯粹的数值,则加上rpx单位
  183. style.width = this.$u.addUnit(this.width);
  184. style.height = this.$u.addUnit(this.height);
  185. // 如果是配置了圆形,设置50%的圆角,否则按照默认的配置值
  186. style.borderRadius = this.shape == 'circle' ? '50%' : this.$u.addUnit(this.borderRadius);
  187. // 如果设置圆角,必须要有hidden,否则可能圆角无效
  188. style.overflow = this.borderRadius > 0 ? 'hidden' : 'visible';
  189. if (this.fade) {
  190. style.opacity = this.opacity;
  191. style.transition = `opacity ${Number(this.durationTime) / 1000}s ease-in-out`;
  192. }
  193. return style;
  194. },
  195. getSrc(){
  196. let v = this.vuex_user_config ? this.vuex_user_config.img_version : '1.0'
  197. if(this.src.indexOf(site.root) === -1){
  198. return this.src;
  199. }
  200. return this.src +`?v=${v}`
  201. }
  202. },
  203. methods: {
  204. // 点击图片
  205. onClick() {
  206. this.$emit('click');
  207. },
  208. // 图片加载失败
  209. onErrorHandler(err) {
  210. this.loading = false;
  211. this.isError = true;
  212. this.$emit('error', err);
  213. },
  214. // 图片加载完成,标记loading结束
  215. onLoadHandler() {
  216. this.loading = false;
  217. this.isError = false;
  218. this.$emit('load');
  219. // 如果不需要动画效果,就不执行下方代码,同时移除加载时的背景颜色
  220. // 否则无需fade效果时,png图片依然能看到下方的背景色
  221. if (!this.fade) return this.removeBgColor();
  222. // 原来opacity为1(不透明,是为了显示占位图),改成0(透明,意味着该元素显示的是背景颜色,默认的灰色),再改成1,是为了获得过渡效果
  223. this.opacity = 0;
  224. // 这里设置为0,是为了图片展示到背景全透明这个过程时间为0,延时之后延时之后重新设置为duration,是为了获得背景透明(灰色)
  225. // 到图片展示的过程中的淡入效果
  226. this.durationTime = 0;
  227. // 延时50ms,否则在浏览器H5,过渡效果无效
  228. setTimeout(() => {
  229. this.durationTime = this.duration;
  230. this.opacity = 1;
  231. setTimeout(() => {
  232. this.removeBgColor();
  233. }, this.durationTime);
  234. }, 50);
  235. },
  236. // 移除图片的背景色
  237. removeBgColor() {
  238. // 淡入动画过渡完成后,将背景设置为透明色,否则png图片会看到灰色的背景
  239. this.backgroundStyle = {
  240. backgroundColor: 'transparent'
  241. };
  242. }
  243. }
  244. };
  245. </script>
  246. <style scoped lang="scss">
  247. @import '../../libs/css/style.components.scss';
  248. .u-image {
  249. position: relative;
  250. transition: opacity 0.5s ease-in-out;
  251. &__image {
  252. width: 100%;
  253. height: 100%;
  254. }
  255. &__loading,
  256. &__error {
  257. position: absolute;
  258. top: 0;
  259. left: 0;
  260. width: 100%;
  261. height: 100%;
  262. @include vue-flex;
  263. align-items: center;
  264. justify-content: center;
  265. background-color: $u-bg-color;
  266. color: $u-tips-color;
  267. font-size: 46rpx;
  268. }
  269. }
  270. </style>