123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- <template>
- <view class="image-viewer" :style="containerStyle">
- <image
- :src="src"
- :mode="mode"
- :style="imageStyle"
- :lazy-load="lazyLoad"
- @load="onImageLoad"
- @error="onImageError"
- @tap="onImageTap"
- ></image>
- </view>
- </template>
- <script>
- export default {
- name: 'ImageViewer',
- props: {
- // 图片地址
- src: {
- type: String,
- required: true
- },
- // 容器宽度
- width: {
- type: [String, Number],
- default: '100%'
- },
- // 容器高度
- height: {
- type: [String, Number],
- default: 'auto'
- },
- // 图片展示模式
- mode: {
- type: String,
- default: 'aspectFit',
- validator: (value) => {
- return ['aspectFit', 'aspectFill', 'widthFix', 'heightFix'].includes(value)
- }
- },
- // 是否懒加载
- lazyLoad: {
- type: Boolean,
- default: true
- },
- // 图片圆角
- radius: {
- type: [String, Number],
- default: 0
- },
- // 是否可点击放大
- preview: {
- type: Boolean,
- default: false
- }
- },
- data() {
- return {
- imageWidth: 0,
- imageHeight: 0,
- loaded: false
- }
- },
- computed: {
- containerStyle() {
- const style = {
- width: typeof this.width === 'number' ? `${this.width}rpx` : this.width,
- height: typeof this.height === 'number' ? `${this.height}rpx` : this.height,
- borderRadius: typeof this.radius === 'number' ? `${this.radius}rpx` : this.radius
- }
- return style
- },
- imageStyle() {
- const style = {
- width: '100%',
- height: '100%'
- }
- return style
- }
- },
- methods: {
- onImageLoad(e) {
- this.loaded = true
- this.imageWidth = e.detail.width
- this.imageHeight = e.detail.height
- this.$emit('load', e)
- },
- onImageError(e) {
- this.$emit('error', e)
- },
- onImageTap() {
- if (this.preview) {
- uni.previewImage({
- urls: [this.src],
- current: this.src
- })
- }
- this.$emit('tap')
- }
- }
- }
- </script>
- <style lang="scss">
- .image-viewer {
- position: relative;
- overflow: hidden;
- width: 100%;
- image {
- display: block;
- transition: opacity 0.3s;
- }
- }
- </style>
|