u-image.vue 8 KB
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
<template>
<u-transition
mode="fade"
:show="show"
:duration="fade ? 1000 : 0"
>
<view
class="u-image"
@tap="onClick"
:style="[wrapStyle, backgroundStyle]"
>
<image
v-if="!isError"
:src="src"
:mode="mode"
@error="onErrorHandler"
@load="onLoadHandler"
:show-menu-by-longpress="showMenuByLongpress"
:lazy-load="lazyLoad"
class="u-image__image"
:style="{
borderRadius: shape == 'circle' ? '10000px' : $u.addUnit(radius),
width: $u.addUnit(width),
height: $u.addUnit(height)
}"
></image>
<view
v-if="showLoading && loading"
class="u-image__loading"
:style="{
borderRadius: shape == 'circle' ? '50%' : $u.addUnit(radius),
backgroundColor: this.bgColor,
width: $u.addUnit(width),
height: $u.addUnit(height)
}"
>
<slot name="loading">
<u-icon
:name="loadingIcon"
:width="width"
:height="height"
></u-icon>
</slot>
</view>
<view
v-if="showError && isError && !loading"
class="u-image__error"
:style="{
borderRadius: shape == 'circle' ? '50%' : $u.addUnit(radius),
width: $u.addUnit(width),
height: $u.addUnit(height)
}"
>
<slot name="error">
<u-icon
:name="errorIcon"
:width="width"
:height="height"
></u-icon>
</slot>
</view>
</view>
</u-transition>
</template>

<script>
import props from './props.js';
/**
* Image 图片
* @description 此组件为uni-app的image组件的加强版,在继承了原有功能外,还支持淡入动画、加载中、加载失败提示、圆角值和形状等。
* @tutorial https://uviewui.com/components/image.html
* @property {String} src 图片地址
* @property {String} mode 裁剪模式,见官网说明 (默认 'aspectFill' )
* @property {String | Number} width 宽度,单位任意,如果为数值,则为px单位 (默认 '300' )
* @property {String | Number} height 高度,单位任意,如果为数值,则为px单位 (默认 '225' )
* @property {String} shape 图片形状,circle-圆形,square-方形 (默认 'square' )
* @property {String | Number} radius 圆角值,单位任意,如果为数值,则为px单位 (默认 0 )
* @property {Boolean} lazyLoad 是否懒加载,仅微信小程序、App、百度小程序、字节跳动小程序有效 (默认 true )
* @property {Boolean} showMenuByLongpress 是否开启长按图片显示识别小程序码菜单,仅微信小程序有效 (默认 true )
* @property {String} loadingIcon 加载中的图标,或者小图片 (默认 'photo' )
* @property {String} errorIcon 加载失败的图标,或者小图片 (默认 'error-circle' )
* @property {Boolean} showLoading 是否显示加载中的图标或者自定义的slot (默认 true )
* @property {Boolean} showError 是否显示加载错误的图标或者自定义的slot (默认 true )
* @property {Boolean} fade 是否需要淡入效果 (默认 true )
* @property {Boolean} webp 只支持网络资源,只对微信小程序有效 (默认 false )
* @property {String | Number} duration 搭配fade参数的过渡时间,单位ms (默认 500 )
* @property {String} bgColor 背景颜色,用于深色页面加载图片时,为了和背景色融合 (默认 '#f3f4f6' )
* @property {Object} customStyle 定义需要用到的外部样式
* @event {Function} click 点击图片时触发
* @event {Function} error 图片加载失败时触发
* @event {Function} load 图片加载成功时触发
* @example <u-image width="100%" height="300px" :src="src"></u-image>
*/
export default {
name: 'u-image',
mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
data() {
return {
// 图片是否加载错误,如果是,则显示错误占位图
isError: false,
// 初始化组件时,默认为加载中状态
loading: true,
// 不透明度,为了实现淡入淡出的效果
opacity: 1,
// 过渡时间,因为props的值无法修改,故需要一个中间值
durationTime: this.duration,
// 图片加载完成时,去掉背景颜色,因为如果是png图片,就会显示灰色的背景
backgroundStyle: {},
// 用于fade模式的控制组件显示与否
show: false
};
},
watch: {
src: {
immediate: true,
handler(n) {
if (!n) {
// 如果传入null或者'',或者false,或者undefined,标记为错误状态
this.isError = true
} else {
this.isError = false;
this.loading = true;
}
}
}
},
computed: {
wrapStyle() {
let style = {};
// 通过调用addUnit()方法,如果有单位,如百分比,px单位等,直接返回,如果是纯粹的数值,则加上rpx单位
style.width = this.$u.addUnit(this.width);
style.height = this.$u.addUnit(this.height);
// 如果是显示圆形,设置一个很多的半径值即可
style.borderRadius = this.shape == 'circle' ? '10000px' : uni.$u.addUnit(this.radius)
// 如果设置圆角,必须要有hidden,否则可能圆角无效
style.overflow = this.borderRadius > 0 ? 'hidden' : 'visible'
// if (this.fade) {
// style.opacity = this.opacity
// // nvue下,这几个属性必须要分开写
// style.transitionDuration = `${this.durationTime}ms`
// style.transitionTimingFunction = 'ease-in-out'
// style.transitionProperty = 'opacity'
// }
return uni.$u.deepMerge(style, uni.$u.addStyle(this.customStyle));

}
},
mounted() {
this.show = true
},
methods: {
// 点击图片
onClick() {
this.$emit('click')
},
// 图片加载失败
onErrorHandler(err) {
this.loading = false
this.isError = true
this.$emit('error', err)
},
// 图片加载完成,标记loading结束
onLoadHandler(event) {
this.loading = false
this.isError = false
this.$emit('load', event)
this.removeBgColor()
// 如果不需要动画效果,就不执行下方代码,同时移除加载时的背景颜色
// 否则无需fade效果时,png图片依然能看到下方的背景色
// if (!this.fade) return this.removeBgColor();
// // 原来opacity为1(不透明,是为了显示占位图),改成0(透明,意味着该元素显示的是背景颜色,默认的灰色),再改成1,是为了获得过渡效果
// this.opacity = 0;
// // 这里设置为0,是为了图片展示到背景全透明这个过程时间为0,延时之后延时之后重新设置为duration,是为了获得背景透明(灰色)
// // 到图片展示的过程中的淡入效果
// this.durationTime = 0;
// // 延时50ms,否则在浏览器H5,过渡效果无效
// setTimeout(() => {
// this.durationTime = this.duration;
// this.opacity = 1;
// setTimeout(() => {
// this.removeBgColor();
// }, this.durationTime);
// }, 50);
},
// 移除图片的背景色
removeBgColor() {
// 淡入动画过渡完成后,将背景设置为透明色,否则png图片会看到灰色的背景
this.backgroundStyle = {
backgroundColor: 'transparent'
};
}
}
};
</script>

<style lang="scss" scoped>
@import '../../libs/css/components.scss';

$u-image-error-top:0px !default;
$u-image-error-left:0px !default;
$u-image-error-width:100% !default;
$u-image-error-hight:100% !default;
$u-image-error-background-color:$u-bg-color !default;
$u-image-error-color:$u-tips-color !default;
$u-image-error-font-size: 46rpx !default;

.u-image {
position: relative;
transition: opacity 0.5s ease-in-out;

&__image {
width: 100%;
height: 100%;
}

&__loading,
&__error {
position: absolute;
top: $u-image-error-top;
left: $u-image-error-left;
width: $u-image-error-width;
height: $u-image-error-hight;
@include flex;
align-items: center;
justify-content: center;
background-color: $u-image-error-background-color;
color: $u-image-error-color;
font-size: $u-image-error-font-size;
}
}
</style>