美文网首页
基于el-image,封装一个图片预览组件

基于el-image,封装一个图片预览组件

作者: surewinT | 来源:发表于2022-05-15 11:36 被阅读0次
show1.jpg

el-image的不足

el-image 本身设计已非常优秀,但图片预览功能存在以下不足:

1.没有相应的用户反馈,用户无法直观的知道该图片可以点击查看详情;

2.预览图片的列表需要单独配置一个数组,在实际开发中往往是需要查看当前图片;

show2.png

调用效果及代码

基于上述问题,新增用户交互反馈,支持单图、多图预览

show3.gif
<!--
 * @Date: 2022-05-16 10:03:11
 * @Author: surewinT 840325271@qq.com
 * @LastEditTime: 2022-05-16 11:00:07
 * @LastEditors: surewinT 840325271@qq.com
 * @Description: 调用页面
-->

<template>
    <div class="">
        <p-el-image
            v-for="(item, index) in imglist"
            :key="index"
            :src="item.path"
            width="200px"
            height="200px"
        />
    </div>
</template>

<script>
import PElImage from '@/components/p-el-image';
export default {
    components: {
        'p-el-image': PElImage,
    },
    props: [],
    data() {
        return {
            imglist: [
                {
                    id: 1,
                    path: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
                },
                {
                    id: 2,
                    path: [
                        'https://fuss10.elemecdn.com/a/3f/3302e58f9a181d2509f3dc0fa68b0jpeg.jpeg',
                        'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
                        'https://cube.elemecdn.com/6/94/4d3ea53c084bad6931a56d5158a48jpeg.jpeg',
                    ],
                },
            ],
        };
    },
    mounted() {},
    watch: {},
    methods: {},
};
</script>

<style lang='scss' scoped>
</style>

组件源码(p-el-image)

<!--
 * @Date: 2022-02-28 21:26:54
 * @Author: surewinT 840325271@qq.com
 * @LastEditTime: 2022-05-16 11:00:18
 * @LastEditors: surewinT 840325271@qq.com
 * @Description: 图片预览组件
-->

<template>
    <span
        class="image-item"
        :style="{
            width: width,
            height: height,
        }"
    >
        <span class="warp" @click="showImage">
            <span class="el-icon-view"></span>
        </span>
        <el-image
            ref="Image"
            class="image"
            :src="imgSrc"
            :preview-src-list="previewSrc"
        ></el-image>
    </span>
</template>

<script>
export default {
    components: {},
    props: {
        src: [Array, String],
        width: {
            typeof: String,
            default: '100px',
        },
        height: {
            typeof: String,
            default: '100px',
        },
    },
    data() {
        return {
            srcList: [],
            baseurl: '',
        };
    },
    mounted() {},
    watch: {},
    computed: {
        imgSrc() {
            if (typeof this.src == 'string') {
                return this.src;
            } else {
                return this.src[0];
            }
        },
        previewSrc() {
            if (typeof this.src == 'string') {
                return [this.src];
            } else {
                return this.src;
            }
        },
    },
    methods: {
        // 显示图片
        showImage() {
            this.$refs.Image.clickHandler();
            this.$emit('image-show', this.src);
        },
    },
};
</script>

<style lang='scss' scoped>
.image-item {
    // width: 100px;
    // height: 100px;
    position: relative;
    display: inline-block;
    cursor: pointer;
    & + .image-item {
        margin-left: 10px;
    }
    .image {
        width: 100%;
        height: 100%;
    }
    .warp {
        position: absolute;
        width: 100%;
        height: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
        color: #fff;
    }
    &:hover {
        .warp {
            z-index: 1;
            background-color: rgba(0, 0, 0, 0.7);
        }
    }
}
</style>

代码仓库

后续补充

相关文章

  • 图片预览-基础版

    基于element-ui 的图片预览,我这里没有封装组件,实际运用中要自己要封装组件 1、element-ui 自...

  • el-image-viewer 图片预览组件

    Element Ui为el-image组件带了图片预览功能,只需添加参数:preview-src-list="sr...

  • 高级3 面向对象实战

    1: 封装一个轮播组件 封装1预览地址封装2预览地址 2: 封装一个曝光加载组件 预览地址 3: 封装一个 Tab...

  • 面向对象实战

    封装一个轮播组件 代码效果预览 封装一个曝光加载组件 效果代码预览 封装一个 Tab 组件 效果代码

  • 面向对象实战

    1. 封装一个轮播组件 预览链接 2. 封装一个渐变轮播组件 预览链接 3. 封装一个曝光加载组件 预览链接 4....

  • web前端-深入(3)-面向对象实战

    题目1:封装一个轮播组件 代码预览 题目2:封装一个曝光加载组件 代码预览 题目3:封装一个 Tab 组件 代码预...

  • 面向对象 实战 -常用JS组件

    轮播组件轮播二次封装预览 曝光加载组件预览 Tab 组件预览 日历组件预览 Modal 组件预览

  • 高级任务3

    1、封装一个轮播组件 预览 代码github 2、封装一个曝光加载组件 预览 代码github 3、封装一个 Ta...

  • 一些面向对象的projects

    1. 封装一个轮播组件 代码地址预览效果 2. 封装一个曝光加载组件 代码地址效果预览 3. 封装一个 Tab 组...

  • [Vue warn]: Invalid prop: type c

    使用elementui中的el-image预览查看图片报错 报错信息: [Vue warn]: Invalid p...

网友评论

      本文标题:基于el-image,封装一个图片预览组件

      本文链接:https://www.haomeiwen.com/subject/ixaiurtx.html