综合办公系统
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.

uv-image.vue 9.5KB

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