vue 自定义image组件 - Go语言中文社区

vue 自定义image组件


介绍

1:当图片加载失败时,给出错误提示。

 

2:当图片加载中时,给出加载提示。

 

3:图片处理模式:等比缩放/裁剪/填充/...

 

1、图片加载状态处理

 通过给图片绑定load事件与error事件处理函数来判断图片加载状态。当图片加载完成时会触发load事件;图片加载出错会触发error事件

// 样本
<img src="..." @load=onLoad @error=onError>

 

2、图片模式

 通过css属性 object-fit(https://developer.mozilla.org/zh-CN/docs/Web/CSS/object-fit) 来控制图片如何适应容器大小。

 

 

3、代码

<template>
    <div class="tm-image" :style="style" @click="onClick">
        <img :src="src"
             :alt="alt"
             :style="{'object-fit': mode}"
             @load="onLoad"
             @error="onError">
        <div v-if="this.loading" class="tm-image-load">
            <slot name="loading">加载中</slot>
        </div>
        <div v-if="this.error" class="tm-image-error">
            <slot name="error">加载出错</slot>
        </div>
    </div>
</template>
<script>
    import suffixPx from '../../utils/suffixPx.js';

    export default {
        name: "tm-image",
        data() {
            return {
                loading: true,
                error: false
            }
        },
        props: {
            src: String,
            alt: String,
            mode: {  // 模式:https://developer.mozilla.org/zh-CN/docs/Web/CSS/object-fit
                type: String,
                default: "fill",
                validator(value) {
                    return ['contain', 'cover', 'fill', 'none', 'scale-down'].includes(value);
                }
            },
            width: [String, Number],
            height: [String, Number]
        },
        computed: {
            style() {
                return {
                    width: suffixPx(this.width),
                    height: suffixPx(this.height)
                }
            }
        },
        methods: {
            onClick(event) {
                this.$emit('click', event);   // 向父节点传递一个自定义事件
            },
            onLoad(event) {
                this.loading = false;
                this.$emit('loading', event); // 向父节点传递一个自定义事件
            },
            onError(event) {
                this.loading = false;
                this.error = true;
                this.$emit('error', event); // 向父节点传递一个自定义事件
            }
        }
    }
</script>
<style scoped>
    .tm-image{
        position: relative;
        overflow: hidden;
    }
    .tm-image .tm-image-load,
    .tm-image .tm-image-error{
        position: absolute;
        top:0;
        left: 0;
        width: 100%;
        height: 100%;
        background: #f2f2f2;
        color: #666;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    .tm-image img {
        width: 100%;
        height: 100%;
    }
</style>

 

个人项目

Vue组件https://gitee.com/whnba/component

 

淘宝天猫粉丝专享优惠券

版权声明:本文来源博客园,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://www.cnblogs.com/whnba/p/10961510.html
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2019-11-17 16:51:34
  • 阅读 ( 1094 )
  • 分类:前端

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢