1
1
<template >
2
- <view
3
- class =" u-image"
4
- @tap =" onClick"
5
- :style =" [wrapStyle, backgroundStyle]"
6
- >
2
+ <view class =" u-image" @tap =" onClick" :style =" [wrapStyle, backgroundStyle]" >
7
3
<image
8
4
v-if =" !isError"
9
5
:src =" src"
13
9
:lazy-load =" lazyLoad"
14
10
class =" u-image__image"
15
11
:style =" {
16
- borderRadius: shape == 'circle' ? '50%' : $u.addUnit(borderRadius),
12
+ borderRadius: shape == 'circle' ? '50%' : $u.addUnit(borderRadius)
17
13
}"
18
14
></image >
19
- <view v-if =" showLoading && loading" class =" u-image__loading" :style =" {
20
- borderRadius: shape == 'circle' ? '50%' : $u.addUnit(borderRadius),
21
- backgroundColor: this.bgColor
22
- }" >
15
+ <view
16
+ v-if =" showLoading && loading"
17
+ class =" u-image__loading"
18
+ :style =" {
19
+ borderRadius: shape == 'circle' ? '50%' : $u.addUnit(borderRadius),
20
+ backgroundColor: this.bgColor
21
+ }"
22
+ >
23
23
<slot v-if =" $slots.loading" name =" loading" />
24
24
<u-icon v-else :name =" loadingIcon" ></u-icon >
25
25
</view >
26
- <view v-if =" showError && isError && !loading" class =" u-image__error" :style =" {
27
- borderRadius: shape == 'circle' ? '50%' : $u.addUnit(borderRadius),
28
- }" >
26
+ <view
27
+ v-if =" showError && isError && !loading"
28
+ class =" u-image__error"
29
+ :style =" {
30
+ borderRadius: shape == 'circle' ? '50%' : $u.addUnit(borderRadius)
31
+ }"
32
+ >
29
33
<slot v-if =" $slots.error" name =" error" />
30
34
<u-icon v-else :name =" errorIcon" ></u-icon >
31
35
</view >
32
36
</view >
33
37
</template >
34
38
35
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 {Boolean} webp 只支持网络资源,只对微信小程序有效(默认 false)
58
+ * @property {String | Number} duration 搭配fade参数的过渡时间,单位ms(默认 500)
59
+ * @event {Function} click 点击图片时触发
60
+ * @event {Function} error 图片加载失败时触发
61
+ * @event {Function} load 图片加载成功时触发
62
+ * @example < u- image width= " 100%" height= " 300rpx" : src= " src" >< / u- image>
63
+ */
36
64
export default {
37
65
props: {
38
66
// 图片地址
@@ -140,7 +168,7 @@ export default {
140
168
style .borderRadius = this .shape == ' circle' ? ' 50%' : this .$u .addUnit (this .borderRadius );
141
169
// 如果设置圆角,必须要有hidden,否则可能圆角无效
142
170
style .overflow = this .borderRadius > 0 ? ' hidden' : ' visible' ;
143
- if (this .fade ) {
171
+ if (this .fade ) {
144
172
style .opacity = this .opacity ;
145
173
style .transition = ` opacity ${ Number (this .durationTime ) / 1000 } s ease-in-out` ;
146
174
}
@@ -165,7 +193,7 @@ export default {
165
193
this .$emit (' load' );
166
194
// 如果不需要动画效果,就不执行下方代码,同时移除加载时的背景颜色
167
195
// 否则无需fade效果时,png图片依然能看到下方的背景色
168
- if (! this .fade ) return this .removeBgColor ();
196
+ if (! this .fade ) return this .removeBgColor ();
169
197
// 原来opacity为1(不透明,是为了显示占位图),改成0(透明,意味着该元素显示的是背景颜色,默认的灰色),再改成1,是为了获得过渡效果
170
198
this .opacity = 0 ;
171
199
// 这里设置为0,是为了图片展示到背景全透明这个过程时间为0,延时之后延时之后重新设置为duration,是为了获得背景透明(灰色)
@@ -177,8 +205,8 @@ export default {
177
205
this .opacity = 1 ;
178
206
setTimeout (() => {
179
207
this .removeBgColor ();
180
- }, this .durationTime )
181
- }, 50 )
208
+ }, this .durationTime );
209
+ }, 50 );
182
210
},
183
211
// 移除图片的背景色
184
212
removeBgColor () {
@@ -203,7 +231,8 @@ export default {
203
231
height : 100% ;
204
232
}
205
233
206
- & __loading , & __error {
234
+ & __loading ,
235
+ & __error {
207
236
position : absolute ;
208
237
top : 0 ;
209
238
left : 0 ;
0 commit comments