uv-image.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <template>
  2. <uv-transition
  3. v-if="show"
  4. :show="show"
  5. mode="fade"
  6. :duration="fade ? duration : 0"
  7. :cell-child="cellChild"
  8. :custom-style="wrapStyle"
  9. >
  10. <view
  11. class="uv-image"
  12. :class="[`uv-image--${elIndex}`]"
  13. @tap="onClick"
  14. :style="[wrapStyle, backgroundStyle]"
  15. >
  16. <image
  17. v-if="!isError && observeShow"
  18. :src="src"
  19. :mode="mode"
  20. @error="onErrorHandler"
  21. @load="onLoadHandler"
  22. :show-menuv-by-longpress="showMenuByLongpress"
  23. :lazy-load="lazyLoad"
  24. class="uv-image__image"
  25. :style="[imageStyle]"
  26. ></image>
  27. <view
  28. v-if="showLoading && loading"
  29. class="uv-image__loading"
  30. :style="{
  31. borderRadius: shape == 'circle' ? '50%' : $uv.addUnit(radius),
  32. backgroundColor: bgColor,
  33. width: $uv.addUnit(width),
  34. height: $uv.addUnit(height)
  35. }"
  36. >
  37. <slot name="loading">
  38. <uv-icon
  39. :name="loadingIcon"
  40. :width="width"
  41. :height="height"
  42. ></uv-icon>
  43. </slot>
  44. </view>
  45. <view
  46. v-if="showError && isError && !loading"
  47. class="uv-image__error"
  48. :style="{
  49. borderRadius: shape == 'circle' ? '50%' : $uv.addUnit(radius),
  50. width: $uv.addUnit(width),
  51. height: $uv.addUnit(height)
  52. }"
  53. >
  54. <slot name="error">
  55. <uv-icon
  56. :name="errorIcon"
  57. :width="width"
  58. :height="height"
  59. ></uv-icon>
  60. </slot>
  61. </view>
  62. </view>
  63. </uv-transition>
  64. </template>
  65. <script>
  66. import mpMixin from '@/uni_modules/uv-ui-tools/libs/mixin/mpMixin.js'
  67. import mixin from '@/uni_modules/uv-ui-tools/libs/mixin/mixin.js'
  68. import props from './props.js';
  69. /**
  70. * Image 图片
  71. * @description 此组件为uni-app的image组件的加强版,在继承了原有功能外,还支持淡入动画、加载中、加载失败提示、圆角值和形状等。
  72. * @tutorial https://www.uvui.cn/components/image.html
  73. * @property {String} src 图片地址
  74. * @property {String} mode 裁剪模式,见官网说明 (默认 'aspectFill' )
  75. * @property {String | Number} width 宽度,单位任意,如果为数值,则为px单位 (默认 '300' )
  76. * @property {String | Number} height 高度,单位任意,如果为数值,则为px单位 (默认 '225' )
  77. * @property {String} shape 图片形状,circle-圆形,square-方形 (默认 'square' )
  78. * @property {String | Number} radius 圆角值,单位任意,如果为数值,则为px单位 (默认 0 )
  79. * @property {Boolean} lazyLoad 是否懒加载,仅微信小程序、App、百度小程序、字节跳动小程序有效 (默认 true )
  80. * @property {Boolean} showMenuByLongpress 是否开启长按图片显示识别小程序码菜单,仅微信小程序有效 (默认 true )
  81. * @property {String} loadingIcon 加载中的图标,或者小图片 (默认 'photo' )
  82. * @property {String} errorIcon 加载失败的图标,或者小图片 (默认 'error-circle' )
  83. * @property {Boolean} showLoading 是否显示加载中的图标或者自定义的slot (默认 true )
  84. * @property {Boolean} showError 是否显示加载错误的图标或者自定义的slot (默认 true )
  85. * @property {Boolean} fade 是否需要淡入效果 (默认 true )
  86. * @property {Boolean} webp 只支持网络资源,只对微信小程序有效 (默认 false )
  87. * @property {String | Number} duration 搭配fade参数的过渡时间,单位ms (默认 500 )
  88. * @property {String} bgColor 背景颜色,用于深色页面加载图片时,为了和背景色融合 (默认 '#f3f4f6' )
  89. * @property {Object} customStyle 定义需要用到的外部样式
  90. * @event {Function} click 点击图片时触发
  91. * @event {Function} error 图片加载失败时触发
  92. * @event {Function} load 图片加载成功时触发
  93. * @example <uv-image width="100%" height="300px" :src="src"></uv-image>
  94. */
  95. export default {
  96. name: 'uv-image',
  97. emits: ['click','load','error'],
  98. mixins: [mpMixin, mixin, props],
  99. data() {
  100. return {
  101. // 图片是否加载错误,如果是,则显示错误占位图
  102. isError: false,
  103. // 初始化组件时,默认为加载中状态
  104. loading: true,
  105. // 图片加载完成时,去掉背景颜色,因为如果是png图片,就会显示灰色的背景
  106. backgroundStyle: {},
  107. // 用于fade模式的控制组件显示与否
  108. show: false,
  109. // 是否开启图片出现在可视范围进行加载(另一种懒加载)
  110. observeShow: !this.observeLazyLoad,
  111. elIndex: '',
  112. // 因为props的值无法修改,故需要一个中间值
  113. imgWidth: this.width,
  114. // 因为props的值无法修改,故需要一个中间值
  115. imgHeight: this.height,
  116. thresholdValue: 50
  117. };
  118. },
  119. watch: {
  120. src: {
  121. immediate: true,
  122. handler(n) {
  123. if (!n) {
  124. // 如果传入null或者'',或者false,或者undefined,标记为错误状态
  125. this.isError = true
  126. } else {
  127. this.isError = false;
  128. this.loading = true;
  129. }
  130. }
  131. },
  132. width(newVal){
  133. // 这样做的目的是避免在更新时候,某些平台动画会恢复关闭状态
  134. this.show = false;
  135. this.$uv.sleep(2).then(res=>{
  136. this.show = true;
  137. });
  138. this.imgWidth = newVal;
  139. },
  140. height(newVal){
  141. // 这样做的目的是避免在更新时候,某些平台动画会恢复关闭状态
  142. this.show = false;
  143. this.$uv.sleep(2).then(res=>{
  144. this.show = true;
  145. });
  146. this.imgHeight = newVal;
  147. }
  148. },
  149. computed: {
  150. wrapStyle() {
  151. let style = {};
  152. // 通过调用addUnit()方法,如果有单位,如百分比,px单位等,直接返回,如果是纯粹的数值,则加上rpx单位
  153. if(this.mode !== 'heightFix') {
  154. style.width = this.$uv.addUnit(this.imgWidth);
  155. }
  156. if(this.mode !== 'widthFix') {
  157. style.height = this.$uv.addUnit(this.imgHeight);
  158. }
  159. // 如果是显示圆形,设置一个很多的半径值即可
  160. style.borderRadius = this.shape == 'circle' ? '10000px' : this.$uv.addUnit(this.radius)
  161. // 如果设置圆角,必须要有hidden,否则可能圆角无效
  162. style.overflow = this.radius > 0 ? 'hidden' : 'visible'
  163. return this.$uv.deepMerge(style, this.$uv.addStyle(this.customStyle));
  164. },
  165. imageStyle() {
  166. let style = {};
  167. style.borderRadius = this.shape == 'circle' ? '10000px' : this.$uv.addUnit(this.radius);
  168. // #ifdef APP-NVUE
  169. style.width = this.$uv.addUnit(this.imgWidth);
  170. style.height = this.$uv.addUnit(this.imgHeight);
  171. // #endif
  172. return style;
  173. }
  174. },
  175. created() {
  176. this.elIndex = this.$uv.guid();
  177. this.observer = {}
  178. this.observerName = 'lazyLoadContentObserver'
  179. },
  180. mounted() {
  181. this.show = true;
  182. if(this.observeLazyLoad) this.observerFn();
  183. },
  184. methods: {
  185. // 点击图片
  186. onClick() {
  187. this.$emit('click')
  188. },
  189. // 图片加载失败
  190. onErrorHandler(err) {
  191. this.loading = false
  192. this.isError = true
  193. this.$emit('error', err)
  194. },
  195. // 图片加载完成,标记loading结束
  196. onLoadHandler(event) {
  197. if(this.mode == 'widthFix') this.imgHeight = 'auto'
  198. if(this.mode == 'heightFix') this.imgWidth = 'auto'
  199. this.loading = false
  200. this.isError = false
  201. this.$emit('load', event)
  202. this.removeBgColor()
  203. },
  204. // 移除图片的背景色
  205. removeBgColor() {
  206. // 淡入动画过渡完成后,将背景设置为透明色,否则png图片会看到灰色的背景
  207. this.backgroundStyle = {
  208. backgroundColor: 'transparent'
  209. };
  210. },
  211. // 观察图片是否在可见视口
  212. observerFn(){
  213. // 在需要用到懒加载的页面,在触发底部的时候触发tOnLazyLoadReachBottom事件,保证所有图片进行加载
  214. this.$nextTick(() => {
  215. uni.$once('onLazyLoadReachBottom', () => {
  216. if (!this.observeShow) this.observeShow = true
  217. })
  218. })
  219. setTimeout(() => {
  220. // #ifndef APP-NVUE
  221. this.disconnectObserver(this.observerName)
  222. const contentObserver = uni.createIntersectionObserver(this)
  223. contentObserver.relativeToViewport({
  224. bottom: this.thresholdValue
  225. }).observe(`.uv-image--${this.elIndex}`, (res) => {
  226. if (res.intersectionRatio > 0) {
  227. // 懒加载状态改变
  228. this.observeShow = true
  229. // 如果图片已经加载,去掉监听,减少性能消耗
  230. this.disconnectObserver(this.observerName)
  231. }
  232. })
  233. this[this.observerName] = contentObserver
  234. // #endif
  235. // #ifdef APP-NVUE
  236. this.observeShow = true;
  237. // #endif
  238. }, 50)
  239. },
  240. disconnectObserver(observerName) {
  241. const observer = this[observerName]
  242. observer && observer.disconnect()
  243. }
  244. }
  245. };
  246. </script>
  247. <style lang="scss" scoped>
  248. @import '@/uni_modules/uv-ui-tools/libs/css/components.scss';
  249. @import '@/uni_modules/uv-ui-tools/libs/css/color.scss';
  250. $uv-image-error-top:0px !default;
  251. $uv-image-error-left:0px !default;
  252. $uv-image-error-width:100% !default;
  253. $uv-image-error-hight:100% !default;
  254. $uv-image-error-background-color:$uv-bg-color !default;
  255. $uv-image-error-color:$uv-tips-color !default;
  256. $uv-image-error-font-size: 46rpx !default;
  257. .uv-image {
  258. position: relative;
  259. transition: opacity 0.5s ease-in-out;
  260. &__image {
  261. width: 100%;
  262. height: 100%;
  263. }
  264. &__loading,
  265. &__error {
  266. position: absolute;
  267. top: $uv-image-error-top;
  268. left: $uv-image-error-left;
  269. width: $uv-image-error-width;
  270. height: $uv-image-error-hight;
  271. @include flex;
  272. align-items: center;
  273. justify-content: center;
  274. background-color: $uv-image-error-background-color;
  275. color: $uv-image-error-color;
  276. font-size: $uv-image-error-font-size;
  277. }
  278. }
  279. </style>